Legger med env

This commit is contained in:
Håkon Størdal 2025-09-03 00:41:40 +02:00
parent 193b0363e5
commit d7a94ee323
2 changed files with 47 additions and 14 deletions

View file

@ -13,10 +13,14 @@ RUN npm ci && npm cache clean --force
# Copy the rest of the application code # Copy the rest of the application code
COPY . . 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 SvelteKit sync to generate .svelte-kit directory and prepare the build
RUN npm run prepare RUN npm run prepare
# Build the application # Build the application with environment variables available
RUN npm run build RUN npm run build
# Production stage # 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/build ./build
COPY --from=base --chown=svelte:nodejs /app/package.json ./package.json 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 # Switch to the non-root user
USER svelte USER svelte
# Expose the port the app runs on # Expose the port the app runs on
EXPOSE 3000 EXPOSE 3000
# Set environment variables # Set environment variables - DATABASE_URL can be overridden at runtime
ENV NODE_ENV=production ENV NODE_ENV=production
ENV PORT=3000 ENV PORT=3000
ENV DATABASE_URL=""
# Use dumb-init to handle signals properly # Use dumb-init to handle signals properly
ENTRYPOINT ["dumb-init", "--"] ENTRYPOINT ["dumb-init", "--"]

View file

@ -1,23 +1,45 @@
import { env } from '$env/dynamic/private';
import { Pool } from 'pg'; import { Pool } from 'pg';
import { env } from '$env/dynamic/private';
// Create a connection pool // Get DATABASE_URL with fallback
const pool = new Pool({ const getDatabaseUrl = () => {
connectionString: env.DATABASE_URL, return (
ssl: false 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 // Simple database client
export const db = { export const db = {
// Execute a query // Execute a query
async query(text: string, params?: any[]) { async query(text: string, params?: any[]) {
const client = await pool.connect(); try {
const poolInstance = getPool();
const client = await poolInstance.connect();
try { try {
const result = await client.query(text, params); const result = await client.query(text, params);
return result; return result;
} finally { } finally {
client.release(); client.release();
} }
} catch (error) {
console.error('Database query failed:', error);
throw error;
}
}, },
// Test connection // Test connection
@ -76,6 +98,9 @@ export const db = {
// Close all connections // Close all connections
async close() { async close() {
if (pool) {
await pool.end(); await pool.end();
pool = null;
}
} }
}; };