setup script

This commit is contained in:
salvacybersec
2025-11-11 04:36:35 +03:00
parent f0b61735cc
commit 05bb2fc55c
6 changed files with 438 additions and 15 deletions

View File

@@ -32,6 +32,10 @@ COPY --from=builder /app/node_modules ./node_modules
# Copy application code
COPY --chown=oltalama:oltalama . .
# Copy entrypoint script
COPY --chown=oltalama:oltalama docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
# Create necessary directories
RUN mkdir -p database logs && \
chown -R oltalama:oltalama /app
@@ -46,8 +50,8 @@ EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"
# Use tini as entrypoint for proper signal handling
ENTRYPOINT ["/sbin/tini", "--"]
# Use tini and custom entrypoint for proper signal handling and auto-config
ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/docker-entrypoint.sh"]
# Start application
CMD ["node", "src/app.js"]

View File

@@ -19,14 +19,18 @@ RUN npm install
# Copy application code
COPY . .
# Copy entrypoint script
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
# Create necessary directories
RUN mkdir -p database logs
# Expose port
EXPOSE 3000
# Use tini as entrypoint
ENTRYPOINT ["/sbin/tini", "--"]
# Use tini and custom entrypoint
ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/docker-entrypoint.sh"]
# Start with nodemon for hot reload
CMD ["npm", "run", "dev"]

82
backend/docker-entrypoint.sh Executable file
View File

@@ -0,0 +1,82 @@
#!/bin/sh
set -e
# Docker Entrypoint Script - Backend
# Otomatik SESSION_SECRET oluşturma ve yönetimi
echo "🚀 Oltalama Backend başlatılıyor..."
# SESSION_SECRET kontrolü ve otomatik oluşturma
if [ -z "$SESSION_SECRET" ] || [ "$SESSION_SECRET" = "change-this-to-a-very-strong-random-secret" ]; then
echo "⚠️ SESSION_SECRET boş veya varsayılan değerde!"
# .env dosyası varsa SESSION_SECRET'ı oradan al
if [ -f "/app/.env" ]; then
SESSION_SECRET_FROM_FILE=$(grep "^SESSION_SECRET=" /app/.env | cut -d'=' -f2-)
if [ -n "$SESSION_SECRET_FROM_FILE" ] && [ "$SESSION_SECRET_FROM_FILE" != "change-this-to-a-very-strong-random-secret" ]; then
export SESSION_SECRET="$SESSION_SECRET_FROM_FILE"
echo "✅ SESSION_SECRET .env dosyasından yüklendi"
fi
fi
# Hala boşsa otomatik oluştur
if [ -z "$SESSION_SECRET" ] || [ "$SESSION_SECRET" = "change-this-to-a-very-strong-random-secret" ]; then
echo "🔑 Yeni SESSION_SECRET otomatik oluşturuluyor..."
# Node.js ile güçlü random secret oluştur
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"
echo "📝 SESSION_SECRET: ${SESSION_SECRET:0:20}... (ilk 20 karakter)"
else
# 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
else
echo "SESSION_SECRET=$SESSION_SECRET" >> /app/.env
fi
echo "✅ SESSION_SECRET güncellendi ve .env dosyasına kaydedildi"
fi
fi
else
echo "✅ SESSION_SECRET zaten ayarlanmış"
fi
# Database dizinini kontrol et
if [ ! -d "/app/database" ]; then
echo "📁 Database dizini oluşturuluyor..."
mkdir -p /app/database
fi
# Logs dizinini kontrol et
if [ ! -d "/app/logs" ]; then
echo "📁 Logs dizini oluşturuluyor..."
mkdir -p /app/logs
fi
# Database migration'ları çalıştır (ilk kurulumda)
if [ ! -f "/app/database/oltalama.db" ]; then
echo "🗄️ İlk kurulum tespit edildi, database oluşturuluyor..."
if [ -f "/app/migrations/run-migrations.js" ]; then
node /app/migrations/run-migrations.js || echo "⚠️ Migration hatası (normal olabilir)"
fi
# Seed data (opsiyonel)
if [ "${AUTO_SEED}" = "true" ] && [ -f "/app/seeders/run-seeders.js" ]; then
echo "🌱 Seed data ekleniyor..."
node /app/seeders/run-seeders.js || echo "⚠️ Seeding hatası (normal olabilir)"
fi
else
echo "✅ Database mevcut, migration atlanıyor"
fi
echo "✅ Backend hazır, uygulama başlatılıyor..."
echo ""
# CMD komutunu çalıştır (exec ile PID 1'e geç)
exec "$@"