"feat: Implement Docker Compose for backend and frontend servers"

This commit is contained in:
2025-05-23 08:32:15 +02:00
parent ef6a8f7b81
commit 359cb4a219
6 changed files with 751 additions and 1 deletions

View File

@@ -1 +1,178 @@
# 🏭 MYP Backend - Standalone Server Konfiguration
# Backend-Service als vollständig unabhängiger Server
version: '3.8'
services:
# === BACKEND SERVICE ===
backend:
build:
context: .
dockerfile: Dockerfile
args:
- BUILDKIT_INLINE_CACHE=1
image: myp/backend:latest
container_name: myp-backend-standalone
restart: unless-stopped
environment:
# Flask-Konfiguration
- FLASK_APP=app.py
- FLASK_ENV=${FLASK_ENV:-production}
- PYTHONUNBUFFERED=1
# Datenbank
- DATABASE_PATH=${DATABASE_PATH:-instance/myp.db}
# Sicherheit
- SECRET_KEY=${SECRET_KEY:-7445630171969DFAC92C53CEC92E67A9CB2E00B3CB2F}
- JWT_SECRET=${JWT_SECRET:-secure-jwt-secret}
# CORS-Konfiguration für Frontend-Zugriff
- CORS_ORIGINS=${CORS_ORIGINS:-http://localhost:3000,https://frontend.myp.local}
# Drucker-Konfiguration
- "PRINTERS=${PRINTERS:-{\"Drucker 1\": {\"ip\": \"192.168.0.100\"}, \"Drucker 2\": {\"ip\": \"192.168.0.101\"}, \"Drucker 3\": {\"ip\": \"192.168.0.102\"}, \"Drucker 4\": {\"ip\": \"192.168.0.103\"}, \"Drucker 5\": {\"ip\": \"192.168.0.104\"}, \"Drucker 6\": {\"ip\": \"192.168.0.106\"}}}"
# TAPO Smart Plug
- TAPO_USERNAME=${TAPO_USERNAME:-till.tomczak@mercedes-benz.com}
- TAPO_PASSWORD=${TAPO_PASSWORD:-744563017196A}
# Netzwerk
- HOST=0.0.0.0
- PORT=5000
# Logging
- LOG_LEVEL=${LOG_LEVEL:-INFO}
volumes:
- backend_instance:/app/instance
- backend_logs:/app/logs
- backend_migrations:/app/migrations
- ./config:/app/config:ro
ports:
- "5000:5000" # Direkter Port-Zugang für Backend-Server
networks:
- backend-network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:5000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
labels:
- "service.type=backend"
- "service.name=myp-backend"
- "service.environment=${FLASK_ENV:-production}"
# === BACKEND DATENBANK (Optional: Separate PostgreSQL) ===
backend-db:
image: postgres:15-alpine
container_name: myp-backend-db
restart: unless-stopped
environment:
- POSTGRES_DB=${DB_NAME:-myp_backend}
- POSTGRES_USER=${DB_USER:-myp_user}
- POSTGRES_PASSWORD=${DB_PASSWORD:-secure_password}
- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- backend_db_data:/var/lib/postgresql/data
- ./sql/init:/docker-entrypoint-initdb.d:ro
ports:
- "5432:5432"
networks:
- backend-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-myp_user} -d ${DB_NAME:-myp_backend}"]
interval: 10s
timeout: 5s
retries: 5
# === BACKEND CACHE (Redis) ===
backend-cache:
image: redis:7.2-alpine
container_name: myp-backend-cache
restart: unless-stopped
command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD:-backend_cache_password}
volumes:
- backend_cache_data:/data
ports:
- "6379:6379"
networks:
- backend-network
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 3
# === PERSISTENTE VOLUMES ===
volumes:
backend_instance:
driver: local
driver_opts:
type: none
o: bind
device: ./instance
backend_logs:
driver: local
driver_opts:
type: none
o: bind
device: ./logs
backend_migrations:
driver: local
driver_opts:
type: none
o: bind
device: ./migrations
backend_db_data:
driver: local
backend_cache_data:
driver: local
# === BACKEND-NETZWERK ===
networks:
backend-network:
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "false"
com.docker.network.bridge.enable_ip_masquerade: "true"
labels:
- "description=MYP Backend Server Netzwerk"
- "project=myp-backend"
- "tier=backend"
# === KONFIGURATION FÜR BACKEND ===
x-backend-defaults: &backend-defaults
restart: unless-stopped
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
labels: "service,environment,tier"
x-healthcheck-backend: &backend-healthcheck
interval: 30s
timeout: 10s
retries: 3
start_period: 40s

66
backend/env.backend Normal file
View File

@@ -0,0 +1,66 @@
# 🏭 MYP Backend - Standalone Server Konfiguration
# Umgebungsvariablen ausschließlich für den Backend-Server
# === FLASK KONFIGURATION ===
FLASK_APP=app.py
FLASK_ENV=production
PYTHONUNBUFFERED=1
# === DATENBANK KONFIGURATION ===
# SQLite (Default)
DATABASE_PATH=instance/myp.db
# PostgreSQL (Optional)
DB_NAME=myp_backend
DB_USER=myp_user
DB_PASSWORD=secure_backend_password
DB_HOST=localhost
DB_PORT=5432
# === SICHERHEIT ===
SECRET_KEY=7445630171969DFAC92C53CEC92E67A9CB2E00B3CB2F
JWT_SECRET=secure-jwt-secret-backend-2024
JWT_ACCESS_TOKEN_EXPIRES=3600
JWT_REFRESH_TOKEN_EXPIRES=2592000
# === CORS KONFIGURATION ===
# Erlaubte Frontend-Origins
CORS_ORIGINS=http://localhost:3000,https://frontend.myp.local,https://myp.frontend.local
# === DRUCKER KONFIGURATION ===
PRINTERS={"Drucker 1": {"ip": "192.168.0.100"}, "Drucker 2": {"ip": "192.168.0.101"}, "Drucker 3": {"ip": "192.168.0.102"}, "Drucker 4": {"ip": "192.168.0.103"}, "Drucker 5": {"ip": "192.168.0.104"}, "Drucker 6": {"ip": "192.168.0.106"}}
# === TAPO SMART PLUG ===
TAPO_USERNAME=till.tomczak@mercedes-benz.com
TAPO_PASSWORD=744563017196A
# === NETZWERK KONFIGURATION ===
HOST=0.0.0.0
PORT=5000
BACKEND_URL=http://localhost:5000
# === CACHE KONFIGURATION ===
REDIS_PASSWORD=backend_cache_password
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0
# === LOGGING ===
LOG_LEVEL=INFO
LOG_FILE=logs/backend.log
LOG_MAX_SIZE=10485760
LOG_BACKUP_COUNT=5
# === DATEISYSTEM ===
UPLOAD_FOLDER=uploads
MAX_CONTENT_LENGTH=16777216
# === MONITORING ===
HEALTH_CHECK_INTERVAL=30
METRICS_ENABLED=true
METRICS_PORT=9090
# === ENTWICKLUNG ===
DEBUG=false
TESTING=false
DEVELOPMENT=false

View File

@@ -0,0 +1,130 @@
#!/bin/bash
# 🏭 MYP Backend - Standalone Server Start
# Startet den Backend-Server vollständig unabhängig vom Frontend
set -e
echo "🏭 MYP Backend - Standalone Server wird gestartet..."
# Farben für Terminal-Ausgabe
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Funktionen
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Arbeitsverzeichnis setzen
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
log_info "Arbeitsverzeichnis: $SCRIPT_DIR"
# Umgebungsvariablen laden
if [ -f ".env.backend" ]; then
log_info "Lade Backend-Umgebungsvariablen..."
export $(cat .env.backend | grep -v '^#' | xargs)
fi
# Prüfe Docker-Installation
if ! command -v docker &> /dev/null; then
log_error "Docker ist nicht installiert!"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
log_error "Docker Compose ist nicht installiert!"
exit 1
fi
log_success "Docker-Installation verifiziert"
# Alte Container stoppen und entfernen
log_info "Stoppe eventuell laufende Backend-Container..."
docker-compose -f docker-compose.backend.yml down --remove-orphans 2>/dev/null || true
# Images aufräumen (optional)
if [ "$1" = "--clean" ]; then
log_info "Räume alte Backend-Images auf..."
docker-compose -f docker-compose.backend.yml down --rmi all --volumes 2>/dev/null || true
docker system prune -f
fi
# Notwendige Verzeichnisse erstellen
log_info "Erstelle notwendige Verzeichnisse..."
mkdir -p instance logs migrations/versions config
# Backend-Container builden und starten
log_info "Backend-Container werden gebaut und gestartet..."
docker-compose -f docker-compose.backend.yml up --build -d
# Warten auf Backend-Start
log_info "Warte auf Backend-Service..."
timeout=120
counter=0
while [ $counter -lt $timeout ]; do
if curl -f http://localhost:5000/health >/dev/null 2>&1; then
log_success "Backend-Server ist bereit!"
break
fi
if [ $((counter % 10)) -eq 0 ]; then
log_info "Warte auf Backend-Service... ($counter/$timeout Sekunden)"
fi
sleep 1
counter=$((counter + 1))
done
if [ $counter -eq $timeout ]; then
log_error "Backend-Service konnte nicht gestartet werden!"
log_info "Zeige Backend-Logs:"
docker-compose -f docker-compose.backend.yml logs backend
exit 1
fi
# Migrationen ausführen
log_info "Führe Datenbank-Migrationen aus..."
docker-compose -f docker-compose.backend.yml exec backend flask db upgrade || log_warning "Migrationen fehlgeschlagen oder nicht erforderlich"
# Service-Status anzeigen
log_info "Backend-Service Status:"
docker-compose -f docker-compose.backend.yml ps
# URLs anzeigen
echo ""
log_success "🎉 Backend-Server erfolgreich gestartet!"
echo ""
echo "📡 Backend-API: http://localhost:5000"
echo "🔧 Backend-Health: http://localhost:5000/health"
echo "📋 Backend-Swagger: http://localhost:5000/swagger"
echo "🗄️ PostgreSQL: localhost:5432"
echo "⚡ Redis-Cache: localhost:6379"
echo ""
# Logs anzeigen (optional)
if [ "$1" = "--logs" ] || [ "$2" = "--logs" ]; then
log_info "Zeige Backend-Logs (Strg+C zum Beenden):"
docker-compose -f docker-compose.backend.yml logs -f
fi
log_info "Verwende 'docker-compose -f docker-compose.backend.yml logs -f' um Logs zu verfolgen"
log_info "Verwende 'docker-compose -f docker-compose.backend.yml down' um den Server zu stoppen"