feat(admin): add GET /api/admin/logs for ring buffer + rate_limit_events

This commit is contained in:
root
2026-05-19 08:50:15 +00:00
parent 5c9a51b8fc
commit 4ea91f9f53
+34
View File
@@ -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 })
}