Adds /api/health, ntfy notify on config save, /admin/analytics charts, and deploy smoke for miniprogram config/API. Co-authored-by: Cursor <cursoragent@cursor.com>
118 lines
3.4 KiB
TypeScript
118 lines
3.4 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { requireAdminSession } from "~/lib/admin-auth";
|
|
import { createNomadroClient, PORTAL_ROUTES, SDK_VERSION } from "@nomadro/sdk";
|
|
import { resolvePocketBaseConfig } from "@nomadro/pb";
|
|
import { resolvePayProvider } from "@nomadro/pay";
|
|
import { readRemoteConfigFile } from "~/lib/remote-config-store";
|
|
import { ntfyConfigured } from "~/lib/ntfy";
|
|
|
|
async function probe(url: string, timeoutMs = 4000) {
|
|
const started = Date.now();
|
|
try {
|
|
const ctrl = new AbortController();
|
|
const t = setTimeout(() => ctrl.abort(), timeoutMs);
|
|
const res = await fetch(url, { signal: ctrl.signal, cache: "no-store" });
|
|
clearTimeout(t);
|
|
return { ok: res.ok, status: res.status, latencyMs: Date.now() - started, url };
|
|
} catch {
|
|
return { ok: false, status: 0, latencyMs: Date.now() - started, url };
|
|
}
|
|
}
|
|
|
|
export async function GET() {
|
|
if (!(await requireAdminSession())) {
|
|
return NextResponse.json({ ok: false, error: "unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const client = createNomadroClient();
|
|
const pb = resolvePocketBaseConfig();
|
|
const pay = resolvePayProvider({ region: "auto", isChinaUser: true });
|
|
const origin =
|
|
process.env.NOMADRO_PUBLIC_ORIGIN?.replace(/\/$/, "") || "https://nomadro.cn";
|
|
|
|
const [
|
|
apiHealth,
|
|
pbHealth,
|
|
umami,
|
|
uptime,
|
|
ntfy,
|
|
portalHealth,
|
|
configProbe,
|
|
bookRead,
|
|
vipRead,
|
|
remoteConfig,
|
|
] = await Promise.all([
|
|
client.health(),
|
|
probe(`${pb.publicUrl}/api/health`),
|
|
probe("https://umami.nomadro.cn"),
|
|
probe("https://uptimekuma.nomadro.cn"),
|
|
probe("https://ntfy.nomadro.cn/v1/health"),
|
|
probe(`${origin}/api/health`),
|
|
probe(`${origin}/api/config`),
|
|
probe(`${origin}/book/read`),
|
|
probe(`${origin}/vip/ebook/read`),
|
|
readRemoteConfigFile(),
|
|
]);
|
|
|
|
const modules = Object.entries(remoteConfig.modules).map(([id, m]) => ({
|
|
id,
|
|
title: m.title,
|
|
enabled: m.enabled !== false,
|
|
}));
|
|
const enabledCount = modules.filter((m) => m.enabled).length;
|
|
|
|
const health = {
|
|
payjsapi: apiHealth,
|
|
pocketbase: pbHealth,
|
|
umami,
|
|
uptimekuma: uptime,
|
|
ntfy,
|
|
portalHealth,
|
|
config: configProbe,
|
|
bookRead,
|
|
vipEbookRead: vipRead,
|
|
};
|
|
|
|
const latencies = Object.entries(health).map(([key, h]) => ({
|
|
key,
|
|
ok: Boolean((h as { ok?: boolean }).ok),
|
|
latencyMs: Number((h as { latencyMs?: number }).latencyMs ?? 0),
|
|
status: Number((h as { status?: number }).status ?? 0),
|
|
}));
|
|
|
|
return NextResponse.json({
|
|
ok: true,
|
|
generatedAt: new Date().toISOString(),
|
|
sdk: SDK_VERSION,
|
|
routes: PORTAL_ROUTES,
|
|
payHint: pay,
|
|
ntfy: { configured: ntfyConfigured() },
|
|
pb: { publicUrl: pb.publicUrl },
|
|
health,
|
|
charts: {
|
|
latencies,
|
|
modules: {
|
|
total: modules.length,
|
|
enabled: enabledCount,
|
|
disabled: modules.length - enabledCount,
|
|
items: modules,
|
|
},
|
|
flags: remoteConfig.flags,
|
|
copy: {
|
|
vipPriceFen: remoteConfig.copy.vipPriceFen,
|
|
homeBrand: remoteConfig.copy.homeBrand,
|
|
},
|
|
},
|
|
anchors: {
|
|
health: "/api/health",
|
|
config: "/api/config",
|
|
bookPreface: "/book/read#00-preface",
|
|
vipPreface: "/vip/ebook/read#00-preface",
|
|
umami: "https://umami.nomadro.cn",
|
|
uptimekuma: "https://uptimekuma.nomadro.cn",
|
|
ntfy: "https://ntfy.nomadro.cn",
|
|
s3: "https://s3.nomadro.cn",
|
|
},
|
|
});
|
|
}
|