generated from haoqimao/MatchU--
feat(admin): add GET /api/admin/logs for ring buffer + rate_limit_events
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { isAdminAuthenticated } from '@/lib/admin/session'
|
||||
import { log } from '@/lib/observability/logger'
|
||||
import { listRateLimitEvents } from '@/lib/db/analytics'
|
||||
import type { LogLevel } from '@/lib/observability/types'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
const VALID_LEVELS: LogLevel[] = ['debug', 'info', 'warn', 'error']
|
||||
|
||||
export async function GET(request: Request) {
|
||||
if (!(await isAdminAuthenticated())) {
|
||||
return NextResponse.json({ code: 'UNAUTHORIZED' }, { status: 401 })
|
||||
}
|
||||
|
||||
const url = new URL(request.url)
|
||||
const view = url.searchParams.get('view') ?? 'logs'
|
||||
const limit = Math.min(500, Number(url.searchParams.get('limit') ?? '100'))
|
||||
const sinceRaw = url.searchParams.get('since')
|
||||
const since = sinceRaw ? Number(sinceRaw) : undefined
|
||||
|
||||
if (view === 'rate_limit') {
|
||||
const rows = listRateLimitEvents({ limit, since })
|
||||
return NextResponse.json({ view, rows })
|
||||
}
|
||||
|
||||
const levelParam = url.searchParams.get('level')
|
||||
const level = levelParam && (VALID_LEVELS as string[]).includes(levelParam)
|
||||
? (levelParam as LogLevel)
|
||||
: undefined
|
||||
|
||||
const events = log.recent({ level, limit, since })
|
||||
return NextResponse.json({ view: 'logs', rows: events })
|
||||
}
|
||||
Reference in New Issue
Block a user