Some checks failed
Staging Build / build (push) Has been cancelled
- Migration: add licserver_base_url/api_key cols to settings table - New: lib/actions/licserver-config.ts (read/write/test server actions) - Updated: proxy routes read config from DB with env-var fallback - Updated: admin/einstellungen adds LicServer card with URL + API key input, show/hide toggle, Save and Connection Test buttons
72 lines
2.9 KiB
TypeScript
72 lines
2.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
import { getLicServerConfig } from '@/lib/actions/licserver-config'
|
|
import type { PartnerDtoPagedResult, LicServerProblemDetails } from '@/lib/types/licserver'
|
|
|
|
/**
|
|
* GET /api/licserver/partners
|
|
*
|
|
* Proxy to LicServer GET /api-v1/partners
|
|
* Query params forwarded: search, page, pageSize
|
|
*
|
|
* Secured: requires a valid Supabase session.
|
|
* base_url + api_key are read from DB settings (admin-configurable),
|
|
* with fallback to LICSERVER_BASE_URL / LICSERVER_API_KEY env vars.
|
|
*/
|
|
export async function GET(req: NextRequest) {
|
|
// ── Auth guard ─────────────────────────────────────────────────────────
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
// ── Load config from DB (falls back to env vars) ──────────────────────
|
|
const cfg = await getLicServerConfig()
|
|
if (!cfg.base_url || !cfg.api_key) {
|
|
console.error('[licserver] base_url or api_key not configured (DB or env)')
|
|
return NextResponse.json(
|
|
{ error: 'LicServer nicht konfiguriert. Bitte in den Admin-Einstellungen hinterlegen.' },
|
|
{ status: 503 }
|
|
)
|
|
}
|
|
|
|
// ── Forward query params ───────────────────────────────────────────────
|
|
const { searchParams } = req.nextUrl
|
|
const upstream = new URL(`${cfg.base_url}/api-v1/partners`)
|
|
const search = searchParams.get('search')
|
|
const page = searchParams.get('page')
|
|
const pageSize = searchParams.get('pageSize')
|
|
if (search) upstream.searchParams.set('search', search)
|
|
if (page) upstream.searchParams.set('page', page)
|
|
if (pageSize) upstream.searchParams.set('pageSize', pageSize)
|
|
|
|
// ── Proxy request ──────────────────────────────────────────────────────
|
|
try {
|
|
const res = await fetch(upstream.toString(), {
|
|
method: 'GET',
|
|
headers: {
|
|
'X-Api-Key': cfg.api_key,
|
|
'Accept': 'application/json',
|
|
},
|
|
// Internal container network — short timeout
|
|
signal: AbortSignal.timeout(5_000),
|
|
})
|
|
|
|
if (!res.ok) {
|
|
const problem: LicServerProblemDetails = await res.json().catch(() => ({}))
|
|
return NextResponse.json(problem, { status: res.status })
|
|
}
|
|
|
|
const data: PartnerDtoPagedResult = await res.json()
|
|
return NextResponse.json(data)
|
|
|
|
} catch (err) {
|
|
console.error('[licserver] partners fetch error:', err)
|
|
return NextResponse.json(
|
|
{ error: 'LicServer nicht erreichbar' },
|
|
{ status: 502 }
|
|
)
|
|
}
|
|
}
|