"Refactor: Rename and restructure guide file for separate backend servers"

This commit is contained in:
Till Tomczak 2025-05-23 08:50:00 +02:00
parent 31b538185a
commit 03ff4260e2
2 changed files with 137 additions and 72 deletions

View File

@ -37,94 +37,159 @@ cd "$SCRIPT_DIR"
log_info "Arbeitsverzeichnis: $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 # Umgebungsvariablen laden
if [ -f ".env.backend" ]; then if [ -f "env.backend" ]; then
log_info "Lade Backend-Umgebungsvariablen..." log_info "Lade Backend-Umgebungsvariablen..."
export $(cat .env.backend | grep -v '^#' | xargs) export $(cat env.backend | grep -v '^#' | grep -v '^$' | xargs)
export FLASK_ENV="$RUN_MODE"
fi fi
# Prüfe Docker-Installation # Prüfe Python-Installation
if ! command -v docker &> /dev/null; then if ! command -v python3 &> /dev/null; then
log_error "Docker ist nicht installiert!" log_error "Python3 ist nicht installiert!"
exit 1 exit 1
fi fi
if ! command -v docker-compose &> /dev/null; then # Prüfe pip-Installation
log_error "Docker Compose ist nicht installiert!" if ! command -v pip3 &> /dev/null; then
log_error "pip3 ist nicht installiert!"
exit 1 exit 1
fi fi
log_success "Docker-Installation verifiziert" log_success "Python-Installation verifiziert"
# Alte Container stoppen und entfernen # Erstelle virtuelle Umgebung falls nicht vorhanden
log_info "Stoppe eventuell laufende Backend-Container..." if [ ! -d "venv" ]; then
docker-compose -f docker-compose.backend.yml down --remove-orphans 2>/dev/null || true log_info "Erstelle virtuelle Python-Umgebung..."
python3 -m venv venv
# 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 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 # Notwendige Verzeichnisse erstellen
log_info "Erstelle notwendige Verzeichnisse..." log_info "Erstelle notwendige Verzeichnisse..."
mkdir -p instance logs migrations/versions config mkdir -p instance logs migrations/versions
# Backend-Container builden und starten # Datenbank initialisieren
log_info "Backend-Container werden gebaut und gestartet..." log_info "Initialisiere Datenbank..."
docker-compose -f docker-compose.backend.yml up --build -d 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')
"
# Warten auf Backend-Start # Drucker initialisieren
log_info "Warte auf Backend-Service..." if [ ! -z "$PRINTERS" ]; then
timeout=120 log_info "Initialisiere Drucker-Konfiguration..."
counter=0 python3 -c "
from app import create_app, init_printers
while [ $counter -lt $timeout ]; do app = create_app('$RUN_MODE')
if curl -f http://localhost:5000/health >/dev/null 2>&1; then with app.app_context():
log_success "Backend-Server ist bereit!" init_printers()
break print('Drucker initialisiert')
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 fi
# Migrationen ausführen # Server starten basierend auf Modus
log_info "Führe Datenbank-Migrationen aus..." if [ "$RUN_MODE" = "production" ]; then
docker-compose -f docker-compose.backend.yml exec backend flask db upgrade || log_warning "Migrationen fehlgeschlagen oder nicht erforderlich" log_info "Starte Backend-Server im Produktionsmodus..."
log_info "Verwende Gunicorn für Produktionsbetrieb"
# Service-Status anzeigen # Prüfe ob Gunicorn installiert ist
log_info "Backend-Service Status:" if ! command -v gunicorn &> /dev/null; then
docker-compose -f docker-compose.backend.yml ps log_error "Gunicorn ist nicht installiert! Installiere mit: pip install gunicorn"
exit 1
fi
# URLs anzeigen # Starte mit Produktions-Skript
echo "" exec ./start-production.sh
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) else
if [ "$1" = "--logs" ] || [ "$2" = "--logs" ]; then log_info "Starte Backend-Server im Entwicklungsmodus..."
log_info "Zeige Backend-Logs (Strg+C zum Beenden):"
docker-compose -f docker-compose.backend.yml logs -f # 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 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"