refactor(webhook): load route map from env
Build and Deploy / build-and-deploy (push) Successful in 5m5s

This commit is contained in:
2026-07-06 16:50:55 +00:00 Unverified
parent 9a936c9133
commit b2f816f604
3 changed files with 29 additions and 9 deletions
+2
View File
@@ -32,6 +32,8 @@ spec:
envFrom:
- secretRef:
name: enka-env
- configMapRef:
name: enka-route-map
livenessProbe:
httpGet:
path: /health
-1
View File
@@ -7,4 +7,3 @@ type: Opaque
stringData:
PRIVATE_API_KEY: "key is not needed"
PUBLIC_API_KEY: "key is not needed"
WH_ROUTE_MAP: "https://cdn.astrxl.dev/sec/a2f43a3d249a-routes.json"
+27 -8
View File
@@ -1,11 +1,9 @@
import type { App } from "@";
import { env } from "bun";
import { status, t } from "elysia";
import { TTLCache } from "enka.ts";
import { createLogger } from "@/lib/logger";
const routesCache = new TTLCache();
type Pending = {
data: unknown;
timer: ReturnType<typeof setTimeout>;
@@ -15,6 +13,7 @@ type Pending = {
const pending = new Map<string, Pending>();
const log = createLogger("WH");
const routes = parseRouteMap(env.WH_ROUTE_MAP);
export function webhookMerger(app: App) {
return app.post(
@@ -72,15 +71,35 @@ export function webhookMerger(app: App) {
type RouteMap = [string, string, number][];
async function getRoutes(): Promise<RouteMap> {
const cached = routesCache.get(".");
return routes;
}
if (cached) return cached as RouteMap;
function parseRouteMap(raw: string): RouteMap {
const parsed = JSON.parse(raw) as unknown;
const res = await fetch(env.WH_ROUTE_MAP).then((r) => r.ok && r.json());
if (!Array.isArray(parsed)) throw new Error("WH_ROUTE_MAP must be a JSON array");
if (!res) throw new Error("cant get route map");
routesCache.set(".", res, 900);
return res;
for (const [index, route] of parsed.entries()) {
if (!Array.isArray(route) || route.length !== 3) {
throw new Error(`WH_ROUTE_MAP route ${index} must be [id, url, bufferTime]`);
}
const [id, url, bufferTime] = route;
if (typeof id !== "string" || !id) {
throw new Error(`WH_ROUTE_MAP route ${index} has invalid id`);
}
if (typeof url !== "string" || !url) {
throw new Error(`WH_ROUTE_MAP route ${index} has invalid url`);
}
if (!Number.isFinite(bufferTime)) {
throw new Error(`WH_ROUTE_MAP route ${index} has invalid bufferTime`);
}
}
return parsed as RouteMap;
}
const MAX_FIELD_LENGTH = Number(env.MAX_FIELD_LENGTH) || 4000;