-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
41 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!', | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ declare global { | |
namespace NodeJS { | ||
interface ProcessEnv { | ||
D1: D1Database | ||
CRON_KEY: string | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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*', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters