logicsrc/apps/logicsrc-web/src/proxy.ts
Anthony Ettinger 356a850cb2
Some checks failed
CI / build (push) Has been cancelled
Deploy to dev2 / deploy (push) Has been cancelled
test / test (push) Has been cancelled
ops: add public GET /api/health to logicsrc-web for status.profullstack.com (#233)
A filesystem route, so it wins over the CommandBoard /api/:path* rewrite
(unset in prod today, which left /api/health to the catch-all 404). One
head-only PostgREST query on blog_posts with a 3s cap: 200
{"status":"ok","db":"ok"} or 503 {"status":"error","db":"down"},
Cache-Control: no-store. The proxy skips the crawl gateway for the path.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 11:46:50 -07:00

30 lines
1.3 KiB
TypeScript

import { gate } from "@/lib/crawl-gateway";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// Canonical host: 301 www.* to the bare apex domain over https, preserving
// path + query (e.g. https://www.logicsrc.com/foo -> https://logicsrc.com/foo).
// This is the Next 16 "proxy" (formerly middleware) entrypoint.
const ALLOWED_APEX = process.env.PUBLIC_DOMAIN || "logicsrc.com";
export async function proxy(request: NextRequest): Promise<Response | NextResponse> {
// The status page's health probe skips the crawl gateway and host redirect.
if (request.nextUrl.pathname === "/api/health") return NextResponse.next();
// Crawl gateway first: AI training crawlers get 402 Payment Required (or the
// sales page at /crawl) unless they present a paid pass. People, Googlebot
// and retrieval crawlers fall through to everything below.
const answer = await gate(request);
if (answer) return answer;
const host = request.headers.get("host") ?? "";
if (host === `www.${ALLOWED_APEX}`) {
const { pathname, search } = request.nextUrl;
return NextResponse.redirect(`https://${ALLOWED_APEX}${pathname}${search}`, 301);
}
return NextResponse.next();
}
export const config = {
// Run on everything except Next's static assets and the favicon.
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"]
};