@@ -8,7 +8,7 @@ The FlexFit platform follows a **Master-Worker microservices pattern** with **Se
88
99### Core Infrastructure Services:
1010- ** Service Registry (Eureka Server)** : Service discovery and health monitoring
11- - ** API Gateway** : Single entry point, request routing, and load balancing
11+ - ** API Gateway** : Single entry point, request routing, load balancing, and ** centralized CORS handling **
1212- ** PostgreSQL Database** : Centralized data storage for all services
1313
1414### Business Logic Services:
@@ -57,6 +57,9 @@ CHAIR_API_KEY=your_chair_api_key_here
5757# -d flag runs in background (detached mode)
5858docker compose up --build -d
5959
60+ # API Gateway will automatically wait for services to register
61+ # No manual restart needed!
62+
6063# View logs for all services (live stream)
6164docker compose logs -f
6265
@@ -71,21 +74,36 @@ docker compose logs -f genai-workout-worker # AI Worker
7174docker compose down
7275```
7376
77+ ## 🌐 CORS Configuration
78+
79+ ** ✅ CORS is centrally managed at the API Gateway level** - this ensures consistent cross-origin handling across all microservices:
80+
81+ - ** Frontend Origin** : All requests from ` http://localhost:3001 ` (or any origin) are automatically allowed
82+ - ** No Duplicate Headers** : Backend services have CORS disabled to prevent conflicts
83+ - ** Global Policy** : API Gateway handles all CORS preflight and actual requests
84+ - ** Development Friendly** : Configured to allow all origins with ` allowedOriginPatterns("*") `
85+
86+ ### Supported Routes:
87+ - ✅ Direct routes: ` http://localhost:8000/api/v1/users/** `
88+ - ✅ Service discovery routes: ` http://localhost:8000/user-service/api/v1/users/** `
89+ - ✅ All HTTP methods: GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH
90+ - ✅ All headers allowed for development
91+
7492## 🐳 Docker Services
7593
7694### Service Overview
7795| Service | Port | Status | Description |
7896| ---------| ------| --------| -------------|
7997| ** Service Registry** | ` 8761 ` | ✅ Healthy | Eureka Server - Service discovery |
80- | ** API Gateway** | ` 8000 ` | ✅ Healthy | Spring Cloud Gateway - Request routing |
98+ | ** API Gateway** | ` 8000 ` | ✅ Healthy | Spring Cloud Gateway - Request routing + CORS |
8199| ** PostgreSQL** | ` 5432 ` | ✅ Healthy | Database server |
82100| ** User Service** | ` 8081 ` | ✅ Healthy | User management API |
83101| ** Workout Plan Service** | ` 8082 ` | ✅ Running | Workout planning API |
84102| ** GenAI Workout Worker** | ` 8083 ` | ✅ Healthy | AI workout generation |
85103
86104### Container Details
87105- ** Service Registry** : Spring Boot 3.5.0 with Netflix Eureka Server
88- - ** API Gateway** : Spring Boot 3.5.0 with Spring Cloud Gateway
106+ - ** API Gateway** : Spring Boot 3.5.0 with Spring Cloud Gateway + ** Reactive CORS Filter **
89107- ** Database** : ` postgres:16 ` with persistent storage and health checks
90108- ** User Service** : Spring Boot 3.5.0 with Eclipse Temurin JDK 21 + Eureka Client
91109- ** Workout Plan Service** : Spring Boot 3.5.0 with Eclipse Temurin JDK 21 + Eureka Client
@@ -99,6 +117,9 @@ docker compose down
99117# Rebuild and start all services
100118docker compose up --build -d
101119
120+ # Fix service discovery timing issues
121+ sleep 30 && docker compose restart api-gateway
122+
102123# View running containers
103124docker compose ps
104125
@@ -124,10 +145,11 @@ docker compose restart workout-plan-service
124145- ** Registered Services** : http://localhost:8761/eureka/apps
125146- ** Health Check** : http://localhost:8761/actuator/health
126147
127- #### API Gateway (Port 8000) - ** Single Entry Point**
148+ #### API Gateway (Port 8000) - ** Single Entry Point** + ** CORS Handler **
128149- ** Health Check** : http://localhost:8000/actuator/health
129150- ** Gateway Routes** : http://localhost:8000/actuator/gateway/routes
130- - ** User Service via Gateway** : http://localhost:8000/api/users/ **
151+ - ** User Service via Gateway** : http://localhost:8000/api/v1/users/ **
152+ - ** User Service (Discovery)** : http://localhost:8000/user-service/api/v1/users/ **
131153- ** Workout Service via Gateway** : http://localhost:8000/api/workout-plans/ **
132154
133155#### User Service (Port 8081) - ** Direct Access**
@@ -173,18 +195,33 @@ docker compose restart workout-plan-service
173195
174196### 📝 Example API Calls
175197
176- #### Register User
198+ #### Register User (with CORS)
177199``` bash
178- curl -X POST http://localhost:8081/api/v1/users/register \
200+ # Via API Gateway (recommended - includes CORS)
201+ curl -X POST http://localhost:8000/user-service/api/v1/users/register \
179202 -H " Content-Type: application/json" \
203+ -H " Origin: http://localhost:3001" \
180204 -d ' {
181205 "username": "johndoe",
182206 "email": "john@example.com",
183207 "password": "securePassword123",
208+ "firstName": "John",
209+ "lastName": "Doe",
210+ "dateOfBirth": "1990-01-15",
211+ "gender": "MALE"
212+ }'
213+
214+ # Direct access (no CORS headers)
215+ curl -X POST http://localhost:8081/api/v1/users/register \
216+ -H " Content-Type: application/json" \
217+ -d ' {
218+ "username": "johndoe",
219+ "email": "john@example.com",
220+ "password": "securePassword123",
221+ "firstName": "John",
222+ "lastName": "Doe",
184223 "dateOfBirth": "1990-01-15",
185- "gender": "MALE",
186- "heightCm": 180,
187- "weightKg": 75.0
224+ "gender": "MALE"
188225 }'
189226```
190227
@@ -273,7 +310,7 @@ team-code-compass/
273310### Service Communication
274311Services communicate through the ` flexfit-network ` Docker network with ** Service Discovery** :
275312- ** Service Registry (Eureka)** : ` service-registry:8761 ` - Central service discovery
276- - ** API Gateway** : ` api-gateway:8000 ` - Routes to registered services
313+ - ** API Gateway** : ` api-gateway:8000 ` - Routes to registered services + ** CORS handling **
277314- ** User Service → Database** : ` postgres:5432 `
278315- ** Workout Plan Service → Database** : ` postgres:5432 `
279316- ** Workout Plan Service → GenAI Worker** : ` flexfit-genai-workout-worker:8083 `
@@ -287,8 +324,28 @@ Services communicate through the `flexfit-network` Docker network with **Service
2873242 . ** Database connection** : Verify ` .env ` file exists and has correct credentials
2883253 . ** Container startup** : Check logs with ` docker compose logs <service-name> `
2893264 . ** Missing API key** : Ensure ` CHAIR_API_KEY ` is set in ` .env ` file
290- 5 . ** Service registration** : Services may take 30-60s to register with Eureka after startup
327+ 5 . ** Service registration timing ** : Services may take 30-60s to register with Eureka after startup
2913286 . ** Command not found** : Use ` docker compose ` (not ` docker-compose ` ) - V2 syntax
329+ 7 . ** CORS errors** : Use API Gateway routes instead of direct service access for frontend
330+ 8 . ** API Gateway startup** : Gateway automatically waits 45s for service registration - no manual intervention needed
331+
332+ ### ⚡ Service Discovery Timing Issue Fix
333+
334+ ** Problem** : API Gateway returns ` 503 Service Unavailable ` because services haven't registered with Eureka yet.
335+
336+ ** Solution** : API Gateway now has an ** internal startup delay** - it automatically waits 45 seconds before starting, allowing all services to register with Eureka.
337+
338+ ``` bash
339+ # Simply start everything - no manual steps needed
340+ docker compose up --build -d
341+
342+ # That's it! API Gateway waits internally for service registration
343+ ```
344+
345+ ** If you still see 503 errors:**
346+ 1 . Check if all services are healthy: ` docker compose ps `
347+ 2 . Check Eureka dashboard: http://localhost:8761
348+ 3 . Check API Gateway logs: ` docker compose logs api-gateway `
292349
293350### Service Status Check
294351``` bash
@@ -320,6 +377,24 @@ docker compose logs --tail=50 api-gateway
320377docker compose logs --tail=50 user-service
321378docker compose logs --tail=50 workout-plan-service
322379docker compose logs --tail=50 genai-workout-worker
380+
381+ # Check for CORS issues in API Gateway logs
382+ docker compose logs api-gateway | grep -i cors
383+ ```
384+
385+ ### CORS Troubleshooting
386+ ``` bash
387+ # Test CORS preflight request
388+ curl -v -X OPTIONS " http://localhost:8000/user-service/api/v1/users/register" \
389+ -H " Origin: http://localhost:3001" \
390+ -H " Access-Control-Request-Method: POST" \
391+ -H " Access-Control-Request-Headers: Content-Type"
392+
393+ # Should return:
394+ # Access-Control-Allow-Origin: http://localhost:3001
395+ # Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS,HEAD,PATCH
396+ # Access-Control-Allow-Headers: Content-Type
397+ # Access-Control-Max-Age: 3600
323398```
324399
325400### Build Issues Resolution
@@ -330,6 +405,9 @@ docker compose down
330405docker system prune -f
331406docker compose up --build -d
332407
408+ # API Gateway will automatically handle timing
409+ # No manual restart needed!
410+
333411# Check for compilation errors
334412docker compose logs workout-plan-service | grep ERROR
335413```
@@ -346,6 +424,8 @@ docker compose logs workout-plan-service | grep ERROR
346424- ** Health Monitoring** : Comprehensive health checks
347425- ** Security** : Development-mode security configuration
348426- ** Multi-stage Builds** : Optimized Docker images
427+ - ** ✅ CORS Support** : Centralized cross-origin handling at API Gateway
428+ - ** ✅ Service Discovery** : Automatic service registration and discovery
349429
350430### 🔧 Technical Stack
351431- ** Backend** : Spring Boot 3.5.0, Java 21
@@ -354,6 +434,8 @@ docker compose logs workout-plan-service | grep ERROR
354434- ** Containerization** : Docker & Docker Compose
355435- ** Documentation** : OpenAPI 3.0, Swagger UI
356436- ** Build Tools** : Maven, pip
437+ - ** Service Discovery** : Netflix Eureka
438+ - ** API Gateway** : Spring Cloud Gateway
357439
358440## 🚧 Future Enhancements
359441
@@ -366,23 +448,33 @@ docker compose logs workout-plan-service | grep ERROR
366448- [ ] CI/CD pipeline integration
367449- [ ] Kubernetes deployment manifests
368450- [ ] Monitoring and logging with ELK stack
451+ - [x] ✅ ** CORS configuration** (completed)
452+ - [x] ✅ ** Service discovery timing fix** (documented)
369453
370454## 🎯 Getting Started Checklist
371455
3724561 . ✅ Clone the repository
3734572 . ✅ Create ` .env ` file with required variables
3744583 . ✅ Run ` docker compose up --build -d ` (note: ` --build ` flag and ` -d ` for background)
375- 4 . ✅ Wait for services to start (30-60 seconds for full registration)
459+ 4 . ✅ ** Wait ~ 60 seconds ** for all services to start and register automatically
3764605 . ✅ Verify all services are healthy: ` docker compose ps `
3774616 . ✅ Check Service Registry: http://localhost:8761 (view registered services)
3784627 . ✅ Test API Gateway: http://localhost:8000/actuator/health
3794638 . ✅ Access Swagger UIs:
380464 - User Service: http://localhost:8081/swagger-ui/index.html
381465 - Workout Plan Service: http://localhost:8082/swagger-ui/index.html
3824669 . ✅ Test GenAI Worker: http://localhost:8083/health
383- 10 . ✅ Test API Gateway routing:
384- - ` curl http://localhost:8000/api/users/health `
385- - ` curl http://localhost:8000/api/workout-plans/health `
467+ 10 . ✅ ** Test CORS-enabled routes** (for frontend):
468+ - ` curl -H "Origin: http://localhost:3001" http://localhost:8000/user-service/api/v1/users/register `
469+ - ` curl -H "Origin: http://localhost:3001" http://localhost:8000/api/v1/users/register `
470+
471+ ### 🌟 Frontend Integration Ready!
472+ Your frontend at ` http://localhost:3001 ` can now make requests to:
473+ - ✅ ` http://localhost:8000/user-service/api/v1/users/register `
474+ - ✅ ` http://localhost:8000/api/v1/users/** `
475+ - ✅ All CORS preflight and actual requests supported
476+ - ✅ No duplicate CORS headers
477+ - ✅ Single, clean CORS policy managed at API Gateway
386478
387479---
388480
0 commit comments