From d7a94ee323ca225745cd12a3adbe2d7234a4eb78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20St=C3=B8rdal?= <30749741+hakon55@users.noreply.github.com> Date: Wed, 3 Sep 2025 00:41:40 +0200 Subject: [PATCH] Legger med env --- Dockerfile | 12 ++++++++++-- src/lib/db.ts | 49 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index 750ad57..cb8761b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,10 +13,14 @@ RUN npm ci && npm cache clean --force # Copy the rest of the application code COPY . . +# Set build-time environment variables for SvelteKit +ARG DATABASE_URL="postgresql://builduser:buildpass@localhost:5432/builddb" +ENV DATABASE_URL=${DATABASE_URL} + # Run SvelteKit sync to generate .svelte-kit directory and prepare the build RUN npm run prepare -# Build the application +# Build the application with environment variables available RUN npm run build # Production stage @@ -42,15 +46,19 @@ RUN npm ci --only=production && npm cache clean --force COPY --from=base --chown=svelte:nodejs /app/build ./build COPY --from=base --chown=svelte:nodejs /app/package.json ./package.json +# Copy environment file if it exists (optional) +COPY --chown=svelte:nodejs .env* ./ || true + # Switch to the non-root user USER svelte # Expose the port the app runs on EXPOSE 3000 -# Set environment variables +# Set environment variables - DATABASE_URL can be overridden at runtime ENV NODE_ENV=production ENV PORT=3000 +ENV DATABASE_URL="" # Use dumb-init to handle signals properly ENTRYPOINT ["dumb-init", "--"] diff --git a/src/lib/db.ts b/src/lib/db.ts index e252cdd..f78e4d2 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -1,22 +1,44 @@ -import { env } from '$env/dynamic/private'; import { Pool } from 'pg'; +import { env } from '$env/dynamic/private'; -// Create a connection pool -const pool = new Pool({ - connectionString: env.DATABASE_URL, - ssl: false -}); +// Get DATABASE_URL with fallback +const getDatabaseUrl = () => { + return ( + env.DATABASE_URL || + process.env.DATABASE_URL || + 'postgresql://postgres:password@localhost:5432/egentrening' + ); +}; + +// Lazy pool creation - only create when actually needed +let pool: Pool | null = null; + +const getPool = () => { + if (!pool) { + pool = new Pool({ + connectionString: getDatabaseUrl(), + ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false + }); + } + return pool; +}; // Simple database client export const db = { // Execute a query async query(text: string, params?: any[]) { - const client = await pool.connect(); try { - const result = await client.query(text, params); - return result; - } finally { - client.release(); + const poolInstance = getPool(); + const client = await poolInstance.connect(); + try { + const result = await client.query(text, params); + return result; + } finally { + client.release(); + } + } catch (error) { + console.error('Database query failed:', error); + throw error; } }, @@ -76,6 +98,9 @@ export const db = { // Close all connections async close() { - await pool.end(); + if (pool) { + await pool.end(); + pool = null; + } } };