Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Deploy separately (Render). Not part of the Next.js app.
|--------|------|------|-------------|
| GET | `/health`, `/v1/health` | — | Liveness |
| GET | `/ready` | — | Database, Redis, and execution-mode readiness |
| POST | `/v1/keepalive` | Cron bearer secret | Wake Render and atomically toggle a dedicated Supabase maintenance row |
| GET/POST | `/v1/keepalive` | Cron bearer secret | Wake Render and atomically toggle a dedicated Supabase maintenance row |
| GET | `/v1/me` | Bearer | Profile + plan + usage (syncs Clerk → Supabase) |
| GET | `/v1/usage` | Bearer | Monthly usage snapshot |
| POST | `/v1/usage` | Bearer | Record usage (enforces limits) |
Expand Down
120 changes: 62 additions & 58 deletions api-gateway/src/routes/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,69 +18,73 @@ export const healthRoutes: FastifyPluginAsync = async (app) => {
ts: new Date().toISOString(),
}));

app.post("/v1/keepalive", async (request, reply) => {
reply
.header("Cache-Control", "no-store, max-age=0")
.header("Pragma", "no-cache")
.header("Expires", "0");
app.route({
method: ["GET", "POST"],
url: "/v1/keepalive",
handler: async (request, reply) => {
reply
.header("Cache-Control", "no-store, max-age=0")
.header("Pragma", "no-cache")
.header("Expires", "0");

if (!env.KEEPALIVE_CRON_SECRET) {
request.log.error("keepalive endpoint called without KEEPALIVE_CRON_SECRET");
return reply.status(503).send({
ok: false,
error: "Keepalive endpoint is not configured",
requestId: request.id,
});
}
if (!env.KEEPALIVE_CRON_SECRET) {
request.log.error("keepalive endpoint called without KEEPALIVE_CRON_SECRET");
return reply.status(503).send({
ok: false,
error: "Keepalive endpoint is not configured",
requestId: request.id,
});
}

if (
!hasValidKeepaliveAuthorization(
request.headers.authorization,
env.KEEPALIVE_CRON_SECRET,
)
) {
return reply.status(401).send({
ok: false,
error: "Unauthorized",
requestId: request.id,
});
}
if (
!hasValidKeepaliveAuthorization(
request.headers.authorization,
env.KEEPALIVE_CRON_SECRET,
)
) {
return reply.status(401).send({
ok: false,
error: "Unauthorized",
requestId: request.id,
});
}

const startedAt = Date.now();
const { data, error } = await getSupabase().rpc("toggle_keepalive_pulse");
if (error) {
request.log.error(
{ err: error, requestId: request.id },
"database keepalive failed",
);
return reply.status(503).send({
ok: false,
error: "Database keepalive failed",
requestId: request.id,
});
}
const startedAt = Date.now();
const { data, error } = await getSupabase().rpc("toggle_keepalive_pulse");
if (error) {
request.log.error(
{ err: error, requestId: request.id },
"database keepalive failed",
);
return reply.status(503).send({
ok: false,
error: "Database keepalive failed",
requestId: request.id,
});
}

const result = Array.isArray(data) ? data[0] : data;
const action = result?.action;
if (action !== "inserted" && action !== "deleted") {
request.log.error(
{ result, requestId: request.id },
"database keepalive returned an invalid result",
);
return reply.status(503).send({
ok: false,
error: "Database keepalive returned an invalid result",
requestId: request.id,
});
}
const result = Array.isArray(data) ? data[0] : data;
const action = result?.action;
if (action !== "inserted" && action !== "deleted") {
request.log.error(
{ result, requestId: request.id },
"database keepalive returned an invalid result",
);
return reply.status(503).send({
ok: false,
error: "Database keepalive returned an invalid result",
requestId: request.id,
});
}

return reply.send({
ok: true,
service: "api-gateway",
database: { action },
latencyMs: Date.now() - startedAt,
ts: new Date().toISOString(),
});
return reply.send({
ok: true,
service: "api-gateway",
database: { action },
latencyMs: Date.now() - startedAt,
ts: new Date().toISOString(),
});
},
});

app.get("/ready", async (_request, reply) => {
Expand Down
4 changes: 2 additions & 2 deletions docs/DEPLOYMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ cron-job.org job with these settings:
|-------|-------|
| URL | `https://api.rontgenai.dev/v1/keepalive` |
| Schedule | Every 13 minutes (`*/13 * * * *`) |
| Method | `POST` |
| Method | `GET` |
| Header | `Authorization: Bearer <KEEPALIVE_CRON_SECRET>` |

Use the same secret in cron-job.org and the Render web service, then redeploy
Expand All @@ -116,7 +116,7 @@ between `database.action: "inserted"` and `database.action: "deleted"`:
# Generate once, then save this value in both Render and cron-job.org.
openssl rand -hex 32

curl --fail --request POST \
curl --fail \
--header "Authorization: Bearer $KEEPALIVE_CRON_SECRET" \
https://api.rontgenai.dev/v1/keepalive
```
Expand Down