From 5c7786bc19806966285ca192716ad8b041b49b7b Mon Sep 17 00:00:00 2001 From: donpat1to Date: Thu, 23 Oct 2025 00:24:23 +0200 Subject: [PATCH] added schema detection for prod dev --- Dockerfile | 2 +- backend/src/scripts/initializeDatabase.ts | 25 ++++++++++- backend/src/server.ts | 54 +++++++---------------- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7e55730..70767fc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -82,7 +82,7 @@ ENV PM2_HOME=/app/.pm2 USER schichtplan -EXPOSE 3000 3002 +EXPOSE 3002 HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:3002/api/health || exit 1 diff --git a/backend/src/scripts/initializeDatabase.ts b/backend/src/scripts/initializeDatabase.ts index 4ef1eb1..2b49b31 100644 --- a/backend/src/scripts/initializeDatabase.ts +++ b/backend/src/scripts/initializeDatabase.ts @@ -8,7 +8,30 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); export async function initializeDatabase(): Promise { - const schemaPath = path.join(__dirname, '../database/schema.sql'); + const possiblePaths = [ + path.join(__dirname, '../database/schema.sql'), + path.join(__dirname, '../../database/schema.sql'), + path.join(process.cwd(), 'database/schema.sql'), + path.join(process.cwd(), 'src/database/schema.sql'), + path.join(process.cwd(), 'dist/database/schema.sql') + ]; + + let schemaPath: string | null = null; + + for (const p of possiblePaths) { + if (fs.existsSync(p)) { + schemaPath = p; + break; + } + } + + if (!schemaPath) { + throw new Error( + `❌ schema.sql not found in any of the tested paths:\n${possiblePaths.join('\n')}` + ); + } + + console.log(`✅ Using schema at: ${schemaPath}`); const schema = fs.readFileSync(schemaPath, 'utf8'); try { diff --git a/backend/src/server.ts b/backend/src/server.ts index c264dae..8cb7a3c 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -1,7 +1,7 @@ // backend/src/server.ts import express from 'express'; -import { fileURLToPath } from 'url'; import path from 'path'; +import { fileURLToPath } from 'url'; import { initializeDatabase } from './scripts/initializeDatabase.js'; // Route imports @@ -21,9 +21,6 @@ const PORT = 3002; // Middleware app.use(express.json()); -// Serviere statische Frontend-Dateien -app.use(express.static(path.join(__dirname, '../../frontend-build'))); - // API Routes app.use('/api/setup', setupRoutes); app.use('/api/auth', authRoutes); @@ -32,17 +29,6 @@ app.use('/api/shift-plans', shiftPlanRoutes); app.use('/api/scheduled-shifts', scheduledShifts); app.use('/api/scheduling', schedulingRoutes); -// Error handling middleware should come after routes -app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => { - console.error('Unhandled error:', err); - res.status(500).json({ error: 'Internal server error' }); -}); - -// 404 handler for API routes -app.use('/api/*', (req, res) => { - res.status(404).json({ error: 'API endpoint not found' }); -}); - // Health route app.get('/api/health', (req: any, res: any) => { res.json({ @@ -52,23 +38,18 @@ app.get('/api/health', (req: any, res: any) => { }); }); -// Setup status route (additional endpoint for clarity) -app.get('/api/initial-setup', async (req: any, res: any) => { - try { - const { db } = await import('./services/databaseService.js'); - - const adminExists = await db.get<{ 'COUNT(*)': number }>( - 'SELECT COUNT(*) FROM employees WHERE role = ?', - ['admin'] - ); +// 🆕 STATIC FILE SERVING FÜR FRONTEND +app.use(express.static(path.join(__dirname, '../../frontend-build'))); - res.json({ - needsInitialSetup: !adminExists || adminExists['COUNT(*)'] === 0 - }); - } catch (error) { - console.error('Error checking initial setup:', error); - res.status(500).json({ error: 'Internal server error' }); - } +// 🆕 FALLBACK FÜR CLIENT-SIDE ROUTING +app.get('*', (req, res) => { + res.sendFile(path.join(__dirname, '../../frontend-build/index.html')); +}); + +// Error handling middleware +app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => { + console.error('Unhandled error:', err); + res.status(500).json({ error: 'Internal server error' }); }); // Initialize the application @@ -76,25 +57,24 @@ const initializeApp = async () => { try { // Initialize database with base schema await initializeDatabase(); - //console.log('✅ Database initialized successfully'); // Apply any pending migrations const { applyMigration } = await import('./scripts/applyMigration.js'); await applyMigration(); - //console.log('✅ Database migrations applied'); // Start server only after successful initialization app.listen(PORT, () => { - console.log('🎉 BACKEND STARTED SUCCESSFULLY!'); + console.log('🎉 APPLICATION STARTED SUCCESSFULLY!'); console.log(`📍 Port: ${PORT}`); - console.log(`📍 Health: http://localhost:${PORT}/api/health`); + console.log(`📍 Frontend: http://localhost:${PORT}`); + console.log(`📍 API: http://localhost:${PORT}/api`); console.log(''); - console.log(`🔧 Setup ready at: http://localhost:${PORT}/api/setup/status`); + console.log(`🔧 Setup: http://localhost:${PORT}/api/setup/status`); console.log('📝 Create your admin account on first launch'); }); } catch (error) { console.error('❌ Error during initialization:', error); - process.exit(1); // Exit if initialization fails + process.exit(1); } };