ab6728c2d5
The ensureInitialized() env check threw on missing SUPABASE_SERVICE_ROLE_KEY and CRON_SECRET during Next.js page collection at Docker build time. These server-only vars are injected at runtime, not build time. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
82 lines
2.3 KiB
TypeScript
82 lines
2.3 KiB
TypeScript
import { loadExtensions } from '@/lib/extensions/loader'
|
|
import { setContextFactory } from '@/lib/extensions/registry'
|
|
import { createExtensionContext } from '@/lib/extensions/context-factory'
|
|
import { registerSupplierInvoiceHandler } from '@/lib/bookkeeping/handlers/supplier-invoice-handler'
|
|
import { createLogger } from '@/lib/logger'
|
|
|
|
const log = createLogger('init')
|
|
|
|
let initialized = false
|
|
|
|
const REQUIRED_CORE_VARS = [
|
|
'NEXT_PUBLIC_SUPABASE_URL',
|
|
'NEXT_PUBLIC_SUPABASE_ANON_KEY',
|
|
'SUPABASE_SERVICE_ROLE_KEY',
|
|
'NEXT_PUBLIC_APP_URL',
|
|
'CRON_SECRET',
|
|
] as const
|
|
|
|
const REQUIRED_EXTENSION_VARS = [
|
|
'ENABLE_BANKING_APP_ID',
|
|
'ENABLE_BANKING_PRIVATE_KEY',
|
|
'ANTHROPIC_API_KEY',
|
|
'OPENAI_API_KEY',
|
|
] as const
|
|
|
|
const OPTIONAL_VARS = [
|
|
'SENTRY_DSN',
|
|
'LANGFUSE_SECRET_KEY',
|
|
'LANGFUSE_PUBLIC_KEY',
|
|
] as const
|
|
|
|
function validateEnvironment(): void {
|
|
// During Docker builds, NEXT_PUBLIC_* vars are placeholder sentinels
|
|
// replaced at runtime by docker-entrypoint.sh. Server-only vars like
|
|
// SUPABASE_SERVICE_ROLE_KEY are not available at build time at all.
|
|
// Skip validation so Next.js page collection doesn't fail.
|
|
const isBuildPlaceholder = process.env.NEXT_PUBLIC_SUPABASE_URL?.startsWith('__')
|
|
if (isBuildPlaceholder) return
|
|
|
|
const missing: string[] = []
|
|
|
|
for (const v of REQUIRED_CORE_VARS) {
|
|
if (!process.env[v]) missing.push(v)
|
|
}
|
|
|
|
if (missing.length > 0) {
|
|
throw new Error(`Missing required environment variables: ${missing.join(', ')}`)
|
|
}
|
|
|
|
const missingExt: string[] = []
|
|
for (const v of REQUIRED_EXTENSION_VARS) {
|
|
if (!process.env[v]) missingExt.push(v)
|
|
}
|
|
|
|
if (missingExt.length > 0) {
|
|
log.warn(`Missing extension environment variables (extensions needing them may not work): ${missingExt.join(', ')}`)
|
|
}
|
|
|
|
for (const v of OPTIONAL_VARS) {
|
|
if (!process.env[v]) {
|
|
log.warn(`Optional environment variable ${v} is not set`)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ensure the system is initialized (extensions loaded, context factory wired,
|
|
* core event handlers registered).
|
|
* Called from API routes that emit events.
|
|
* Idempotent — safe to call multiple times.
|
|
*/
|
|
export function ensureInitialized(): void {
|
|
if (initialized) return
|
|
|
|
validateEnvironment()
|
|
setContextFactory(createExtensionContext)
|
|
registerSupplierInvoiceHandler()
|
|
loadExtensions()
|
|
|
|
initialized = true
|
|
}
|