Files
claudemesh/packages/api/src/index.ts
Alejandro Gutiérrez 509af3afe0
Some checks failed
CI / Lint (push) Has been cancelled
CI / Typecheck (push) Has been cancelled
CI / Broker tests (Postgres) (push) Has been cancelled
CI / Docker build (linux/amd64) (push) Has been cancelled
feat(web): public mesh stats counter + /api/public/stats endpoint
Live social-proof counter on the landing page, tied to the E2E
narrative. Formatted as understated mono footer, not hero brag.

Backend — new GET /api/public/stats (unauthed, 60s in-memory cache):
  {
    messagesRouted: SELECT COUNT(*) FROM mesh.message_queue,
    meshesCreated: SELECT COUNT(*) FROM mesh.mesh WHERE archivedAt IS NULL,
    peersActive: SELECT COUNT(*) FROM mesh.presence WHERE disconnectedAt IS NULL,
    lastUpdated: ISO timestamp,
  }

Aggregate counts only — no ids, no names, no ciphertext, no routing
metadata. Safe for public consumption. cache-control header sets
public/s-maxage=60 for edge caching. `x-cache: HIT|MISS` for debug.

Frontend — new MeshStats Server Component at
modules/marketing/home/mesh-stats.tsx. Reads the endpoint server-side
via the ~/lib/api/server client, renders monospace footer:

  ciphertext routed → 4,217 messages · 12 meshes · 8 peers online
  broker sees none of it

Graceful zero state: when messagesRouted === 0 shows
"ciphertext → ready to route" instead of embarrassing zeros. Tabular-
nums for the numeric spans so they don't jitter across renders.

Mounted between <CallToAction /> and <LatestNewsToaster />. Page-level
`export const revalidate = 60` so Next.js ISR refreshes the counter
every minute without a DB hit on every request (combined with the
API cache = two-layer 60s TTL, DB sees ~1 query/minute).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 16:00:00 +01:00

63 lines
2.0 KiB
TypeScript

import { Hono } from "hono";
import { some } from "hono/combine";
import { cors } from "hono/cors";
import { csrf } from "hono/csrf";
import { logger as loggerMiddleware } from "hono/logger";
import { auth } from "@turbostarter/auth/server";
import { logger } from "@turbostarter/shared/logger";
import { matchesPattern } from "@turbostarter/shared/utils";
import { localize, delay } from "./middleware";
import { adminRouter } from "./modules/admin/router";
// import { aiRouter } from "./modules/ai/router"; // disabled: @turbostarter/ai package removed in claudemesh
import { authRouter } from "./modules/auth/router";
import { billingRouter } from "./modules/billing/router";
import { myRouter } from "./modules/mesh/router";
import { organizationRouter } from "./modules/organization/router";
import { publicRouter } from "./modules/public/router";
import { storageRouter } from "./modules/storage/router";
import { onError } from "./utils/on-error";
import type { Context } from "hono";
const appRouter = new Hono()
.basePath("/api")
.use(
some(
(c: Context) => !!c.req.header("x-client-platform")?.startsWith("mobile"),
csrf({
origin: (origin, c) =>
[...auth.options.trustedOrigins, new URL(c.req.url).origin].some(
(trustedOrigin) => matchesPattern(origin, trustedOrigin),
),
}),
),
)
.use(
cors({
origin: "*",
allowHeaders: ["Content-Type", "Authorization"],
maxAge: 3600,
credentials: true,
}),
)
.use(loggerMiddleware((...args) => logger.info(...args)))
.use(delay)
.use(localize)
.get("/health", (c) => c.json({ status: "ok" }))
.route("/admin", adminRouter)
// .route("/ai", aiRouter) // disabled: @turbostarter/ai package removed in claudemesh
.route("/auth", authRouter)
.route("/billing", billingRouter)
.route("/my", myRouter)
.route("/organizations", organizationRouter)
.route("/public", publicRouter)
.route("/storage", storageRouter)
.onError(onError);
type AppRouter = typeof appRouter;
export type { AppRouter };
export { appRouter };