- Backups: a guest whose newest backup is older than its limit is an alert (backup kind), in the daily backup message, the digest and /metrics (homelab_backup_guests, ..._last_age_seconds) - Per-server thresholds (servers.thresholds, migration 0030): offline minutes, disk percent, forecast window, backup age, and per-disk limits or "leave alone"; applied everywhere the global ones were; saving clears the alerts cache - Update runs (update_runs, migration 0031): Update... on the Updates page puts the server under maintenance, runs a Semaphore playbook limited to it, waits for the host to report, and reports what is left and whether a restart is due; poller survives restarts - Notification toggle for update-run results; tests and docs Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
141 lines
6.1 KiB
TypeScript
141 lines
6.1 KiB
TypeScript
import express from "express";
|
|
import session from "express-session";
|
|
import FileStoreFactory from "session-file-store";
|
|
import { fileURLToPath } from "node:url";
|
|
import { dirname, join } from "node:path";
|
|
import { mkdirSync, existsSync } from "node:fs";
|
|
import { env } from "./env.js";
|
|
import { resolveDataPath } from "./paths.js";
|
|
import { authRouter } from "./auth/router.js";
|
|
import { meRouter } from "./routes/me.js";
|
|
import { uptimeRouter } from "./routes/uptime.js";
|
|
import { shareLinksRouter } from "./routes/shareLinks.js";
|
|
import { runbooksRouter } from "./routes/runbooks.js";
|
|
import { updateRunsRouter } from "./routes/updateRuns.js";
|
|
import { inventoryRouter } from "./routes/inventory.js";
|
|
import { backupsRouter } from "./routes/backups.js";
|
|
import { metricsRouter, metricsTokensRouter } from "./routes/metrics.js";
|
|
import { eventWebhooksRouter } from "./routes/eventWebhooks.js";
|
|
import { publicWallRouter } from "./routes/publicWall.js";
|
|
import { usersRouter } from "./routes/users.js";
|
|
import { auditLogRouter } from "./routes/auditLog.js";
|
|
import { diagLogRouter } from "./routes/diagLog.js";
|
|
import { secretsRouter } from "./routes/secrets.js";
|
|
import { ipamRouter } from "./routes/ipam.js";
|
|
import { dnsRouter } from "./routes/dns.js";
|
|
import { serversRouter } from "./routes/servers.js";
|
|
import { tasksRouter } from "./routes/tasks.js";
|
|
import { agentReportRouter } from "./routes/agentReport.js";
|
|
import { integrationsRouter } from "./routes/integrations.js";
|
|
import { settingsRouter } from "./routes/settings.js";
|
|
import { searchRouter } from "./routes/search.js";
|
|
import { sessionsRouter } from "./routes/sessions.js";
|
|
import { maintenanceRouter } from "./routes/maintenance.js";
|
|
import { domainsRouter } from "./routes/domains.js";
|
|
import { consistencyRouter } from "./routes/consistency.js";
|
|
import { privacyRouter } from "./routes/privacy.js";
|
|
import { tagsRouter } from "./routes/tags.js";
|
|
import { portsRouter } from "./routes/ports.js";
|
|
import { alertsRouter } from "./routes/alerts.js";
|
|
import { mapRouter } from "./routes/map.js";
|
|
import { generatorRouter } from "./routes/generator.js";
|
|
import { alertTicketsRouter } from "./routes/alertTickets.js";
|
|
import { apiErrorHandler } from "./utils/apiErrorHandler.js";
|
|
import { securityHeaders } from "./utils/securityHeaders.js";
|
|
import { sameOriginGuard } from "./utils/sameOriginGuard.js";
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const webDist = join(__dirname, "..", "..", "web", "dist");
|
|
const agentDir = join(__dirname, "..", "..", "agent");
|
|
|
|
/**
|
|
* The whole web application, ready to listen. It lives apart from index.ts (which also runs migrations and starts the
|
|
* schedulers) so the tests can run the real thing — the order of the middleware below is part of what they check.
|
|
*/
|
|
export function createApp() {
|
|
const FileStore = FileStoreFactory(session);
|
|
const sessionDir = resolveDataPath(env.sessionDir);
|
|
mkdirSync(sessionDir, { recursive: true });
|
|
|
|
const app = express();
|
|
app.disable("x-powered-by");
|
|
app.set("trust proxy", 1);
|
|
app.use(securityHeaders);
|
|
// The agent endpoint comes first: it reads its own (larger) body, only after checking the token, and an agent has no use for a
|
|
// session. It is also the one place a request isn't from a browser, so it isn't subject to the same-origin check below.
|
|
app.use("/api/agent/report", agentReportRouter);
|
|
// Everything after this is reached with a session cookie, so a request that came from another origin is refused here.
|
|
app.use(sameOriginGuard);
|
|
app.use(express.json());
|
|
app.use(
|
|
session({
|
|
store: new FileStore({ path: sessionDir, logFn: () => {} }),
|
|
secret: env.sessionSecret,
|
|
resave: false,
|
|
saveUninitialized: false,
|
|
cookie: {
|
|
httpOnly: true,
|
|
sameSite: "lax",
|
|
secure: env.nodeEnv === "production",
|
|
maxAge: 7 * 24 * 60 * 60 * 1000,
|
|
},
|
|
}),
|
|
);
|
|
|
|
app.get("/health", (_req, res) => res.json({ ok: true }));
|
|
|
|
// What this installation calls itself and how it should look (a dev copy in a different colour), asked for before sign-in so the
|
|
// login page can show it too. Just a label and a colour.
|
|
app.get("/api/environment", (_req, res) => res.json({ environment: env.environment }));
|
|
|
|
// Publicly readable so `curl .../agent/linux/install.sh | bash` works from a
|
|
// freshly provisioned server with no prior session. Contains no secrets.
|
|
if (existsSync(agentDir)) {
|
|
app.use("/agent", express.static(agentDir));
|
|
}
|
|
|
|
app.use("/auth", authRouter);
|
|
app.use("/api/me", meRouter);
|
|
app.use("/api/uptime", uptimeRouter);
|
|
app.use("/api/share-links", shareLinksRouter);
|
|
app.use("/api/runbooks", runbooksRouter);
|
|
app.use("/api/update-runs", updateRunsRouter);
|
|
app.use("/api/inventory", inventoryRouter);
|
|
app.use("/api/backups", backupsRouter);
|
|
app.use("/api/metrics-tokens", metricsTokensRouter);
|
|
app.use("/api/event-webhooks", eventWebhooksRouter);
|
|
// Prometheus scrapes this with a bearer token, not a session.
|
|
app.use("/metrics", metricsRouter);
|
|
app.use("/api/public/wall", publicWallRouter);
|
|
app.use("/api/users", usersRouter);
|
|
app.use("/api/audit-log", auditLogRouter);
|
|
app.use("/api/diag-log", diagLogRouter);
|
|
app.use("/api/secrets", secretsRouter);
|
|
app.use("/api/ipam", ipamRouter);
|
|
app.use("/api/dns", dnsRouter);
|
|
app.use("/api/servers", serversRouter);
|
|
app.use("/api/tasks", tasksRouter);
|
|
app.use("/api/integrations", integrationsRouter);
|
|
app.use("/api/settings", settingsRouter);
|
|
app.use("/api/search", searchRouter);
|
|
app.use("/api/sessions", sessionsRouter);
|
|
app.use("/api/maintenance", maintenanceRouter);
|
|
app.use("/api/domains", domainsRouter);
|
|
app.use("/api/consistency", consistencyRouter);
|
|
app.use("/api/privacy", privacyRouter);
|
|
app.use("/api/tags", tagsRouter);
|
|
app.use("/api/ports", portsRouter);
|
|
app.use("/api/alerts", alertsRouter);
|
|
app.use("/api/map", mapRouter);
|
|
app.use("/api/generator", generatorRouter);
|
|
app.use("/api/alert-tickets", alertTicketsRouter);
|
|
|
|
if (existsSync(webDist)) {
|
|
app.use(express.static(webDist));
|
|
app.get("*", (_req, res) => res.sendFile(join(webDist, "index.html")));
|
|
}
|
|
|
|
app.use(apiErrorHandler);
|
|
return app;
|
|
}
|