Skip to content

Commit 295913e

Browse files
committed
add cron scheduling for leaderboard updates
1 parent 7dfb5ff commit 295913e

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/app/api/github/route.ts renamed to src/app/api/github/sync-leaderboard/route.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import { unauthorized } from "next/navigation";
12
import { NextResponse } from "next/server";
3+
import { env } from "~/env";
24
import syncLeaderboard from "~/server/github/syncLeaderboard";
35

4-
export async function GET() {
6+
export async function GET(request: Request) {
7+
if (request.headers.get("authorization") !== `Bearer ${env.CRON_SECRET}`) {
8+
unauthorized();
9+
}
10+
511
try {
612
await syncLeaderboard();
713
return NextResponse.json({ success: true });

src/env.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const env = createEnv({
1919
.transform((str) => "https://" + str)
2020
.pipe(z.url())
2121
: z.url().default("http://localhost:3000"),
22+
CRON_SECRET: z.string().min(32),
2223
DEVDOGS_EPOCH: z.coerce.date().default(new Date(2024, 7, 22)),
2324
DISCORD_CLIENT_ID: z.string(),
2425
DISCORD_CLIENT_SECRET: z.string(),
@@ -68,6 +69,7 @@ export const env = createEnv({
6869
(process.env.VERCEL_ENV === "production"
6970
? process.env.VERCEL_PROJECT_PRODUCTION_URL
7071
: process.env.VERCEL_URL),
72+
CRON_SECRET: process.env.CRON_SECRET,
7173
DEVDOGS_EPOCH: process.env.DEVDOGS_EPOCH,
7274
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID,
7375
GITHUB_CLIENT_SECRET: process.env.GITHUB_CLIENT_SECRET,

vercel.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,16 @@
22
"$schema": "https://openapi.vercel.sh/vercel.json",
33
"git": {
44
"deploymentEnabled": false
5+
},
6+
"crons": [
7+
{
8+
"path": "/api/github/sync-leaderboard",
9+
"schedule": "0 0 * * *"
10+
}
11+
],
12+
"functions": {
13+
"api/github/sync-leaderboard/route.ts": {
14+
"maxDuration": 30
15+
}
516
}
617
}

0 commit comments

Comments
 (0)