This commit is contained in:
salvacybersec
2025-11-11 07:04:23 +03:00
parent 85631931f9
commit 9a3f299c0c
2 changed files with 74 additions and 33 deletions

View File

@@ -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'))") SESSION_SECRET=$(node -e "console.log(require('crypto').randomBytes(64).toString('hex'))")
export SESSION_SECRET export SESSION_SECRET
# Session secret'ı .env dosyasına kaydet (persist) # .env dosyası yolu
if [ ! -f "/app/.env" ]; then ENV_FILE="/app/.env"
echo "SESSION_SECRET=$SESSION_SECRET" > /app/.env
echo "✅ Yeni SESSION_SECRET oluşturuldu ve .env dosyasına kaydedildi" # 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)" 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 # Mevcut .env dosyasını güncelle
if grep -q "^SESSION_SECRET=" /app/.env; then if grep -q "^SESSION_SECRET=" "$ENV_FILE"; then
sed -i "s|^SESSION_SECRET=.*|SESSION_SECRET=$SESSION_SECRET|" /app/.env sed -i "s|^SESSION_SECRET=.*|SESSION_SECRET=$SESSION_SECRET|" "$ENV_FILE"
else else
echo "SESSION_SECRET=$SESSION_SECRET" >> /app/.env echo "SESSION_SECRET=$SESSION_SECRET" >> "$ENV_FILE"
fi fi
echo "✅ SESSION_SECRET güncellendi ve .env dosyasına kaydedildi" 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
fi fi
else else

View File

@@ -127,9 +127,17 @@ app.use(express.urlencoded({ extended: true }));
// Serve frontend build files (if exists, for production) // Serve frontend build files (if exists, for production)
const path = require('path'); const path = require('path');
const frontendDistPath = path.join(__dirname, '../../frontend/dist');
const fs = require('fs'); 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 // Serve static files (landing page) - but only for specific paths
// Don't serve from public if frontend dist exists (to avoid conflicts) // Don't serve from public if frontend dist exists (to avoid conflicts)
if (!fs.existsSync(frontendDistPath)) { if (!fs.existsSync(frontendDistPath)) {
@@ -137,20 +145,33 @@ if (!fs.existsSync(frontendDistPath)) {
} }
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 // List files for debugging
try { try {
const files = fs.readdirSync(frontendDistPath); const files = fs.readdirSync(frontendDistPath);
logger.info(`Frontend dist files: ${files.join(', ')}`); 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'); const assetsPath = path.join(frontendDistPath, 'assets');
if (fs.existsSync(assetsPath)) { if (fs.existsSync(assetsPath)) {
const assetFiles = fs.readdirSync(assetsPath); const assetFiles = fs.readdirSync(assetsPath);
logger.info(`Frontend assets: ${assetFiles.length} files`); 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) { } 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 // 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) // SPA fallback: serve index.html for all non-API routes (must be after all routes)
if (fs.existsSync(frontendDistPath)) { if (fs.existsSync(frontendDistPath)) {
const indexHtmlPath = path.join(frontendDistPath, 'index.html'); const indexHtmlPath = path.join(frontendDistPath, 'index.html');
logger.info(`Frontend SPA fallback enabled: ${indexHtmlPath}`);
app.get('*', (req, res, next) => { if (fs.existsSync(indexHtmlPath)) {
// Skip API routes and tracking routes logger.info(`✅ Frontend SPA fallback enabled: ${indexHtmlPath}`);
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 app.get('*', (req, res, next) => {
logger.info(`SPA fallback: serving index.html for ${req.path}`); // Skip API routes and tracking routes
if (req.path.startsWith('/api') || req.path.startsWith('/t/') || req.path.startsWith('/health')) {
// Serve frontend SPA return res.status(404).json({
if (fs.existsSync(indexHtmlPath)) { 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) => { res.sendFile(indexHtmlPath, (err) => {
if (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({ res.status(500).json({
success: false, success: false,
error: 'Failed to load frontend', error: 'Failed to load frontend',
}); });
} }
}); });
} else { });
logger.error(`index.html not found at: ${indexHtmlPath}`); } else {
res.status(500).json({ 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, success: false,
error: 'Frontend not found', error: 'Endpoint not found',
}); });
} });
}); }
} else { } 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) // 404 handler (if frontend not built)
app.use((req, res) => { app.use((req, res) => {
res.status(404).json({ res.status(404).json({