From 364c3627a45cc3e6d0d336debf2710b254666bf2 Mon Sep 17 00:00:00 2001 From: stringsplit <77242831+nbitzz@users.noreply.github.com> Date: Tue, 30 Apr 2024 17:24:50 -0700 Subject: [PATCH] /api/v1/info --- src/server/index.ts | 28 ++++++++++++++-------------- src/server/routes/api/v1/api.json | 1 + src/server/routes/api/v1/info.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 src/server/routes/api/v1/info.ts diff --git a/src/server/index.ts b/src/server/index.ts index 9cf2e34..a96c6da 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -4,13 +4,10 @@ import { Hono } from "hono" import fs from "fs" import { readFile } from "fs/promises" import Files from "./lib/files.js" -import { getAccount } from "./lib/middleware.js" import APIRouter from "./routes/api.js" -import preview from "./routes/api/web/preview.js" import { fileURLToPath } from "url" import { dirname } from "path" -import pkg from "../../package.json" assert { type: "json" } -import config, { ClientConfiguration } from "./lib/config.js" +import config from "./lib/config.js" const app = new Hono() @@ -60,16 +57,6 @@ if (config.forceSSL) { }) } -app.get("/server", (ctx) => - ctx.json({ - version: pkg.version, - files: Object.keys(files.files).length, - maxDiscordFiles: config.maxDiscordFiles, - maxDiscordFileSize: config.maxDiscordFileSize, - accounts: config.accounts, - } as ClientConfiguration) -) - // funcs // init data @@ -87,6 +74,19 @@ apiRouter.loadAPIMethods().then(() => { console.log("API OK!") // moved here to ensure it's matched last + app.get("/server", async (ctx) => + app.fetch( + new Request( + new URL( + "/api/v1/info", + ctx.req.raw.url + ).href, + ctx.req.raw + ), + ctx.env + ) + ) + app.get("/:fileId", async (ctx) => app.fetch( new Request( diff --git a/src/server/routes/api/v1/api.json b/src/server/routes/api/v1/api.json index 52553b5..fc46ef7 100644 --- a/src/server/routes/api/v1/api.json +++ b/src/server/routes/api/v1/api.json @@ -4,6 +4,7 @@ "mount": [ "account", "session", + "info", { "file": "file/index", "to": "/file" diff --git a/src/server/routes/api/v1/info.ts b/src/server/routes/api/v1/info.ts new file mode 100644 index 0000000..392f011 --- /dev/null +++ b/src/server/routes/api/v1/info.ts @@ -0,0 +1,29 @@ +import { Hono } from "hono" +import * as Accounts from "../../../lib/accounts.js" +import { HttpBindings } from "@hono/node-server" +import pkg from "../../../../../package.json" assert {type: "json"} +import config, { ClientConfiguration } from "../../../lib/config.js" +import type Files from "../../../lib/files.js" + +const router = new Hono<{ + Variables: { + account: Accounts.Account + }, + Bindings: HttpBindings +}>() + +export default function(files: Files) { + + router.get("/", async (ctx) => + ctx.json({ + version: pkg.version, + files: Object.keys(files.files).length, + totalSize: Object.values(files.files).filter(e => e.sizeInBytes).reduce((acc,cur)=>acc+cur.sizeInBytes!,0), + maxDiscordFiles: config.maxDiscordFiles, + maxDiscordFileSize: config.maxDiscordFileSize, + accounts: config.accounts + } as ClientConfiguration) + ) + + return router +}