定时任务增加鉴权
This commit is contained in:
@@ -11,8 +11,20 @@ import { SearchResult } from '@/lib/types';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { password: string } }
|
||||
) {
|
||||
console.log(request.url);
|
||||
|
||||
const cronPassword = process.env.CRON_PASSWORD || 'mtvpls';
|
||||
if (params.password !== cronPassword) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: 'Unauthorized' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('Cron job triggered:', new Date().toISOString());
|
||||
|
||||
@@ -133,6 +133,6 @@ function shouldSkipAuth(pathname: string): boolean {
|
||||
// 配置middleware匹配规则
|
||||
export const config = {
|
||||
matcher: [
|
||||
'/((?!_next/static|_next/image|favicon.ico|login|register|oidc-register|warning|api/login|api/register|api/logout|api/auth/oidc|api/cron|api/server-config|api/proxy-m3u8|api/cms-proxy|api/tvbox/subscribe|api/theme/css|api/openlist/cms-proxy|api/openlist/play).*)',
|
||||
'/((?!_next/static|_next/image|favicon.ico|login|register|oidc-register|warning|api/login|api/register|api/logout|api/auth/oidc|api/cron/|api/server-config|api/proxy-m3u8|api/cms-proxy|api/tvbox/subscribe|api/theme/css|api/openlist/cms-proxy|api/openlist/play).*)',
|
||||
],
|
||||
};
|
||||
|
||||
3
start.js
3
start.js
@@ -58,8 +58,9 @@ const intervalId = setInterval(() => {
|
||||
|
||||
// 执行 cron 任务的函数
|
||||
function executeCronJob() {
|
||||
const cronPassword = process.env.CRON_PASSWORD || 'mtvpls';
|
||||
const cronUrl = `http://${process.env.HOSTNAME || 'localhost'}:${process.env.PORT || 3000
|
||||
}/api/cron`;
|
||||
}/api/cron/${cronPassword}`;
|
||||
|
||||
console.log(`Executing cron job: ${cronUrl}`);
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
],
|
||||
"crons": [
|
||||
{
|
||||
"path": "/api/cron",
|
||||
"path": "/api/cron/mtvpls",
|
||||
"schedule": "0 1 * * *"
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user