# πŸ—οΈ MYP - Separate Server Architektur ## Übersicht Das MYP-System wurde in **zwei vollstΓ€ndig unabhΓ€ngige Server** aufgeteilt: - **🏭 Backend-Server** (Port 5000): Flask-API fΓΌr GeschΓ€ftslogik und Datenmanagement - **🎨 Frontend-Server** (Port 3000): Next.js-Anwendung fΓΌr BenutzeroberflΓ€che ## πŸ”— Server-Kommunikation ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” HTTP/API β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Frontend │◄───────────────►│ Backend β”‚ β”‚ (Next.js) β”‚ β”‚ (Flask) β”‚ β”‚ Port: 3000 β”‚ β”‚ Port: 5000 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ## πŸš€ Separate Server starten ### Backend-Server (unabhΓ€ngig) ```bash cd backend ./start-backend-server.sh # Alternative mit Logs ./start-backend-server.sh --logs # VollstΓ€ndige Neuinstallation ./start-backend-server.sh --clean ``` **Backend verfΓΌgbar unter:** - πŸ“‘ Backend-API: http://localhost:5000 - πŸ”§ Health-Check: http://localhost:5000/health - πŸ“‹ API-Docs: http://localhost:5000/swagger ### Frontend-Server (unabhΓ€ngig) ```bash cd frontend ./start-frontend-server.sh # Alternative mit Logs ./start-frontend-server.sh --logs # VollstΓ€ndige Neuinstallation ./start-frontend-server.sh --clean ``` **Frontend verfΓΌgbar unter:** - 🌐 Web-App: http://localhost:3000 - πŸ”§ Health-Check: http://localhost:3000/health - πŸ“¦ CDN-Assets: http://localhost:8080 ## βš™οΈ Konfiguration ### Backend-Konfiguration **Datei:** `backend/env.backend` ```bash # Backend-Server PORT=5000 BACKEND_URL=http://localhost:5000 # CORS fΓΌr Frontend-Zugriff CORS_ORIGINS=http://localhost:3000,https://frontend.myp.local # Datenbank DATABASE_PATH=instance/myp.db # Cache REDIS_HOST=localhost REDIS_PORT=6379 ``` ### Frontend-Konfiguration **Datei:** `frontend/env.frontend` ```bash # Frontend-Server PORT=3000 FRONTEND_URL=http://localhost:3000 # Backend-API Verbindung BACKEND_API_URL=http://localhost:5000/api NEXT_PUBLIC_API_URL=http://localhost:5000/api # Cache FRONTEND_REDIS_PORT=6380 ``` ## πŸ‹ Docker-Container ### Backend-Container ```bash cd backend docker-compose -f docker-compose.backend.yml up -d # Services: # - myp-backend-standalone (Port 5000) # - myp-backend-db (PostgreSQL, Port 5432) # - myp-backend-cache (Redis, Port 6379) ``` ### Frontend-Container ```bash cd frontend docker-compose -f docker-compose.frontend.yml up -d # Services: # - myp-frontend-standalone (Port 3000) # - myp-frontend-cache (Redis, Port 6380) # - myp-frontend-cdn (Nginx, Port 8080) ``` ## πŸ”’ Sicherheit ### CORS-Konfiguration Das Backend ist fΓΌr **explizite Frontend-Origins** konfiguriert: ```python # Backend: app.py CORS(app, origins=['http://localhost:3000', 'https://frontend.myp.local'], supports_credentials=True, allow_headers=['Content-Type', 'Authorization', 'X-Requested-With'], methods=['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']) ``` ### API-Authentifizierung - **JWT-Tokens** fΓΌr API-Zugriff - **Session-basierte** Authentifizierung fΓΌr Web-UI - **Separate Secrets** fΓΌr Frontend und Backend ## πŸ“Š Monitoring ### Health-Checks **Backend:** GET http://localhost:5000/health ```json { "status": "healthy", "service": "myp-backend", "database": "connected", "timestamp": "2024-01-15T10:30:00Z" } ``` **Frontend:** GET http://localhost:3000/health ```json { "status": "healthy", "service": "myp-frontend", "backend": { "url": "http://localhost:5000", "status": "connected" }, "timestamp": "2024-01-15T10:30:00Z" } ``` ### Logs verfolgen ```bash # Backend-Logs cd backend docker-compose -f docker-compose.backend.yml logs -f # Frontend-Logs cd frontend docker-compose -f docker-compose.frontend.yml logs -f ``` ## πŸ”§ Troubleshooting ### Backend startet nicht ```bash # 1. PrΓΌfe Ports netstat -an | grep 5000 # 2. PrΓΌfe Backend-Logs cd backend docker-compose -f docker-compose.backend.yml logs backend # 3. Datenbank-Migration docker-compose -f docker-compose.backend.yml exec backend flask db upgrade ``` ### Frontend kann Backend nicht erreichen ```bash # 1. PrΓΌfe Backend-VerfΓΌgbarkeit curl http://localhost:5000/health # 2. PrΓΌfe CORS-Konfiguration curl -H "Origin: http://localhost:3000" \ -H "Access-Control-Request-Method: GET" \ -H "Access-Control-Request-Headers: Content-Type" \ -X OPTIONS http://localhost:5000/api/printers # 3. PrΓΌfe Frontend-Umgebungsvariablen cd frontend grep BACKEND env.frontend ``` ### Port-Konflikte ```bash # Standard-Ports Γ€ndern # Backend: PORT=5001 in env.backend # Frontend: PORT=3001 in env.frontend # Neue URLs in beiden Konfigurationen anpassen ``` ## 🌐 Deployment ### Produktions-Deployment ```bash # Backend (Server 1) cd backend FLASK_ENV=production ./start-backend-server.sh # Frontend (Server 2) cd frontend NODE_ENV=production BACKEND_API_URL=https://api.myp.de ./start-frontend-server.sh ``` ### Reverse Proxy (Optional) FΓΌr Produktion kΓΆnnen beide Server hinter einem Reverse Proxy laufen: ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Client │────►│ Nginx/Caddy │────►│ Frontend β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β–Ίβ”‚ :3000 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ /api/* β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”Œβ”€β–Ίβ”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ /api/* β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ Backend β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ :5000 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ## πŸ“ Dateistruktur ``` Projektarbeit-MYP/ β”œβ”€β”€ backend/ # 🏭 Backend-Server β”‚ β”œβ”€β”€ docker-compose.backend.yml # Docker-Konfiguration β”‚ β”œβ”€β”€ start-backend-server.sh # Start-Skript β”‚ β”œβ”€β”€ env.backend # Umgebungsvariablen β”‚ └── app.py # Flask-Anwendung β”œβ”€β”€ frontend/ # 🎨 Frontend-Server β”‚ β”œβ”€β”€ docker-compose.frontend.yml # Docker-Konfiguration β”‚ β”œβ”€β”€ start-frontend-server.sh # Start-Skript β”‚ β”œβ”€β”€ env.frontend # Umgebungsvariablen β”‚ └── src/ # Next.js-Anwendung └── SEPARATE_SERVERS_GUIDE.md # Diese Dokumentation ``` ## βœ… Vorteile der Trennung 1. **πŸ”„ UnabhΓ€ngige Skalierung** - Frontend und Backend kΓΆnnen getrennt skaliert werden 2. **πŸš€ Separate Deployments** - Updates kΓΆnnen unabhΓ€ngig deployed werden 3. **πŸ›‘οΈ Verbesserte Sicherheit** - Klare API-Grenzen und CORS-Kontrolle 4. **πŸ”§ Technologie-FlexibilitΓ€t** - Frontend und Backend kΓΆnnen verschiedene Technologien verwenden 5. **πŸ“Š Besseres Monitoring** - Separate Health-Checks und Logs 6. **πŸ—οΈ Microservice-Ready** - Vorbereitung fΓΌr Microservice-Architektur --- **Hinweis:** Die alte gekoppelte Konfiguration ist weiterhin in `docker-compose.yml` verfΓΌgbar, wird aber fΓΌr neue Deployments nicht empfohlen.