mirror of
https://github.com/Radiquum/AniX.git
synced 2025-09-08 07:23:54 +05:00
feat/api-prox: add logger
This commit is contained in:
parent
cbfd24146d
commit
1a439f0a77
4 changed files with 41 additions and 9 deletions
|
@ -1,3 +1,8 @@
|
||||||
// Epoch Semantic Versioning - https://antfu.me/posts/epoch-semver
|
// Epoch Semantic Versioning - https://antfu.me/posts/epoch-semver
|
||||||
// {EPOCH * 1000 + MAJOR}.MINOR.PATCH
|
// {EPOCH * 1000 + MAJOR}.MINOR.PATCH
|
||||||
export const appVersion = "0000.0.0";
|
export const appVersion = "0000.0.0";
|
||||||
|
|
||||||
|
export const baseUrls = [
|
||||||
|
"https://api-s.anixsekai.com/",
|
||||||
|
"https://api.anixsekai.com/",
|
||||||
|
];
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
import { Hono } from "hono";
|
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 { asciiHTML, separatorHTML } from "./utils/info.js";
|
||||||
import { appVersion } from "./config.js";
|
import { appVersion } from "./config.js";
|
||||||
|
|
||||||
const app = new Hono({ strict: true });
|
const app = new Hono({ strict: true });
|
||||||
app.use(trimTrailingSlash())
|
app.use(trimTrailingSlash());
|
||||||
|
app.use(logger(RouteLogger));
|
||||||
|
|
||||||
app.get("/", (c) => {
|
app.get("/", (c) => {
|
||||||
return c.html(`
|
return c.html(`
|
||||||
|
@ -56,7 +59,7 @@ app.get("/health", (c) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/health/json", (c) => {
|
app.get("/health/json", (c) => {
|
||||||
return c.json({"status": "OK", "version": appVersion});
|
return c.json({ status: "OK", version: appVersion });
|
||||||
});
|
});
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
|
19
api-prox/src/utils/logger.ts
Normal file
19
api-prox/src/utils/logger.ts
Normal 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}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
|
@ -10,7 +10,12 @@ type Failure<E> = {
|
||||||
|
|
||||||
type Result<T, E = Error> = Success<T> | 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: Promise<T>
|
||||||
): Promise<Result<T, E>> {
|
): Promise<Result<T, E>> {
|
||||||
try {
|
try {
|
||||||
|
@ -21,15 +26,15 @@ export async function tryCatch<T, E = Error>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateError(message: string, code: number) {
|
function generateError(message: string, code: number): TCError {
|
||||||
return {message: message, code: code}
|
return { message: message, code: code }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function tryCatchAPI<T, E = Error>(
|
export async function tryCatchAPI<T>(
|
||||||
promise: Promise<any>
|
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);
|
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 (
|
if (
|
||||||
data.headers.get("content-length") &&
|
data.headers.get("content-length") &&
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue