|
| 1 | +import { type NextRequest, NextResponse } from 'next/server' |
| 2 | +import { getSession } from '@/lib/auth' |
| 3 | +import { env } from '@/lib/core/config/env' |
| 4 | + |
| 5 | +const ENV_URLS: Record<string, string | undefined> = { |
| 6 | + dev: env.MOTHERSHIP_DEV_URL, |
| 7 | + staging: env.MOTHERSHIP_STAGING_URL, |
| 8 | + prod: env.MOTHERSHIP_PROD_URL, |
| 9 | +} |
| 10 | + |
| 11 | +function getMothershipUrl(environment: string): string | null { |
| 12 | + return ENV_URLS[environment] ?? null |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Proxy to the mothership admin API. |
| 17 | + * |
| 18 | + * Query params: |
| 19 | + * env - "dev" | "staging" | "prod" |
| 20 | + * endpoint - the admin endpoint path, e.g. "requests", "licenses", "traces" |
| 21 | + * |
| 22 | + * The request body (for POST) is forwarded as-is. Additional query params |
| 23 | + * (e.g. requestId for GET /traces) are forwarded. |
| 24 | + */ |
| 25 | +export async function POST(req: NextRequest) { |
| 26 | + const session = await getSession() |
| 27 | + if (!session?.user || session.user.role !== 'admin') { |
| 28 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 29 | + } |
| 30 | + |
| 31 | + const adminKey = env.MOTHERSHIP_API_ADMIN_KEY |
| 32 | + if (!adminKey) { |
| 33 | + return NextResponse.json({ error: 'MOTHERSHIP_API_ADMIN_KEY not configured' }, { status: 500 }) |
| 34 | + } |
| 35 | + |
| 36 | + const { searchParams } = new URL(req.url) |
| 37 | + const environment = searchParams.get('env') || 'dev' |
| 38 | + const endpoint = searchParams.get('endpoint') |
| 39 | + |
| 40 | + if (!endpoint) { |
| 41 | + return NextResponse.json({ error: 'endpoint query param required' }, { status: 400 }) |
| 42 | + } |
| 43 | + |
| 44 | + const baseUrl = getMothershipUrl(environment) |
| 45 | + if (!baseUrl) { |
| 46 | + return NextResponse.json( |
| 47 | + { error: `No URL configured for environment: ${environment}` }, |
| 48 | + { status: 400 } |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + const targetUrl = `${baseUrl}/api/admin/${endpoint}` |
| 53 | + |
| 54 | + try { |
| 55 | + const body = await req.text() |
| 56 | + const upstream = await fetch(targetUrl, { |
| 57 | + method: 'POST', |
| 58 | + headers: { |
| 59 | + 'Content-Type': 'application/json', |
| 60 | + 'x-api-key': adminKey, |
| 61 | + }, |
| 62 | + ...(body ? { body } : {}), |
| 63 | + }) |
| 64 | + |
| 65 | + const data = await upstream.json() |
| 66 | + return NextResponse.json(data, { status: upstream.status }) |
| 67 | + } catch (error) { |
| 68 | + return NextResponse.json( |
| 69 | + { |
| 70 | + error: `Failed to reach mothership (${environment}): ${error instanceof Error ? error.message : 'Unknown error'}`, |
| 71 | + }, |
| 72 | + { status: 502 } |
| 73 | + ) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +export async function GET(req: NextRequest) { |
| 78 | + const session = await getSession() |
| 79 | + if (!session?.user || session.user.role !== 'admin') { |
| 80 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 81 | + } |
| 82 | + |
| 83 | + const adminKey = env.MOTHERSHIP_API_ADMIN_KEY |
| 84 | + if (!adminKey) { |
| 85 | + return NextResponse.json({ error: 'MOTHERSHIP_API_ADMIN_KEY not configured' }, { status: 500 }) |
| 86 | + } |
| 87 | + |
| 88 | + const { searchParams } = new URL(req.url) |
| 89 | + const environment = searchParams.get('env') || 'dev' |
| 90 | + const endpoint = searchParams.get('endpoint') |
| 91 | + |
| 92 | + if (!endpoint) { |
| 93 | + return NextResponse.json({ error: 'endpoint query param required' }, { status: 400 }) |
| 94 | + } |
| 95 | + |
| 96 | + const baseUrl = getMothershipUrl(environment) |
| 97 | + if (!baseUrl) { |
| 98 | + return NextResponse.json( |
| 99 | + { error: `No URL configured for environment: ${environment}` }, |
| 100 | + { status: 400 } |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + const forwardParams = new URLSearchParams() |
| 105 | + searchParams.forEach((value, key) => { |
| 106 | + if (key !== 'env' && key !== 'endpoint') { |
| 107 | + forwardParams.set(key, value) |
| 108 | + } |
| 109 | + }) |
| 110 | + |
| 111 | + const qs = forwardParams.toString() |
| 112 | + const targetUrl = `${baseUrl}/api/admin/${endpoint}${qs ? `?${qs}` : ''}` |
| 113 | + |
| 114 | + try { |
| 115 | + const upstream = await fetch(targetUrl, { |
| 116 | + method: 'GET', |
| 117 | + headers: { 'x-api-key': adminKey }, |
| 118 | + }) |
| 119 | + |
| 120 | + const data = await upstream.json() |
| 121 | + return NextResponse.json(data, { status: upstream.status }) |
| 122 | + } catch (error) { |
| 123 | + return NextResponse.json( |
| 124 | + { |
| 125 | + error: `Failed to reach mothership (${environment}): ${error instanceof Error ? error.message : 'Unknown error'}`, |
| 126 | + }, |
| 127 | + { status: 502 } |
| 128 | + ) |
| 129 | + } |
| 130 | +} |
0 commit comments