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 })
|
||||
}
|
||||
32
src/app/api/scan/route.ts
Normal file
32
src/app/api/scan/route.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { requireAdmin } from '@/lib/auth'
|
||||
import { isScanRunning, runFullScan } from '@/lib/scanner'
|
||||
import { getScanConfig } from '@/lib/app-settings'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const auth = await requireAdmin(request)
|
||||
if (auth instanceof NextResponse) return auth
|
||||
|
||||
const config = getScanConfig()
|
||||
|
||||
return NextResponse.json({
|
||||
isRunning: isScanRunning(),
|
||||
lastScanAt: config.lastScanAt,
|
||||
schedule: config.schedule,
|
||||
enabled: config.enabled,
|
||||
})
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const auth = await requireAdmin(request)
|
||||
if (auth instanceof NextResponse) return auth
|
||||
|
||||
if (isScanRunning()) {
|
||||
return NextResponse.json({ started: false, reason: 'already running' }, { status: 409 })
|
||||
}
|
||||
|
||||
// Fire-and-forget — do not await
|
||||
runFullScan().catch((err) => console.error('[api/scan] Scan error:', err))
|
||||
|
||||
return NextResponse.json({ started: true }, { status: 202 })
|
||||
}
|
||||
Reference in New Issue
Block a user