"feat: Add Separate Servers Guide and update installation scripts"
This commit is contained in:
parent
9fe529247b
commit
72230c342d
1
SEPARATE_SERVERS_GUIDE.md
Normal file
1
SEPARATE_SERVERS_GUIDE.md
Normal file
@ -0,0 +1 @@
|
||||
|
@ -183,9 +183,15 @@ try {
|
||||
$env:FLASK_APP = "app.py"
|
||||
$env:FLASK_ENV = $InstallMode
|
||||
|
||||
$PythonCode = "from app import create_app, init_db; app = create_app('$InstallMode'); app.app_context().push(); init_db(); print('Datenbank initialisiert')"
|
||||
python -c $PythonCode
|
||||
Write-Success "Datenbank erfolgreich initialisiert"
|
||||
# Verwende das Test-Skript für die Datenbank-Initialisierung
|
||||
Write-Log "Führe Datenbank-Initialisierung über Test-Skript aus..."
|
||||
python test-backend-setup.py
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Success "Datenbank erfolgreich initialisiert"
|
||||
} else {
|
||||
Write-Warning "Datenbank-Initialisierung mit Warnungen abgeschlossen"
|
||||
}
|
||||
} catch {
|
||||
Write-Error "Fehler bei der Datenbank-Initialisierung: $_"
|
||||
exit 1
|
||||
|
337
backend/start-backend-server.ps1
Normal file
337
backend/start-backend-server.ps1
Normal file
@ -0,0 +1,337 @@
|
||||
# MYP Backend - Windows PowerShell Server Start
|
||||
# Startet den Backend-Server vollständig unabhängig vom Frontend
|
||||
|
||||
param(
|
||||
[switch]$Production,
|
||||
[switch]$Development,
|
||||
[switch]$Logs,
|
||||
[switch]$Help
|
||||
)
|
||||
|
||||
# Farben für PowerShell
|
||||
$Red = "Red"
|
||||
$Green = "Green"
|
||||
$Yellow = "Yellow"
|
||||
$Blue = "Cyan"
|
||||
|
||||
function Write-Log {
|
||||
param([string]$Message, [string]$Color = "White")
|
||||
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
Write-Host "[$timestamp] $Message" -ForegroundColor $Color
|
||||
}
|
||||
|
||||
function Write-Success {
|
||||
param([string]$Message)
|
||||
Write-Log "SUCCESS: $Message" -Color $Green
|
||||
}
|
||||
|
||||
function Write-Warning {
|
||||
param([string]$Message)
|
||||
Write-Log "WARNING: $Message" -Color $Yellow
|
||||
}
|
||||
|
||||
function Write-Error {
|
||||
param([string]$Message)
|
||||
Write-Log "FEHLER: $Message" -Color $Red
|
||||
}
|
||||
|
||||
# Banner
|
||||
Write-Host "========================================" -ForegroundColor $Blue
|
||||
Write-Host "🏭 MYP Backend - Standalone Server Start" -ForegroundColor $Blue
|
||||
Write-Host "========================================" -ForegroundColor $Blue
|
||||
|
||||
if ($Help) {
|
||||
Write-Host @"
|
||||
Verwendung: .\start-backend-server.ps1 [OPTIONEN]
|
||||
|
||||
OPTIONEN:
|
||||
-Production Produktionsmodus (Gunicorn)
|
||||
-Development Entwicklungsmodus (Flask Dev Server)
|
||||
-Logs Zeige Live-Logs
|
||||
-Help Zeige diese Hilfe
|
||||
|
||||
BEISPIELE:
|
||||
.\start-backend-server.ps1 -Development
|
||||
.\start-backend-server.ps1 -Production
|
||||
.\start-backend-server.ps1 -Development -Logs
|
||||
"@
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Bestimme Ausführungsmodus
|
||||
$RunMode = "development"
|
||||
if ($Production) {
|
||||
$RunMode = "production"
|
||||
Write-Log "🏭 Produktionsmodus aktiviert" -Color $Blue
|
||||
} elseif ($Development) {
|
||||
$RunMode = "development"
|
||||
Write-Log "🔧 Entwicklungsmodus aktiviert" -Color $Blue
|
||||
} else {
|
||||
$RunMode = "development"
|
||||
Write-Log "🔧 Standard-Entwicklungsmodus aktiviert" -Color $Blue
|
||||
}
|
||||
|
||||
# Arbeitsverzeichnis prüfen
|
||||
$CurrentDir = Get-Location
|
||||
Write-Log "Arbeitsverzeichnis: $CurrentDir"
|
||||
|
||||
if (-not (Test-Path "app.py")) {
|
||||
Write-Error "app.py nicht gefunden! Bitte im Backend-Verzeichnis ausführen."
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Python-Installation prüfen
|
||||
Write-Log "🐍 Prüfe Python-Installation..." -Color $Blue
|
||||
|
||||
try {
|
||||
$PythonVersion = python --version 2>&1
|
||||
Write-Log "Python-Version: $PythonVersion"
|
||||
Write-Success "Python-Installation verifiziert"
|
||||
} catch {
|
||||
Write-Error "Python ist nicht installiert oder nicht im PATH!"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Umgebungsvariablen laden
|
||||
if (Test-Path "env.backend") {
|
||||
Write-Log "⚙️ Lade Backend-Umgebungsvariablen..." -Color $Blue
|
||||
|
||||
$EnvContent = Get-Content "env.backend"
|
||||
foreach ($Line in $EnvContent) {
|
||||
if ($Line -match "^([^#][^=]+)=(.*)$") {
|
||||
$Key = $Matches[1].Trim()
|
||||
$Value = $Matches[2].Trim()
|
||||
[Environment]::SetEnvironmentVariable($Key, $Value, "Process")
|
||||
}
|
||||
}
|
||||
|
||||
# Überschreibe FLASK_ENV mit dem gewählten Modus
|
||||
$env:FLASK_ENV = $RunMode
|
||||
Write-Success "Umgebungsvariablen geladen"
|
||||
} else {
|
||||
Write-Warning "env.backend nicht gefunden"
|
||||
}
|
||||
|
||||
# Notwendige Verzeichnisse erstellen
|
||||
Write-Log "📁 Prüfe Verzeichnisse..." -Color $Blue
|
||||
|
||||
$Directories = @("instance", "logs", "uploads")
|
||||
foreach ($Dir in $Directories) {
|
||||
if (-not (Test-Path $Dir)) {
|
||||
New-Item -ItemType Directory -Path $Dir | Out-Null
|
||||
Write-Log "Verzeichnis erstellt: $Dir"
|
||||
}
|
||||
}
|
||||
|
||||
# Dependencies prüfen
|
||||
Write-Log "📦 Prüfe Python-Dependencies..." -Color $Blue
|
||||
|
||||
if (Test-Path "requirements.txt") {
|
||||
try {
|
||||
pip install -r requirements.txt --quiet
|
||||
Write-Success "Dependencies aktualisiert"
|
||||
} catch {
|
||||
Write-Warning "Fehler beim Aktualisieren der Dependencies: $_"
|
||||
}
|
||||
} else {
|
||||
Write-Warning "requirements.txt nicht gefunden"
|
||||
}
|
||||
|
||||
# Datenbank initialisieren
|
||||
Write-Log "🗄️ Initialisiere Datenbank..." -Color $Blue
|
||||
|
||||
try {
|
||||
$env:FLASK_APP = "app.py"
|
||||
|
||||
# Erstelle temporäre Python-Datei für Datenbank-Initialisierung
|
||||
$TempInitFile = "temp_init.py"
|
||||
$InitCode = @"
|
||||
from app import create_app, init_db
|
||||
app = create_app('$RunMode')
|
||||
with app.app_context():
|
||||
init_db()
|
||||
print('✅ Datenbank initialisiert')
|
||||
"@
|
||||
|
||||
$InitCode | Out-File -FilePath $TempInitFile -Encoding UTF8
|
||||
python $TempInitFile
|
||||
Remove-Item $TempInitFile -Force
|
||||
|
||||
Write-Success "Datenbank initialisiert"
|
||||
} catch {
|
||||
Write-Error "Fehler bei der Datenbank-Initialisierung: $_"
|
||||
if (Test-Path $TempInitFile) {
|
||||
Remove-Item $TempInitFile -Force
|
||||
}
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Port prüfen
|
||||
$Port = 5000
|
||||
if ($env:PORT) {
|
||||
$Port = [int]$env:PORT
|
||||
}
|
||||
|
||||
$PortInUse = Get-NetTCPConnection -LocalPort $Port -ErrorAction SilentlyContinue
|
||||
if ($PortInUse) {
|
||||
Write-Warning "Port $Port ist bereits belegt!"
|
||||
$Port = 5001
|
||||
Write-Log "Verwende alternativen Port $Port..."
|
||||
}
|
||||
|
||||
# Server starten basierend auf Modus
|
||||
if ($RunMode -eq "production") {
|
||||
Write-Log "🏭 Starte Backend-Server im Produktionsmodus..." -Color $Blue
|
||||
|
||||
# Prüfe Gunicorn
|
||||
try {
|
||||
gunicorn --version | Out-Null
|
||||
Write-Log "Verwende Gunicorn für Produktionsbetrieb"
|
||||
} catch {
|
||||
Write-Error "Gunicorn ist nicht installiert! Installiere mit: pip install gunicorn"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Gunicorn Konfiguration
|
||||
$Workers = if ($env:WORKERS) { $env:WORKERS } else { 4 }
|
||||
$BindAddress = if ($env:BIND_ADDRESS) { $env:BIND_ADDRESS } else { "0.0.0.0:$Port" }
|
||||
$Timeout = if ($env:TIMEOUT) { $env:TIMEOUT } else { 30 }
|
||||
|
||||
Write-Log "Backend-Server startet auf $BindAddress mit $Workers Workern..."
|
||||
|
||||
# Starte Gunicorn
|
||||
$GunicornCmd = "gunicorn --bind $BindAddress --workers $Workers --timeout $Timeout wsgi:application"
|
||||
|
||||
if ($Logs) {
|
||||
Write-Log "Starte mit Live-Logs..." -Color $Blue
|
||||
Invoke-Expression $GunicornCmd
|
||||
} else {
|
||||
Write-Log "Starte im Hintergrund..." -Color $Blue
|
||||
Start-Process -FilePath "gunicorn" -ArgumentList "--bind", $BindAddress, "--workers", $Workers, "--timeout", $Timeout, "wsgi:application" -NoNewWindow
|
||||
|
||||
# Warte auf Server-Start
|
||||
Write-Log "Warte auf Backend-Service..." -Color $Blue
|
||||
$Counter = 0
|
||||
$Timeout = 60
|
||||
|
||||
do {
|
||||
Start-Sleep -Seconds 1
|
||||
$Counter++
|
||||
|
||||
try {
|
||||
$Response = Invoke-WebRequest -Uri "http://localhost:$Port/monitoring/health/simple" -Method GET -TimeoutSec 5 -UseBasicParsing
|
||||
if ($Response.StatusCode -eq 200) {
|
||||
Write-Success "Backend-Server ist bereit!"
|
||||
break
|
||||
}
|
||||
} catch {
|
||||
# Ignoriere Fehler während der Startphase
|
||||
}
|
||||
|
||||
if ($Counter % 10 -eq 0) {
|
||||
Write-Log "Warte auf Backend-Service... ($Counter/$Timeout Sekunden)"
|
||||
}
|
||||
} while ($Counter -lt $Timeout)
|
||||
|
||||
if ($Counter -eq $Timeout) {
|
||||
Write-Error "Backend-Service konnte nicht gestartet werden!"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
Write-Log "🔧 Starte Backend-Server im Entwicklungsmodus..." -Color $Blue
|
||||
|
||||
# Flask-Entwicklungsserver
|
||||
$env:FLASK_APP = "app.py"
|
||||
$env:FLASK_ENV = "development"
|
||||
$env:FLASK_DEBUG = "1"
|
||||
|
||||
Write-Log "Backend-Server startet auf Port $Port..."
|
||||
|
||||
if ($Logs) {
|
||||
Write-Log "Starte mit Live-Logs..." -Color $Blue
|
||||
python -m flask run --host=0.0.0.0 --port=$Port
|
||||
} else {
|
||||
Write-Log "Starte im Hintergrund..." -Color $Blue
|
||||
$FlaskProcess = Start-Process -FilePath "python" -ArgumentList "-m", "flask", "run", "--host=0.0.0.0", "--port=$Port" -NoNewWindow -PassThru
|
||||
|
||||
# Warte auf Server-Start
|
||||
Write-Log "Warte auf Backend-Service..." -Color $Blue
|
||||
$Counter = 0
|
||||
$Timeout = 60
|
||||
|
||||
do {
|
||||
Start-Sleep -Seconds 1
|
||||
$Counter++
|
||||
|
||||
try {
|
||||
$Response = Invoke-WebRequest -Uri "http://localhost:$Port/monitoring/health/simple" -Method GET -TimeoutSec 5 -UseBasicParsing
|
||||
if ($Response.StatusCode -eq 200) {
|
||||
Write-Success "Backend-Server ist bereit!"
|
||||
break
|
||||
}
|
||||
} catch {
|
||||
# Ignoriere Fehler während der Startphase
|
||||
}
|
||||
|
||||
if ($Counter % 10 -eq 0) {
|
||||
Write-Log "Warte auf Backend-Service... ($Counter/$Timeout Sekunden)"
|
||||
}
|
||||
} while ($Counter -lt $Timeout)
|
||||
|
||||
if ($Counter -eq $Timeout) {
|
||||
Write-Error "Backend-Service konnte nicht gestartet werden!"
|
||||
if ($FlaskProcess -and !$FlaskProcess.HasExited) {
|
||||
$FlaskProcess.Kill()
|
||||
}
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# URLs anzeigen
|
||||
Write-Host ""
|
||||
Write-Success "🎉 Backend-Server erfolgreich gestartet!"
|
||||
Write-Host ""
|
||||
Write-Host "📡 Backend-API: http://localhost:$Port" -ForegroundColor $Green
|
||||
Write-Host "🔧 Backend-Health: http://localhost:$Port/monitoring/health/simple" -ForegroundColor $Green
|
||||
Write-Host "📋 Backend-Test: http://localhost:$Port/api/test" -ForegroundColor $Green
|
||||
Write-Host ""
|
||||
|
||||
if ($RunMode -eq "development") {
|
||||
Write-Host "🔧 Entwicklungsmodus aktiv:" -ForegroundColor $Blue
|
||||
Write-Host "- Debug-Modus ist aktiviert" -ForegroundColor $White
|
||||
Write-Host "- Automatisches Neuladen bei Änderungen" -ForegroundColor $White
|
||||
Write-Host "- Detaillierte Fehlermeldungen" -ForegroundColor $White
|
||||
} else {
|
||||
Write-Host "🏭 Produktionsmodus aktiv:" -ForegroundColor $Blue
|
||||
Write-Host "- Gunicorn mit $Workers Workern" -ForegroundColor $White
|
||||
Write-Host "- Optimiert für Performance" -ForegroundColor $White
|
||||
Write-Host "- Logging aktiviert" -ForegroundColor $White
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
|
||||
# Logs anzeigen (optional)
|
||||
if ($Logs -and $RunMode -eq "development") {
|
||||
Write-Log "Zeige Backend-Logs (Strg+C zum Beenden):" -Color $Blue
|
||||
if (Test-Path "logs\myp.log") {
|
||||
Get-Content "logs\myp.log" -Wait
|
||||
} else {
|
||||
Write-Warning "Keine Log-Datei gefunden"
|
||||
}
|
||||
}
|
||||
|
||||
if (!$Logs) {
|
||||
Write-Log "Verwende Strg+C um den Server zu stoppen" -Color $Blue
|
||||
|
||||
# Warte auf Benutzereingabe
|
||||
try {
|
||||
while ($true) {
|
||||
Start-Sleep -Seconds 1
|
||||
}
|
||||
} catch {
|
||||
Write-Log "Server wird beendet..." -Color $Yellow
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user