From ec938960cb305024ebe299849766f3e52a8063cd Mon Sep 17 00:00:00 2001 From: Radiquum Date: Fri, 5 Sep 2025 22:33:40 +0500 Subject: [PATCH] feat/api-prox: add index and health pages --- api-prox/src/config.json | 1 + api-prox/src/index.ts | 63 ++++++++++++++++++++++++++++++---- api-prox/src/utils/info.ts | 27 +++++++++++++++ api-prox/src/utils/tryCatch.ts | 61 ++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 6 deletions(-) create mode 100644 api-prox/src/config.json create mode 100644 api-prox/src/utils/info.ts create mode 100644 api-prox/src/utils/tryCatch.ts diff --git a/api-prox/src/config.json b/api-prox/src/config.json new file mode 100644 index 0000000..9904ca9 --- /dev/null +++ b/api-prox/src/config.json @@ -0,0 +1 @@ +{"appVersion": "0.0.1"} \ No newline at end of file diff --git a/api-prox/src/index.ts b/api-prox/src/index.ts index 3191383..d580133 100644 --- a/api-prox/src/index.ts +++ b/api-prox/src/index.ts @@ -1,9 +1,60 @@ -import { Hono } from 'hono' +import { Hono } from "hono"; +import { asciiHTML, separatorHTML } from "./utils/info.js"; +import config from "./config.json" with { type: "json" }; -const app = new Hono() +const app = new Hono(); -app.get('/', (c) => { - return c.text('Hello Hono!') -}) +app.get("/", (c) => { + return c.html(` + + + + + + + ${asciiHTML()} + ${separatorHTML()} +

To get started, modify your apk to use unknown[endpoint] or deploy the AniX web client

+ ${separatorHTML()} + + + +`); +}); -export default app +app.get("/health", (c) => { + return c.html(` + + + + + + ${asciiHTML()} + ${separatorHTML()} +

Status: OK

+

Version: ${config.appVersion}

+ + +`); +}); + +app.get("/health/json", (c) => { + return c.json({"status": "OK", "version": config.appVersion}); +}); + +export default app; diff --git a/api-prox/src/utils/info.ts b/api-prox/src/utils/info.ts new file mode 100644 index 0000000..603108a --- /dev/null +++ b/api-prox/src/utils/info.ts @@ -0,0 +1,27 @@ +// ___ _ __ ___ ____ ____ ____ +// / | ____ (_) ______ ______/ /_ / | / __ \/ _/ / __ \_________ _ ____ __ +// / /| | / __ \/ / |/_/ __ `/ ___/ __/ / /| | / /_/ // / / /_/ / ___/ __ \| |/_/ / / / +// / ___ |/ / / / /> `); + stringBuilder.push(" ___ _ __ ___ ____ ____ ____ ") + stringBuilder.push(" / | ____ (_) ______ ______/ /_ / | / __ \\/ _/ / __ \\_________ _ ____ __") + stringBuilder.push(" / /| | / __ \\/ / |/_/ __ `/ ___/ __/ / /| | / /_/ // / / /_/ / ___/ __ \\| |/_/ / / /") + stringBuilder.push(" / ___ |/ / / / /> </ /_/ / / / /_ / ___ |/ ____// / / ____/ / / /_/ /> </ /_/ /") + stringBuilder.push("/_/ |_/_/ /_/_/_/|_|\\__,_/_/ \\__/ /_/ |_/_/ /___/ /_/ /_/ \\____/_/|_|\\__, /") + stringBuilder.push(" /____/") + stringBuilder.push(``); + return stringBuilder.join("\n"); +} + +export function separatorHTML() { + const stringBuilder = []; + stringBuilder.push(`
`);
+    stringBuilder.push("-".repeat(92))
+    stringBuilder.push(`
`); + return stringBuilder.join("\n"); +} \ No newline at end of file diff --git a/api-prox/src/utils/tryCatch.ts b/api-prox/src/utils/tryCatch.ts new file mode 100644 index 0000000..7a81358 --- /dev/null +++ b/api-prox/src/utils/tryCatch.ts @@ -0,0 +1,61 @@ +type Success = { + data: T; + error: null; +}; + +type Failure = { + data: null; + error: E; +}; + +type Result = Success | Failure; + +export async function tryCatch( + promise: Promise +): Promise> { + try { + const data = await promise; + return { data, error: null }; + } catch (error) { + return { data: null, error: error as E }; + } +} + +function generateError(message: string, code: number) { + return {message: message, code: code} +} + +export async function tryCatchAPI( + promise: Promise +): Promise> { + const { data, error }: Awaited> = await tryCatch(promise); + if (!data || error) return { data: null, error: error }; + + if ( + data.headers.get("content-length") && + Number(data.headers.get("content-length")) == 0 + ) { + return { + data: null, + error: generateError("Not Found", 404), + }; + } + + try { + const body: Awaited = await data.json(); + if (body.code != 0) { + return { + data: null, + error: generateError("Anixart API Error", body.code), + }; + } + return { + data: body, + error: null, + }; + } catch { + return { data: null, error: generateError("failed to parse json", 500) } + } + + return { data: null, error: generateError("tryCatch.ts: unreachable", 500) } +} \ No newline at end of file