Files
vps-panel/src/app/api/db/route.ts
T
2026-05-27 16:53:06 +03:00

46 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextRequest, NextResponse } from 'next/server'
import { requireAuth } from '@/lib/auth'
import { readConfig } from '@/lib/config'
import { testDb, getDbStats, getTables, runQuery } from '@/lib/db'
export async function GET(req: NextRequest) {
const err = await requireAuth(req)
if (err) return err
const dbId = req.nextUrl.searchParams.get('dbId')
const action = req.nextUrl.searchParams.get('action') ?? 'stats'
if (!dbId) return NextResponse.json({ error: 'dbId gerekli' }, { status: 400 })
const config = await readConfig()
const db = config.databases.find(d => d.id === dbId)
if (!db) return NextResponse.json({ error: 'DB bulunamadı' }, { status: 404 })
if (action === 'test') return NextResponse.json(await testDb(db))
try {
if (action === 'stats') return NextResponse.json(await getDbStats(db))
if (action === 'tables') return NextResponse.json(await getTables(db))
} catch (e: unknown) {
return NextResponse.json({ error: e instanceof Error ? e.message : 'Bağlantı hatası' }, { status: 400 })
}
return NextResponse.json({ error: 'Geçersiz action' }, { status: 400 })
}
export async function POST(req: NextRequest) {
const err = await requireAuth(req)
if (err) return err
const body = await req.json()
if (body.action === 'test') {
return NextResponse.json(await testDb(body.db))
}
const { dbId, sql } = body
const config = await readConfig()
const db = config.databases.find(d => d.id === dbId)
if (!db) return NextResponse.json({ error: 'DB bulunamadı' }, { status: 404 })
return NextResponse.json(await runQuery(db, sql))
}