Files
omniai-server/src/routes/public.js
T

86 lines
3.1 KiB
JavaScript
Raw Normal View History

2026-06-02 13:14:10 +08:00
const { keyManager, listModelPrices, pool } = require("./context");
function registerPriceRoutes(router) {
// ── Public ───────────────────────────────────────────────────────────
router.get("/prices", async (_req, res) => {
const prices = await listModelPrices({ enabledOnly: true });
res.json(prices);
});
}
function registerPackageRoutes(router) {
// ── Public: Packages ─────────────────────────────────────────────────
router.get("/packages", async (_req, res) => {
const { rows } = await pool.query(
"SELECT * FROM packages WHERE enabled = 1 ORDER BY sort_order, id",
);
res.json(
rows.map((row) => ({
id: Number(row.id),
name: row.name,
description: row.description,
priceCents: row.price_cents,
creditsCents: row.credits_cents,
imageQuota: row.image_quota,
videoQuota: row.video_quota,
textQuota: row.text_quota,
durationDays: row.duration_days,
sortOrder: row.sort_order,
})),
);
});
}
function registerHealthRoutes(router) {
// ── Health ───────────────────────────────────────────────────────────
// Public health: minimal response, no provider/key details exposed
2026-06-02 13:14:10 +08:00
router.get("/health", async (_req, res) => {
res.json({ status: "ok", uptime: process.uptime() });
});
// Admin-only health: full provider status (requires auth via admin middleware)
router.get("/admin/providers/status", async (_req, res) => {
2026-06-02 13:14:10 +08:00
const status = await keyManager.getAllStatus();
res.json({ status: "ok", uptime: process.uptime(), providers: status });
});
}
const PUBLIC_CONFIG_PROFILES = new Set(["web-public-config", "web-model-capabilities"]);
function createPublicConfigFallback(name) {
if (name === "web-model-capabilities") {
return {
imageModels: [],
videoModels: [],
chatModels: [],
};
}
return {};
}
function registerPublicConfigRoutes(router) {
router.get("/public/config/profile", async (req, res) => {
const name = String(req.query.name || "web-public-config").trim() || "web-public-config";
if (!PUBLIC_CONFIG_PROFILES.has(name)) return res.status(404).json({ error: "Public config profile not found" });
const { rows: [row] } = await pool.query(
"SELECT config_json, description, updated_at FROM config_profiles WHERE name = $1",
[name],
);
if (!row) return res.json({ name, config: createPublicConfigFallback(name), description: "", updatedAt: null });
let config = {};
try { config = JSON.parse(row.config_json || "{}"); } catch {}
return res.json({ name, config, description: row.description || "", updatedAt: row.updated_at });
});
}
2026-06-02 13:14:10 +08:00
module.exports = {
registerPriceRoutes,
registerPackageRoutes,
registerPublicConfigRoutes,
2026-06-02 13:14:10 +08:00
registerHealthRoutes,
};