feat(frontend): use runtime env vars for config endpoint
Changed /api/config to read API_URL (without NEXT_PUBLIC_ prefix) which allows runtime configuration without rebuilding the image. Priority order: 1. API_URL (runtime env var) 2. NEXT_PUBLIC_API_URL (build-time fallback) 3. Default localhost values
This commit is contained in:
parent
0da936550b
commit
4577310c75
1 changed files with 26 additions and 7 deletions
|
|
@ -3,17 +3,35 @@ import { NextResponse } from 'next/server';
|
||||||
/**
|
/**
|
||||||
* Runtime Configuration API
|
* Runtime Configuration API
|
||||||
*
|
*
|
||||||
* This endpoint returns environment variables that can be read at runtime,
|
* This endpoint returns environment variables that are read at RUNTIME,
|
||||||
* allowing the application to use different configurations without rebuilding.
|
* not build time. This allows changing configuration without rebuilding.
|
||||||
*
|
*
|
||||||
* Usage: Fetch /api/config on app initialization and use the returned values.
|
* IMPORTANT: Use env vars WITHOUT the NEXT_PUBLIC_ prefix here!
|
||||||
|
* - NEXT_PUBLIC_* = baked in at build time (bad for runtime config)
|
||||||
|
* - Regular env vars = read at runtime (good!)
|
||||||
|
*
|
||||||
|
* In your .env or container config, set:
|
||||||
|
* API_URL=https://api.example.com
|
||||||
|
* BACKOFFICE_URL=https://backoffice.example.com
|
||||||
|
* (etc.)
|
||||||
|
*
|
||||||
|
* The NEXT_PUBLIC_* versions are only used as fallbacks for local dev.
|
||||||
*/
|
*/
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
|
// Priority: Runtime env vars > Build-time NEXT_PUBLIC_* > Defaults
|
||||||
const config = {
|
const config = {
|
||||||
apiUrl: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8521',
|
apiUrl: process.env.API_URL
|
||||||
backofficeUrl: process.env.NEXT_PUBLIC_BACKOFFICE_URL || 'http://localhost:3001',
|
|| process.env.NEXT_PUBLIC_API_URL
|
||||||
seederApiUrl: process.env.NEXT_PUBLIC_SEEDER_API_URL || 'http://localhost:3002',
|
|| 'http://localhost:8521',
|
||||||
scraperApiUrl: process.env.NEXT_PUBLIC_SCRAPER_API_URL || 'http://localhost:3003',
|
backofficeUrl: process.env.BACKOFFICE_URL
|
||||||
|
|| process.env.NEXT_PUBLIC_BACKOFFICE_URL
|
||||||
|
|| 'http://localhost:3001',
|
||||||
|
seederApiUrl: process.env.SEEDER_API_URL
|
||||||
|
|| process.env.NEXT_PUBLIC_SEEDER_API_URL
|
||||||
|
|| 'http://localhost:3002',
|
||||||
|
scraperApiUrl: process.env.SCRAPER_API_URL
|
||||||
|
|| process.env.NEXT_PUBLIC_SCRAPER_API_URL
|
||||||
|
|| 'http://localhost:3003',
|
||||||
};
|
};
|
||||||
|
|
||||||
return NextResponse.json(config, {
|
return NextResponse.json(config, {
|
||||||
|
|
@ -23,3 +41,4 @@ export async function GET() {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue