Skip to content

Commit de3eb3a

Browse files
committed
fix: Make Socket.io optional and fix CORS for Azure
1 parent 668eded commit de3eb3a

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

backend/server.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
// FlowOps Backend Server - v1.0.4 (Real-time features)
1+
// FlowOps Backend Server - v1.0.5 (Azure fix)
22
const express = require('express');
33
const http = require('http');
44
const dotenv = require('dotenv');
55
const cors = require('cors');
66
const morgan = require('morgan');
77
const path = require('path');
8-
const swaggerUi = require('swagger-ui-express');
9-
const swaggerSpec = require('./config/swagger');
108
const connectDB = require('./config/db');
11-
const { initializeSocket } = require('./config/socket');
129
const errorHandler = require('./middleware/errorHandler');
1310
const { apiLimiter } = require('./middleware/rateLimit');
1411

@@ -21,25 +18,27 @@ connectDB();
2118
const app = express();
2219
const server = http.createServer(app);
2320

24-
// Initialize Socket.io
25-
initializeSocket(server);
21+
// Initialize Socket.io only if enabled
22+
if (process.env.ENABLE_WEBSOCKETS === 'true') {
23+
try {
24+
const { initializeSocket } = require('./config/socket');
25+
initializeSocket(server);
26+
console.log('Socket.io enabled');
27+
} catch (err) {
28+
console.error('Socket.io initialization failed:', err.message);
29+
}
30+
}
2631

2732
// Body parser
2833
app.use(express.json());
2934

30-
// Enable CORS with specific origins
31-
const corsOptions = {
32-
origin: [
33-
'http://localhost:5173',
34-
'http://localhost:3000',
35-
'https://flowops-frontend.azurewebsites.net',
36-
process.env.FRONTEND_URL
37-
].filter(Boolean),
35+
// Enable CORS - allow all origins for Azure compatibility
36+
app.use(cors({
37+
origin: true,
3838
credentials: true,
39-
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
40-
allowedHeaders: ['Content-Type', 'Authorization']
41-
};
42-
app.use(cors(corsOptions));
39+
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'],
40+
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With']
41+
}));
4342

4443
// Apply rate limiting to all API routes
4544
app.use('/api', apiLimiter);
@@ -52,11 +51,17 @@ if (process.env.NODE_ENV === 'development') {
5251
app.use(morgan('dev'));
5352
}
5453

55-
// API Documentation
56-
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, {
57-
customCss: '.swagger-ui .topbar { display: none }',
58-
customSiteTitle: 'FlowOps API Documentation'
59-
}));
54+
// Swagger API Documentation (optional in production)
55+
try {
56+
const swaggerUi = require('swagger-ui-express');
57+
const swaggerSpec = require('./config/swagger');
58+
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, {
59+
customCss: '.swagger-ui .topbar { display: none }',
60+
customSiteTitle: 'FlowOps API Documentation'
61+
}));
62+
} catch (err) {
63+
console.log('Swagger documentation not available');
64+
}
6065

6166
// Health check
6267
app.get('/health', (req, res) => {
@@ -85,6 +90,5 @@ app.use(errorHandler);
8590
const PORT = process.env.PORT || 3001;
8691

8792
server.listen(PORT, () => {
88-
console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`);
89-
console.log(`Socket.io enabled for real-time updates`);
93+
console.log(`Server running in ${process.env.NODE_ENV || 'production'} mode on port ${PORT}`);
9094
});

0 commit comments

Comments
 (0)