Skip to content

Commit

Permalink
middlware cron key
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbos committed Feb 29, 2024
1 parent d2efe4c commit fa2b58f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
7 changes: 7 additions & 0 deletions app/api/scrape/test/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NextRequest, NextResponse } from 'next/server';

export async function GET(req: NextRequest, res: NextResponse) {
return Response.json({
test: 'Hello from the edgxxxxe!',
})
}
1 change: 1 addition & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ declare global {
namespace NodeJS {
interface ProcessEnv {
D1: D1Database
CRON_KEY: string
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NextResponse, NextRequest } from 'next/server'

export function middleware(request: NextRequest, response: NextResponse) {
// Not using next.js specific APIS here so it can run anywhere
const url = new URL(request.url);
if (url.pathname.startsWith('/api')) {
// API Key can be passed via a header or query param
const apiKey = request.headers.get('X-CRON-KEY') || url.searchParams.get('CRON-KEY');
const secret = process.env.CRON_KEY;
if (apiKey !== secret) {
return NextResponse.json({ error: 'Unauthorized. Please send CRON-KEY header or query param with your request' }, { status: 401 });
}
}
return NextResponse.next()
}


export const config = {
matcher: '/api/:path*',
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"pages:build": "npx @cloudflare/next-on-pages",
"pages:preview": "npm run pages:build && wrangler pages dev .vercel/output/static --compatibility-date=2024-01-17 --compatibility-flag=nodejs_compat",
"pages:deploy": "npm run pages:build && wrangler pages deploy .vercel/output/static",
"worker:deploy": "wrangler deploy",
"drizzle:generate": "drizzle-kit generate:sqlite",
"drizzle:up": "drizzle-kit up:sqlite",
"drizzle:studio": "drizzle-kit studio",
Expand Down
19 changes: 12 additions & 7 deletions src/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ export default {
// Write code for updating your API
switch (event.cron) {
case "* * * * *":
// Every one minutes
console.log(`CRon: ${event.cron}`);
console.log(`Once a min: ${event.cron}`);
break;
case "*/3 * * * *":
// Every ten minutes
console.log(`Running cron job with cron: ${event.cron}`);
// await updateAPI2();
case "*/3 * * * *": {
console.log(`Three times a min: ${event.cron}`);
const result = await fetch("https://stats.syntax.fm/api/scrape/spotify", {
headers: {
"X-CRON-KEY": process.env.CRON_KEY
}
});
console.log(await result.json());
break;
}
}
console.log("cron processed");
},
fetch: async (event, env, ctx) => {
async fetch(event, env, ctx) {
return new Response("We dont need a fetch handler for cron jobs. But Hi!");
}
} satisfies ExportedHandler;

0 comments on commit fa2b58f

Please sign in to comment.