#!/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" # Umgebungsmodus bestimmen if [ "$1" = "--production" ] || [ "$FLASK_ENV" = "production" ]; then RUN_MODE="production" log_info "Produktionsmodus aktiviert" elif [ "$1" = "--development" ] || [ "$FLASK_ENV" = "development" ]; then RUN_MODE="development" log_info "Entwicklungsmodus aktiviert" else RUN_MODE="development" log_info "Standard-Entwicklungsmodus aktiviert" fi # Umgebungsvariablen laden if [ -f "env.backend" ]; then log_info "Lade Backend-Umgebungsvariablen..." export $(cat env.backend | grep -v '^#' | grep -v '^$' | xargs) export FLASK_ENV="$RUN_MODE" fi # Prüfe Python-Installation if ! command -v python3 &> /dev/null; then log_error "Python3 ist nicht installiert!" exit 1 fi # Prüfe pip-Installation if ! command -v pip3 &> /dev/null; then log_error "pip3 ist nicht installiert!" exit 1 fi log_success "Python-Installation verifiziert" # Erstelle virtuelle Umgebung falls nicht vorhanden if [ ! -d "venv" ]; then log_info "Erstelle virtuelle Python-Umgebung..." python3 -m venv venv fi # Aktiviere virtuelle Umgebung log_info "Aktiviere virtuelle Umgebung..." source venv/bin/activate # Installiere Dependencies log_info "Installiere Python-Dependencies..." pip install --upgrade pip pip install -r requirements.txt # Notwendige Verzeichnisse erstellen log_info "Erstelle notwendige Verzeichnisse..." mkdir -p instance logs migrations/versions # Datenbank initialisieren log_info "Initialisiere Datenbank..." export FLASK_APP=app.py python3 -c " from app import create_app, init_db app = create_app('$RUN_MODE') with app.app_context(): init_db() print('Datenbank initialisiert') " # Drucker initialisieren if [ ! -z "$PRINTERS" ]; then log_info "Initialisiere Drucker-Konfiguration..." python3 -c " from app import create_app, init_printers app = create_app('$RUN_MODE') with app.app_context(): init_printers() print('Drucker initialisiert') " fi # Server starten basierend auf Modus if [ "$RUN_MODE" = "production" ]; then log_info "Starte Backend-Server im Produktionsmodus..." log_info "Verwende Gunicorn für Produktionsbetrieb" # Prüfe ob Gunicorn installiert ist if ! command -v gunicorn &> /dev/null; then log_error "Gunicorn ist nicht installiert! Installiere mit: pip install gunicorn" exit 1 fi # Starte mit Produktions-Skript exec ./start-production.sh else log_info "Starte Backend-Server im Entwicklungsmodus..." # Flask-Entwicklungsserver export FLASK_APP=app.py export FLASK_ENV=development export FLASK_DEBUG=1 # Port prüfen PORT=${PORT:-5000} if netstat -tuln | grep -q ":$PORT "; then log_warning "Port $PORT ist bereits belegt!" log_info "Verwende alternativen Port 5001..." PORT=5001 fi log_info "Backend-Server startet auf Port $PORT..." # Flask-Server starten python3 -m flask run --host=0.0.0.0 --port=$PORT & FLASK_PID=$! # Warten auf Server-Start log_info "Warte auf Backend-Service..." timeout=60 counter=0 while [ $counter -lt $timeout ]; do if curl -f http://localhost:$PORT/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!" kill $FLASK_PID 2>/dev/null || true exit 1 fi # URLs anzeigen echo "" log_success "🎉 Backend-Server erfolgreich gestartet!" echo "" echo "📡 Backend-API: http://localhost:$PORT" echo "🔧 Backend-Health: http://localhost:$PORT/health" echo "📋 Backend-Test: http://localhost:$PORT/api/test" echo "" # Logs anzeigen (optional) if [ "$2" = "--logs" ] || [ "$3" = "--logs" ]; then log_info "Zeige Backend-Logs (Strg+C zum Beenden):" tail -f logs/myp.log 2>/dev/null || log_warning "Keine Log-Datei gefunden" fi log_info "Verwende Strg+C um den Server zu stoppen" # Warte auf Signal wait $FLASK_PID fi