diff --git a/api-prox/src/config.ts b/api-prox/src/config.ts index 29422ee..0cac298 100644 --- a/api-prox/src/config.ts +++ b/api-prox/src/config.ts @@ -1,3 +1,8 @@ // Epoch Semantic Versioning - https://antfu.me/posts/epoch-semver // {EPOCH * 1000 + MAJOR}.MINOR.PATCH export const appVersion = "0000.0.0"; + +export const baseUrls = [ + "https://api-s.anixsekai.com/", + "https://api.anixsekai.com/", +]; diff --git a/api-prox/src/index.ts b/api-prox/src/index.ts index 0bd6486..244cdc2 100644 --- a/api-prox/src/index.ts +++ b/api-prox/src/index.ts @@ -1,10 +1,13 @@ import { Hono } from "hono"; -import { trimTrailingSlash } from 'hono/trailing-slash' +import { logger } from "hono/logger"; +import { RouteLogger } from "./utils/logger.js"; +import { trimTrailingSlash } from "hono/trailing-slash"; import { asciiHTML, separatorHTML } from "./utils/info.js"; import { appVersion } from "./config.js"; const app = new Hono({ strict: true }); -app.use(trimTrailingSlash()) +app.use(trimTrailingSlash()); +app.use(logger(RouteLogger)); app.get("/", (c) => { return c.html(` @@ -56,7 +59,7 @@ app.get("/health", (c) => { }); app.get("/health/json", (c) => { - return c.json({"status": "OK", "version": appVersion}); + return c.json({ status: "OK", version: appVersion }); }); export default app; diff --git a/api-prox/src/utils/logger.ts b/api-prox/src/utils/logger.ts new file mode 100644 index 0000000..903f2ae --- /dev/null +++ b/api-prox/src/utils/logger.ts @@ -0,0 +1,19 @@ +export const RouteLogger = (message: string) => { + const args = message.split(" "); + const direction = args[0]; + const method = args[1]; + const url = new URL("http://example.com" + args[2]); + if (url.searchParams.get("token")) { + url.searchParams.set("token", "*********"); + } + + if (direction == "<--") { + console.log(`REQ | ${method} ${url.pathname}${url.search}`); + } else { + const status = args[3]; + const time = args[4]; + console.log( + `RES | ${method} ${url.pathname}${url.search} ${status} ${time}` + ); + } +}; diff --git a/api-prox/src/utils/tryCatch.ts b/api-prox/src/utils/tryCatch.ts index 7a81358..d255906 100644 --- a/api-prox/src/utils/tryCatch.ts +++ b/api-prox/src/utils/tryCatch.ts @@ -10,7 +10,12 @@ type Failure = { type Result = Success | Failure; -export async function tryCatch( +type TCError = { + message: string; + code: number; +} + +export async function tryCatch( promise: Promise ): Promise> { try { @@ -21,15 +26,15 @@ export async function tryCatch( } } -function generateError(message: string, code: number) { - return {message: message, code: code} +function generateError(message: string, code: number): TCError { + return { message: message, code: code } } -export async function tryCatchAPI( +export async function tryCatchAPI( promise: Promise -): Promise> { +): Promise> { const { data, error }: Awaited> = await tryCatch(promise); - if (!data || error) return { data: null, error: error }; + if (!data || error) return { data: null, error: generateError("No data returned", 500) }; if ( data.headers.get("content-length") &&