add scanning
This commit is contained in:
42
src/app/api/scan-settings/route.ts
Normal file
42
src/app/api/scan-settings/route.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import cron from 'node-cron'
|
||||
import { requireAdmin } from '@/lib/auth'
|
||||
import { getScanConfig, updateScanConfig } from '@/lib/app-settings'
|
||||
import { restartScheduler } from '@/lib/scheduler'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const auth = await requireAdmin(request)
|
||||
if (auth instanceof NextResponse) return auth
|
||||
|
||||
const { schedule, enabled } = getScanConfig()
|
||||
return NextResponse.json({ schedule, enabled })
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest) {
|
||||
const auth = await requireAdmin(request)
|
||||
if (auth instanceof NextResponse) return auth
|
||||
|
||||
let body: { schedule?: string; enabled?: boolean }
|
||||
try {
|
||||
body = await request.json()
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 })
|
||||
}
|
||||
|
||||
const { schedule, enabled } = body
|
||||
|
||||
if (typeof schedule !== 'string' || !schedule.trim()) {
|
||||
return NextResponse.json({ error: 'schedule is required' }, { status: 400 })
|
||||
}
|
||||
if (typeof enabled !== 'boolean') {
|
||||
return NextResponse.json({ error: 'enabled must be a boolean' }, { status: 400 })
|
||||
}
|
||||
if (!cron.validate(schedule)) {
|
||||
return NextResponse.json({ error: 'Invalid cron expression' }, { status: 400 })
|
||||
}
|
||||
|
||||
updateScanConfig(schedule, enabled)
|
||||
restartScheduler()
|
||||
|
||||
return NextResponse.json({ schedule, enabled })
|
||||
}
|
||||
Reference in New Issue
Block a user