Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,136 @@
|
||||
import { Hono } from "hono";
|
||||
import { cors } from "hono/cors";
|
||||
import { isMetricsEnabled } from "@toolctrl/metrics";
|
||||
import { getApiAppDependencies, configureApiAppDependenciesForTests, type ApiAppDependencies } from "./app-dependencies.js";
|
||||
import type { ApiEnv } from "./api-types.js";
|
||||
import { postStripeBillingWebhookRequest } from "./billing/stripe-webhook-route.js";
|
||||
import { getAllowedCorsOrigin } from "./cors.js";
|
||||
import { apiLogger } from "./logger.js";
|
||||
import { getApiMetrics } from "./metrics.js";
|
||||
import { createBodyLimitMiddleware } from "./middleware/body-limit.js";
|
||||
import { createErrorHandler, createNotFoundHandler } from "./middleware/error-envelope.js";
|
||||
import { createMetricsMiddleware } from "./middleware/metrics.js";
|
||||
import { createRequestIdMiddleware } from "./middleware/request-id.js";
|
||||
import { createSentryTracingMiddleware } from "./middleware/sentryTracing.js";
|
||||
import { createAuditContextMiddleware } from "./middleware/audit-context.js";
|
||||
import { createApiMaintenanceMiddleware } from "./platform/maintenance/maintenance.js";
|
||||
import { apiErrorReporter } from "./sentry.js";
|
||||
import { registerAccountsRoutes } from "./routes/accounts-routes.js";
|
||||
import { registerAnalyticsRoutes } from "./routes/analytics-routes.js";
|
||||
import { registerAuthRoutes } from "./routes/auth-routes.js";
|
||||
import { registerDevicesRoutes } from "./routes/devices-routes.js";
|
||||
import { registerEntityRowRoutes } from "./routes/entity-row-routes.js";
|
||||
import { registerMaintenanceRoutes } from "./routes/maintenance-routes.js";
|
||||
import { handleApiMetricsRequest } from "./routes/metrics.js";
|
||||
import { registerNotificationRoutes } from "./routes/notifications-routes.js";
|
||||
import { registerRelationshipRoutes } from "./routes/relationship-routes.js";
|
||||
import { handleApiStatusRequest } from "./routes/status.js";
|
||||
import { registerSystemRoutes } from "./routes/system-routes.js";
|
||||
import { registerWorkspaceRoutes } from "./routes/workspace-routes.js";
|
||||
import { getUsageRequest } from "./usage/usage-route.js";
|
||||
|
||||
export { configureApiAppDependenciesForTests };
|
||||
export type { ApiAppDependencies };
|
||||
|
||||
export const app = new Hono<ApiEnv>();
|
||||
|
||||
// Sentry tracing MUST be the first middleware so every request starts a span
|
||||
// (including 404s + auth-rejected requests). Self-disables when SENTRY_DSN_API
|
||||
// is empty so local dev keeps zero Sentry overhead.
|
||||
app.use("*", createSentryTracingMiddleware());
|
||||
|
||||
app.use("*", createRequestIdMiddleware({ logger: apiLogger }));
|
||||
// Audit-capture context MUST come after requestId so deeper code paths
|
||||
// can attach the request-id to AuditEvent rows for cross-stack correlation.
|
||||
app.use("*", createAuditContextMiddleware());
|
||||
app.use("*", createBodyLimitMiddleware());
|
||||
app.use("*", createApiMaintenanceMiddleware());
|
||||
|
||||
// Prometheus per-request instrumentation. Self-noop when METRICS_ENABLED is
|
||||
// false so dev/test runs incur zero overhead. The shared registry from
|
||||
// `getApiMetrics()` is also used by the /metrics endpoint below.
|
||||
const apiMetricsEnabled = isMetricsEnabled(process.env);
|
||||
const apiMetricsBundle = getApiMetrics();
|
||||
app.use(
|
||||
"*",
|
||||
createMetricsMiddleware({
|
||||
enabled: apiMetricsEnabled,
|
||||
metrics: apiMetricsBundle.metrics,
|
||||
})
|
||||
);
|
||||
|
||||
app.get("/status", (c) =>
|
||||
handleApiStatusRequest(c, {
|
||||
checkDb: () => getApiAppDependencies().checkReadiness(),
|
||||
getRevision: () => process.env.APP_REVISION?.trim() ?? "",
|
||||
getRegion: () => process.env.APP_REGION?.trim() ?? process.env.FLY_REGION?.trim() ?? "",
|
||||
getUptimeSeconds: () => process.uptime(),
|
||||
})
|
||||
);
|
||||
|
||||
app.get("/metrics", (c) =>
|
||||
handleApiMetricsRequest(c, {
|
||||
enabled: apiMetricsEnabled,
|
||||
configuredToken: process.env.METRICS_TOKEN,
|
||||
registry: apiMetricsBundle.registry,
|
||||
})
|
||||
);
|
||||
|
||||
app.use("*", async (c, next) => {
|
||||
await next();
|
||||
c.header("x-content-type-options", "nosniff");
|
||||
c.header("referrer-policy", "no-referrer");
|
||||
c.header("x-frame-options", "DENY");
|
||||
c.header("permissions-policy", "camera=(), microphone=(), geolocation=(), payment=()");
|
||||
c.header("cross-origin-opener-policy", "same-origin");
|
||||
c.header("cross-origin-resource-policy", "same-origin");
|
||||
});
|
||||
|
||||
app.use(
|
||||
"*",
|
||||
cors({
|
||||
origin: getAllowedCorsOrigin,
|
||||
allowHeaders: [
|
||||
"authorization",
|
||||
"content-type",
|
||||
"idempotency-key",
|
||||
"last-event-id",
|
||||
"x-account-id",
|
||||
"x-analytics-token",
|
||||
"x-api-key",
|
||||
"x-filename",
|
||||
"x-request-id",
|
||||
"x-tenant-id",
|
||||
"x-toolctrl-tenant-id",
|
||||
"x-toolctrl-user-id",
|
||||
"x-storage-object-status",
|
||||
"webhook-id",
|
||||
"webhook-timestamp",
|
||||
"webhook-signature",
|
||||
"x-webhook-id",
|
||||
"x-webhook-timestamp",
|
||||
"x-webhook-signature",
|
||||
],
|
||||
allowMethods: ["GET", "POST", "PATCH", "DELETE", "OPTIONS"],
|
||||
credentials: true,
|
||||
})
|
||||
);
|
||||
|
||||
registerSystemRoutes(app);
|
||||
registerAuthRoutes(app);
|
||||
registerAccountsRoutes(app);
|
||||
registerAnalyticsRoutes(app);
|
||||
registerNotificationRoutes(app);
|
||||
registerWorkspaceRoutes(app);
|
||||
registerMaintenanceRoutes(app);
|
||||
registerEntityRowRoutes(app);
|
||||
registerRelationshipRoutes(app);
|
||||
registerDevicesRoutes(app);
|
||||
|
||||
app.get("/v1/usage", (c) => getUsageRequest(c, getApiAppDependencies()));
|
||||
app.post("/v1/billing/stripe/webhooks", (c) => postStripeBillingWebhookRequest(c, getApiAppDependencies()));
|
||||
|
||||
app.notFound(createNotFoundHandler());
|
||||
app.onError(createErrorHandler({ reporter: apiErrorReporter }));
|
||||
|
||||
export type ApiApp = typeof app;
|
||||
Reference in new issue
Block a user