|
|
|
|
@@ -1,8 +1,9 @@
|
|
|
|
|
// 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';
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
|
|
|
|
|
// Route imports
|
|
|
|
|
import authRoutes from './routes/auth.js';
|
|
|
|
|
@@ -21,9 +22,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 +30,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,49 +39,82 @@ 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
|
|
|
|
|
const frontendBuildPath = path.join(__dirname, '../frontend-build');
|
|
|
|
|
console.log('📁 Frontend build path:', frontendBuildPath);
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
needsInitialSetup: !adminExists || adminExists['COUNT(*)'] === 0
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error checking initial setup:', error);
|
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
// Überprüfe ob das Verzeichnis existiert
|
|
|
|
|
if (fs.existsSync(frontendBuildPath)) {
|
|
|
|
|
console.log('✅ Frontend build directory exists');
|
|
|
|
|
const files = fs.readdirSync(frontendBuildPath);
|
|
|
|
|
console.log('📄 Files in frontend-build:', files);
|
|
|
|
|
|
|
|
|
|
// Serviere statische Dateien
|
|
|
|
|
app.use(express.static(frontendBuildPath));
|
|
|
|
|
|
|
|
|
|
console.log('✅ Static file serving configured');
|
|
|
|
|
} else {
|
|
|
|
|
console.log('❌ Frontend build directory NOT FOUND:', frontendBuildPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
|
const indexPath = path.join(frontendBuildPath, 'index.html');
|
|
|
|
|
console.log('📄 Serving index.html from:', indexPath);
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync(indexPath)) {
|
|
|
|
|
res.sendFile(indexPath);
|
|
|
|
|
} else {
|
|
|
|
|
console.error('❌ index.html not found at:', indexPath);
|
|
|
|
|
res.status(404).send('Frontend not found - index.html missing');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('*', (req, res) => {
|
|
|
|
|
// Ignoriere API Routes
|
|
|
|
|
if (req.path.startsWith('/api/')) {
|
|
|
|
|
return res.status(404).json({ error: 'API endpoint not found' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const indexPath = path.join(frontendBuildPath, 'index.html');
|
|
|
|
|
console.log('🔄 Client-side routing for:', req.path, '-> index.html');
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync(indexPath)) {
|
|
|
|
|
res.sendFile(indexPath);
|
|
|
|
|
} else {
|
|
|
|
|
console.error('❌ index.html not found for client-side routing');
|
|
|
|
|
res.status(404).json({ error: 'Frontend application not found' });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|