Pārlūkot izejas kodu

Fix CORS configuration and HTTPS port 443 issue

- Add support for multiple CORS origins (comma-separated)
  * Updated src/app.js to parse comma-separated CORS_ORIGIN env variable
  * Updated src/server.js to support multiple origins for Socket.io WebSocket connections
  * Allows frontend apps from localhost, beta, and production to access the API

- Fix nginx configuration typo preventing HTTPS from working
  * Fixed 'body_size_sent' -> 'body_bytes_sent' in log_format
  * This typo was preventing nginx from starting with SSL configuration
  * Port 443 is now properly listening and accepting HTTPS connections

- Update docker-compose.prod.yml with default CORS origins
  * Set default CORS_ORIGIN to include all frontend environments:
    - http://localhost:3000 (local development)
    - https://beta-app.insightbull.io (beta/staging)
    - https://app.insightbull.io (production)

These changes enable:
- Frontend team to access API from all environments (localhost, beta, production)
- HTTPS connections to work properly (port 443)
- WebSocket connections to work from all allowed origins
- Proper CORS headers for cross-origin requests

Fixes connection issues reported by frontend team where:
- CORS was blocking localhost:3000 requests
- HTTPS port 443 was refusing connections
muhammad.uzair 2 mēneši atpakaļ
vecāks
revīzija
2dfc5f7ba6
4 mainītis faili ar 14 papildinājumiem un 4 dzēšanām
  1. 1 1
      docker-compose.prod.yml
  2. 1 1
      nginx/nginx.prod.conf
  3. 6 1
      src/app.js
  4. 6 1
      src/server.js

+ 1 - 1
docker-compose.prod.yml

@@ -40,7 +40,7 @@ services:
40 40
       - DB_NAME=${DB_NAME:-financial_data}
41 41
       - DB_USER=${DB_USER:-postgres}
42 42
       - DB_PASSWORD=${DB_PASSWORD}
43
-      - CORS_ORIGIN=${CORS_ORIGIN:-*}
43
+      - CORS_ORIGIN=${CORS_ORIGIN:-http://localhost:3000,https://beta-app.insightbull.io,https://app.insightbull.io}
44 44
       - JWT_SECRET=${JWT_SECRET}
45 45
       - LOG_LEVEL=${LOG_LEVEL:-info}
46 46
     # No volume mounts in production (code is baked into image)

+ 1 - 1
nginx/nginx.prod.conf

@@ -16,7 +16,7 @@ http {
16 16
     default_type application/octet-stream;
17 17
 
18 18
     log_format main '$remote_addr - $remote_user [$time_local] "$request" '
19
-                    '$status $body_size_sent "$http_referer" '
19
+                    '$status $body_bytes_sent "$http_referer" '
20 20
                     '"$http_user_agent" "$http_x_forwarded_for"';
21 21
 
22 22
     access_log /var/log/nginx/access.log main;

+ 6 - 1
src/app.js

@@ -21,8 +21,13 @@ const app = express();
21 21
 app.use(helmet());
22 22
 
23 23
 // CORS configuration
24
+// Support comma-separated origins for multiple frontend applications
25
+const allowedOrigins = process.env.CORS_ORIGIN 
26
+  ? process.env.CORS_ORIGIN.split(',').map(origin => origin.trim())
27
+  : '*';
28
+
24 29
 app.use(cors({
25
-  origin: process.env.CORS_ORIGIN || '*',
30
+  origin: allowedOrigins,
26 31
   credentials: true
27 32
 }));
28 33
 

+ 6 - 1
src/server.js

@@ -16,9 +16,14 @@ const startServer = async () => {
16 16
     const server = createServer(app);
17 17
 
18 18
     // Initialize Socket.io - Force WebSocket transport to prevent polling fallback
19
+    // Support comma-separated origins for multiple frontend applications
20
+    const allowedOrigins = process.env.CORS_ORIGIN 
21
+      ? process.env.CORS_ORIGIN.split(',').map(origin => origin.trim())
22
+      : "*";
23
+
19 24
     const io = new Server(server, {
20 25
       cors: {
21
-        origin: process.env.CORS_ORIGIN || "*",
26
+        origin: allowedOrigins,
22 27
         methods: ["GET", "POST"],
23 28
         credentials: true
24 29
       },