diff --git a/src/hooks.server.ts b/src/hooks.server.ts index e60cae0..ed3dba7 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -7,20 +7,25 @@ import { sequence } from "@sveltejs/kit/hooks" import * as auth from "./auth" import { initializeScheduler } from "./lib/server/scheduler" import { dbConnect } from "./utils/db" +import { env } from "$env/dynamic/private" import fs from 'fs' import path from 'path' -// Initialize database connection on server startup -console.log('🚀 Server starting - initializing database connection...'); -await dbConnect().then(() => { - console.log('✅ Database connected successfully'); - // Initialize the recurring payment scheduler after DB is ready - initializeScheduler(); - console.log('✅ Recurring payment scheduler initialized'); -}).catch((error) => { - console.error('❌ Failed to connect to database on startup:', error); - // Don't crash the server - API routes will attempt reconnection -}); +// Initialize database connection on server startup (skip during build when no env vars) +if (env.MONGO_URL) { + console.log('🚀 Server starting - initializing database connection...'); + await dbConnect().then(() => { + console.log('✅ Database connected successfully'); + // Initialize the recurring payment scheduler after DB is ready + initializeScheduler(); + console.log('✅ Recurring payment scheduler initialized'); + }).catch((error) => { + console.error('❌ Failed to connect to database on startup:', error); + // Don't crash the server - API routes will attempt reconnection + }); +} else { + console.log('⚠️ MONGO_URL not set - skipping database initialization (build mode)'); +} async function authorization({ event, resolve }) { const session = await event.locals.auth();