diff --git a/backend/docker-entrypoint.sh b/backend/docker-entrypoint.sh index f3b6197..3882a7a 100755 --- a/backend/docker-entrypoint.sh +++ b/backend/docker-entrypoint.sh @@ -27,19 +27,30 @@ if [ -z "$SESSION_SECRET" ] || [ "$SESSION_SECRET" = "change-this-to-a-very-stro SESSION_SECRET=$(node -e "console.log(require('crypto').randomBytes(64).toString('hex'))") export SESSION_SECRET - # Session secret'ı .env dosyasına kaydet (persist) - if [ ! -f "/app/.env" ]; then - echo "SESSION_SECRET=$SESSION_SECRET" > /app/.env - echo "✅ Yeni SESSION_SECRET oluşturuldu ve .env dosyasına kaydedildi" + # .env dosyası yolu + ENV_FILE="/app/.env" + + # Eğer .env bir dizin ise (bind mount sorunu), bind mount'u atla + # Sadece environment variable olarak kullan + if [ -d "$ENV_FILE" ]; then + echo "⚠️ .env bir dizin olarak tespit edildi (bind mount sorunu)" + echo "⚠️ SESSION_SECRET sadece environment variable olarak kullanılacak" echo "📝 SESSION_SECRET: ${SESSION_SECRET:0:20}... (ilk 20 karakter)" - else + echo "💡 İpucu: Host'ta .env dosyası oluşturun veya docker-compose.yml'de SESSION_SECRET environment variable'ı kullanın" + elif [ -f "$ENV_FILE" ]; then # Mevcut .env dosyasını güncelle - if grep -q "^SESSION_SECRET=" /app/.env; then - sed -i "s|^SESSION_SECRET=.*|SESSION_SECRET=$SESSION_SECRET|" /app/.env + if grep -q "^SESSION_SECRET=" "$ENV_FILE"; then + sed -i "s|^SESSION_SECRET=.*|SESSION_SECRET=$SESSION_SECRET|" "$ENV_FILE" else - echo "SESSION_SECRET=$SESSION_SECRET" >> /app/.env + echo "SESSION_SECRET=$SESSION_SECRET" >> "$ENV_FILE" fi echo "✅ SESSION_SECRET güncellendi ve .env dosyasına kaydedildi" + echo "📝 SESSION_SECRET: ${SESSION_SECRET:0:20}... (ilk 20 karakter)" + else + # .env dosyası yok, oluştur + echo "SESSION_SECRET=$SESSION_SECRET" > "$ENV_FILE" + echo "✅ Yeni SESSION_SECRET oluşturuldu ve .env dosyasına kaydedildi" + echo "📝 SESSION_SECRET: ${SESSION_SECRET:0:20}... (ilk 20 karakter)" fi fi else diff --git a/backend/src/app.js b/backend/src/app.js index ad72cbe..9ccb885 100644 --- a/backend/src/app.js +++ b/backend/src/app.js @@ -127,9 +127,17 @@ app.use(express.urlencoded({ extended: true })); // Serve frontend build files (if exists, for production) const path = require('path'); -const frontendDistPath = path.join(__dirname, '../../frontend/dist'); const fs = require('fs'); +// Determine frontend dist path +// In single container: /app/backend/src -> /app/frontend/dist +// In separate containers: backend/src -> ../../frontend/dist (won't exist, that's OK) +const frontendDistPath = path.join(__dirname, '../../frontend/dist'); + +// Log the resolved path for debugging +logger.info(`Frontend dist path resolved: ${frontendDistPath}`); +logger.info(`__dirname: ${__dirname}`); + // Serve static files (landing page) - but only for specific paths // Don't serve from public if frontend dist exists (to avoid conflicts) if (!fs.existsSync(frontendDistPath)) { @@ -137,20 +145,33 @@ if (!fs.existsSync(frontendDistPath)) { } if (fs.existsSync(frontendDistPath)) { - logger.info(`Frontend dist found at: ${frontendDistPath}`); + logger.info(`✅ Frontend dist found at: ${frontendDistPath}`); // List files for debugging try { const files = fs.readdirSync(frontendDistPath); logger.info(`Frontend dist files: ${files.join(', ')}`); + const indexHtmlPath = path.join(frontendDistPath, 'index.html'); + if (fs.existsSync(indexHtmlPath)) { + logger.info(`✅ index.html found at: ${indexHtmlPath}`); + } else { + logger.error(`❌ index.html NOT found at: ${indexHtmlPath}`); + } + const assetsPath = path.join(frontendDistPath, 'assets'); if (fs.existsSync(assetsPath)) { const assetFiles = fs.readdirSync(assetsPath); logger.info(`Frontend assets: ${assetFiles.length} files`); + if (assetFiles.length > 0) { + logger.info(`Sample assets: ${assetFiles.slice(0, 3).join(', ')}${assetFiles.length > 3 ? '...' : ''}`); + } + } else { + logger.warn(`⚠️ Assets directory not found at: ${assetsPath}`); } } catch (err) { - logger.warn(`Could not read frontend dist: ${err.message}`); + logger.error(`❌ Could not read frontend dist: ${err.message}`); + logger.error(`Error stack: ${err.stack}`); } // Serve static files with proper headers for SPA @@ -223,41 +244,50 @@ app.use('/t', require('./routes/tracking.routes')); // SPA fallback: serve index.html for all non-API routes (must be after all routes) if (fs.existsSync(frontendDistPath)) { const indexHtmlPath = path.join(frontendDistPath, 'index.html'); - logger.info(`Frontend SPA fallback enabled: ${indexHtmlPath}`); - app.get('*', (req, res, next) => { - // Skip API routes and tracking routes - if (req.path.startsWith('/api') || req.path.startsWith('/t/') || req.path.startsWith('/health')) { - return res.status(404).json({ - success: false, - error: 'Endpoint not found', - }); - } + if (fs.existsSync(indexHtmlPath)) { + logger.info(`✅ Frontend SPA fallback enabled: ${indexHtmlPath}`); - // Log SPA fallback - logger.info(`SPA fallback: serving index.html for ${req.path}`); - - // Serve frontend SPA - if (fs.existsSync(indexHtmlPath)) { + app.get('*', (req, res, next) => { + // Skip API routes and tracking routes + if (req.path.startsWith('/api') || req.path.startsWith('/t/') || req.path.startsWith('/health')) { + return res.status(404).json({ + success: false, + error: 'Endpoint not found', + }); + } + + // Log SPA fallback (only for non-asset requests to reduce noise) + if (!req.path.startsWith('/assets/') && !req.path.match(/\.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf|eot)$/)) { + logger.info(`SPA fallback: serving index.html for ${req.path}`); + } + + // Serve frontend SPA res.sendFile(indexHtmlPath, (err) => { if (err) { - logger.error(`Failed to send index.html: ${err.message}`); + logger.error(`❌ Failed to send index.html: ${err.message}`); + logger.error(`Error stack: ${err.stack}`); res.status(500).json({ success: false, error: 'Failed to load frontend', }); } }); - } else { - logger.error(`index.html not found at: ${indexHtmlPath}`); - res.status(500).json({ + }); + } else { + logger.error(`❌ index.html not found at: ${indexHtmlPath}`); + logger.error(`Frontend dist exists but index.html is missing!`); + // 404 handler (if frontend not properly built) + app.use((req, res) => { + res.status(404).json({ success: false, - error: 'Frontend not found', + error: 'Endpoint not found', }); - } - }); + }); + } } else { - logger.warn(`Frontend dist not found at: ${frontendDistPath}`); + logger.warn(`⚠️ Frontend dist not found at: ${frontendDistPath}`); + logger.warn(`This is normal if using separate frontend/backend containers.`); // 404 handler (if frontend not built) app.use((req, res) => { res.status(404).json({