feat/api-prox: add logger

This commit is contained in:
Kentai Radiquum 2025-09-06 02:12:22 +05:00
parent cbfd24146d
commit 1a439f0a77
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
4 changed files with 41 additions and 9 deletions

View file

@ -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/",
];

View file

@ -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;

View file

@ -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}`
);
}
};

View file

@ -10,7 +10,12 @@ type Failure<E> = {
type Result<T, E = Error> = Success<T> | Failure<E>;
export async function tryCatch<T, E = Error>(
type TCError = {
message: string;
code: number;
}
export async function tryCatch<T, E = TCError>(
promise: Promise<T>
): Promise<Result<T, E>> {
try {
@ -21,15 +26,15 @@ export async function tryCatch<T, E = Error>(
}
}
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<T, E = Error>(
export async function tryCatchAPI<T>(
promise: Promise<any>
): Promise<Result<object | null, object | null>> {
): Promise<Result<T | null, TCError| null>> {
const { data, error }: Awaited<Result<Response | null, Error | null>> = 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") &&