diff --git a/backend/server.js b/backend/server.js index 6fb7a0d..e1091a5 100644 --- a/backend/server.js +++ b/backend/server.js @@ -115,6 +115,8 @@ const corsOptions = { // Only attach CORS to API endpoints, not static assets app.use('/api', cors(corsOptions)); +// Handle preflight explicitly for API paths +app.options('/api/*', cors(corsOptions)); // Initialize rate limiters (they will be created dynamically) let generalRateLimiter; @@ -138,6 +140,22 @@ async function initializeRateLimiters() { app.use(express.json({ limit: '100mb' })); app.use(express.urlencoded({ extended: true, limit: '100mb' })); +// Request logging for API routes (with timestamps) +const apiRequestLogger = (req, res, next) => { + try { + const started = Date.now(); + const ts = new Date().toISOString(); + logger.info(`[${ts}] ${req.method} ${req.originalUrl}`); + res.on('finish', () => { + const ms = Date.now() - started; + const tsDone = new Date().toISOString(); + logger.info(`[${tsDone}] ${req.method} ${req.originalUrl} -> ${res.statusCode} (${ms}ms)`); + }); + } catch (_) {} + next(); +}; +app.use('/api', apiRequestLogger); + // Maintenance mode middleware - add after body parsing but before routes app.use(maintenanceMiddleware); diff --git a/scripts/setup.sh b/scripts/setup.sh index ec826d7..1dd5d27 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -690,8 +690,15 @@ EOF # Start services log_step "Starting services..." systemctl daemon-reload - systemctl enable picpeak-backend picpeak-workers - systemctl start picpeak-backend picpeak-workers + systemctl enable picpeak-backend + # Stop/remove legacy workers service if present + if systemctl list-unit-files | grep -q '^picpeak-workers.service'; then + systemctl disable picpeak-workers || true + systemctl stop picpeak-workers || true + rm -f /etc/systemd/system/picpeak-workers.service + systemctl daemon-reload + fi + systemctl start picpeak-backend log_success "Native installation completed!" } @@ -1001,7 +1008,10 @@ update_native_installation() { log_step "Updating native installation..." # Stop services - systemctl stop picpeak-backend picpeak-workers + systemctl stop picpeak-backend || true + if systemctl list-unit-files | grep -q '^picpeak-workers.service'; then + systemctl stop picpeak-workers || true + fi # Backup current configuration if [[ -f "$NATIVE_APP_DIR/app/backend/.env" ]]; then @@ -1042,7 +1052,7 @@ update_native_installation() { fi # Restart services - systemctl restart picpeak-backend picpeak-workers + systemctl restart picpeak-backend log_success "Native installation updated successfully!" }