File tree 5 files changed +41
-7
lines changed
5 files changed +41
-7
lines changed Original file line number Diff line number Diff line change
1
+ import { NextRequest , NextResponse } from 'next/server' ;
2
+
3
+ export async function GET ( req : NextRequest , res : NextResponse ) {
4
+ return Response . json ( {
5
+ test : 'Hello from the edgxxxxe!' ,
6
+ } )
7
+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ declare global {
2
2
namespace NodeJS {
3
3
interface ProcessEnv {
4
4
D1 : D1Database
5
+ CRON_KEY : string
5
6
}
6
7
}
7
8
}
Original file line number Diff line number Diff line change
1
+ import { NextResponse , NextRequest } from 'next/server'
2
+
3
+ export function middleware ( request : NextRequest , response : NextResponse ) {
4
+ // Not using next.js specific APIS here so it can run anywhere
5
+ const url = new URL ( request . url ) ;
6
+ if ( url . pathname . startsWith ( '/api' ) ) {
7
+ // API Key can be passed via a header or query param
8
+ const apiKey = request . headers . get ( 'X-CRON-KEY' ) || url . searchParams . get ( 'CRON-KEY' ) ;
9
+ const secret = process . env . CRON_KEY ;
10
+ if ( apiKey !== secret ) {
11
+ return NextResponse . json ( { error : 'Unauthorized. Please send CRON-KEY header or query param with your request' } , { status : 401 } ) ;
12
+ }
13
+ }
14
+ return NextResponse . next ( )
15
+ }
16
+
17
+
18
+ export const config = {
19
+ matcher : '/api/:path*' ,
20
+ }
Original file line number Diff line number Diff line change 10
10
"pages:build" : " npx @cloudflare/next-on-pages" ,
11
11
"pages:preview" : " npm run pages:build && wrangler pages dev .vercel/output/static --compatibility-date=2024-01-17 --compatibility-flag=nodejs_compat" ,
12
12
"pages:deploy" : " npm run pages:build && wrangler pages deploy .vercel/output/static" ,
13
+ "worker:deploy" : " wrangler deploy" ,
13
14
"drizzle:generate" : " drizzle-kit generate:sqlite" ,
14
15
"drizzle:up" : " drizzle-kit up:sqlite" ,
15
16
"drizzle:studio" : " drizzle-kit studio" ,
Original file line number Diff line number Diff line change @@ -3,18 +3,23 @@ export default {
3
3
// Write code for updating your API
4
4
switch ( event . cron ) {
5
5
case "* * * * *" :
6
- // Every one minutes
7
- console . log ( `CRon: ${ event . cron } ` ) ;
6
+ console . log ( `Once a min: ${ event . cron } ` ) ;
8
7
break ;
9
- case "*/3 * * * *" :
10
- // Every ten minutes
11
- console . log ( `Running cron job with cron: ${ event . cron } ` ) ;
12
- // await updateAPI2();
8
+ case "*/3 * * * *" : {
9
+ console . log ( `Three times a min: ${ event . cron } ` ) ;
10
+ const result = await fetch ( "https://stats.syntax.fm/api/scrape/spotify" , {
11
+ headers : {
12
+ "X-CRON-KEY" : process . env . CRON_KEY
13
+ }
14
+ } ) ;
15
+ console . log ( await result . json ( ) ) ;
13
16
break ;
17
+ }
14
18
}
15
19
console . log ( "cron processed" ) ;
16
20
} ,
17
- fetch : async ( event , env , ctx ) => {
21
+ async fetch ( event , env , ctx ) {
18
22
return new Response ( "We dont need a fetch handler for cron jobs. But Hi!" ) ;
19
23
}
20
24
} satisfies ExportedHandler ;
25
+
You can’t perform that action at this time.
0 commit comments