Added healhtRoute, honeypot, ConsentScript, asserEnv

This commit is contained in:
2026-10-10 00:17:13 +02:00
parent 52977e67fc
commit 1b52060dce
63 changed files with 2110 additions and 36 deletions
+32
View File
@@ -0,0 +1,32 @@
import type { BasePayload } from 'payload';
type CreateHealthRouteArgs = {
/**
* Collection to ping for a real DB round-trip. Default 'pages'. Pick any
* collection that always exists in the project.
*/
collection?: string;
/**
* Optional extra checks merged into the body (e.g. a cache or queue ping).
* Throwing marks the whole healthcheck unhealthy.
*/
extra?: () => Promise<Record<string, unknown>>;
/** Same cached accessor the rest of the app uses. */
getCachedPayload: () => Promise<BasePayload>;
};
/**
* A liveness/readiness route for Coolify/Docker healthchecks and uptime
* monitors. Does a real DB round-trip (not just "process is up") and returns
* 200 when reachable, 503 when not — so an orchestrator can restart or hold
* traffic off a container whose database is down.
*
* // app/api/health/route.ts
* import { createHealthRoute } from '@intecion/ipal-kit'
* import { getCachedPayload } from '@/lib/content'
* export const dynamic = 'force-dynamic' // never prerender/cache a healthcheck
* export const GET = createHealthRoute({ getCachedPayload })
*
* Point Coolify's healthcheck at /api/health. Uses the global `Response`
* (standard in Next route handlers) — no next/server import, peer-dep clean.
*/
export declare function createHealthRoute(args: CreateHealthRouteArgs): () => Promise<Response>;
export {};
+62
View File
@@ -0,0 +1,62 @@
/**
* A liveness/readiness route for Coolify/Docker healthchecks and uptime
* monitors. Does a real DB round-trip (not just "process is up") and returns
* 200 when reachable, 503 when not — so an orchestrator can restart or hold
* traffic off a container whose database is down.
*
* // app/api/health/route.ts
* import { createHealthRoute } from '@intecion/ipal-kit'
* import { getCachedPayload } from '@/lib/content'
* export const dynamic = 'force-dynamic' // never prerender/cache a healthcheck
* export const GET = createHealthRoute({ getCachedPayload })
*
* Point Coolify's healthcheck at /api/health. Uses the global `Response`
* (standard in Next route handlers) — no next/server import, peer-dep clean.
*/ export function createHealthRoute(args) {
const { collection = 'pages', extra, getCachedPayload } = args;
return async ()=>{
const startedAt = Date.now();
try {
const payload = await getCachedPayload();
// Cheapest real round-trip: ask for at most one row, no population.
await payload.find({
collection: collection,
depth: 0,
limit: 1,
pagination: false
});
const extraData = extra ? await extra() : {};
const body = {
db: 'up',
status: 'ok',
timestamp: new Date().toISOString(),
tookMs: Date.now() - startedAt,
...extraData
};
return new Response(JSON.stringify(body), {
headers: {
'cache-control': 'no-store',
'content-type': 'application/json'
},
status: 200
});
} catch (err) {
const body = {
db: 'down',
error: err instanceof Error ? err.message : 'unknown',
status: 'error',
timestamp: new Date().toISOString(),
tookMs: Date.now() - startedAt
};
return new Response(JSON.stringify(body), {
headers: {
'cache-control': 'no-store',
'content-type': 'application/json'
},
status: 503
});
}
};
}
//# sourceMappingURL=createHealthRoute.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/modules/health/createHealthRoute.ts"],"sourcesContent":["import type { BasePayload } from 'payload'\n\ntype CreateHealthRouteArgs = {\n /**\n * Collection to ping for a real DB round-trip. Default 'pages'. Pick any\n * collection that always exists in the project.\n */\n collection?: string\n /**\n * Optional extra checks merged into the body (e.g. a cache or queue ping).\n * Throwing marks the whole healthcheck unhealthy.\n */\n extra?: () => Promise<Record<string, unknown>>\n /** Same cached accessor the rest of the app uses. */\n getCachedPayload: () => Promise<BasePayload>\n}\n\n/**\n * A liveness/readiness route for Coolify/Docker healthchecks and uptime\n * monitors. Does a real DB round-trip (not just \"process is up\") and returns\n * 200 when reachable, 503 when not — so an orchestrator can restart or hold\n * traffic off a container whose database is down.\n *\n * // app/api/health/route.ts\n * import { createHealthRoute } from '@intecion/ipal-kit'\n * import { getCachedPayload } from '@/lib/content'\n * export const dynamic = 'force-dynamic' // never prerender/cache a healthcheck\n * export const GET = createHealthRoute({ getCachedPayload })\n *\n * Point Coolify's healthcheck at /api/health. Uses the global `Response`\n * (standard in Next route handlers) — no next/server import, peer-dep clean.\n */\nexport function createHealthRoute(args: CreateHealthRouteArgs): () => Promise<Response> {\n const { collection = 'pages', extra, getCachedPayload } = args\n\n return async () => {\n const startedAt = Date.now()\n try {\n const payload = await getCachedPayload()\n // Cheapest real round-trip: ask for at most one row, no population.\n await payload.find({\n collection: collection as never,\n depth: 0,\n limit: 1,\n pagination: false,\n })\n\n const extraData = extra ? await extra() : {}\n const body = {\n db: 'up',\n status: 'ok',\n timestamp: new Date().toISOString(),\n tookMs: Date.now() - startedAt,\n ...extraData,\n }\n return new Response(JSON.stringify(body), {\n headers: { 'cache-control': 'no-store', 'content-type': 'application/json' },\n status: 200,\n })\n } catch (err) {\n const body = {\n db: 'down',\n error: err instanceof Error ? err.message : 'unknown',\n status: 'error',\n timestamp: new Date().toISOString(),\n tookMs: Date.now() - startedAt,\n }\n return new Response(JSON.stringify(body), {\n headers: { 'cache-control': 'no-store', 'content-type': 'application/json' },\n status: 503,\n })\n }\n }\n}\n"],"names":["createHealthRoute","args","collection","extra","getCachedPayload","startedAt","Date","now","payload","find","depth","limit","pagination","extraData","body","db","status","timestamp","toISOString","tookMs","Response","JSON","stringify","headers","err","error","Error","message"],"mappings":"AAiBA;;;;;;;;;;;;;;CAcC,GACD,OAAO,SAASA,kBAAkBC,IAA2B;IAC3D,MAAM,EAAEC,aAAa,OAAO,EAAEC,KAAK,EAAEC,gBAAgB,EAAE,GAAGH;IAE1D,OAAO;QACL,MAAMI,YAAYC,KAAKC,GAAG;QAC1B,IAAI;YACF,MAAMC,UAAU,MAAMJ;YACtB,oEAAoE;YACpE,MAAMI,QAAQC,IAAI,CAAC;gBACjBP,YAAYA;gBACZQ,OAAO;gBACPC,OAAO;gBACPC,YAAY;YACd;YAEA,MAAMC,YAAYV,QAAQ,MAAMA,UAAU,CAAC;YAC3C,MAAMW,OAAO;gBACXC,IAAI;gBACJC,QAAQ;gBACRC,WAAW,IAAIX,OAAOY,WAAW;gBACjCC,QAAQb,KAAKC,GAAG,KAAKF;gBACrB,GAAGQ,SAAS;YACd;YACA,OAAO,IAAIO,SAASC,KAAKC,SAAS,CAACR,OAAO;gBACxCS,SAAS;oBAAE,iBAAiB;oBAAY,gBAAgB;gBAAmB;gBAC3EP,QAAQ;YACV;QACF,EAAE,OAAOQ,KAAK;YACZ,MAAMV,OAAO;gBACXC,IAAI;gBACJU,OAAOD,eAAeE,QAAQF,IAAIG,OAAO,GAAG;gBAC5CX,QAAQ;gBACRC,WAAW,IAAIX,OAAOY,WAAW;gBACjCC,QAAQb,KAAKC,GAAG,KAAKF;YACvB;YACA,OAAO,IAAIe,SAASC,KAAKC,SAAS,CAACR,OAAO;gBACxCS,SAAS;oBAAE,iBAAiB;oBAAY,gBAAgB;gBAAmB;gBAC3EP,QAAQ;YACV;QACF;IACF;AACF"}
+1
View File
@@ -0,0 +1 @@
export { createHealthRoute } from './createHealthRoute.js';
+3
View File
@@ -0,0 +1,3 @@
export { createHealthRoute } from './createHealthRoute.js';
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/modules/health/index.ts"],"sourcesContent":["export { createHealthRoute } from './createHealthRoute.js'\n"],"names":["createHealthRoute"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,yBAAwB"}