Skip to content

Commit

Permalink
Merge pull request #52 from CS3219-AY2425S1/fix-cors
Browse files Browse the repository at this point in the history
fix cors for sockets
  • Loading branch information
guowei42 authored Nov 13, 2024
2 parents d9ad28a + dda0afc commit 2fd83a0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
29 changes: 10 additions & 19 deletions api-gateway/api.conf
Original file line number Diff line number Diff line change
Expand Up @@ -43,48 +43,39 @@ server {
}

location /matching {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;

proxy_pass http://matching_service/;

# socket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';

# forward headers
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "upgrade";
}

location /chat {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;

proxy_pass http://chat_service/;

# socket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';

# forward headers
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location /collaboration {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;

proxy_pass http://collab_service/;

# socket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';

# forward headers
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location / {
Expand Down
3 changes: 2 additions & 1 deletion server/ai-service/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ dotenv.config();
const port = process.env.PORT || 3005;

const corsOptions = {
origin: "*",
origin: ["http://localhost:3000", "https://peerprep-nine.vercel.app"],
methods: "GET, POST, DELETE, PUT, PATCH",
allowedHeaders:
"Origin, X-Requested-With, Content-Type, Accept, Authorization, Cookie",
credentials: true
};

app.use(cors(corsOptions));
Expand Down
10 changes: 6 additions & 4 deletions server/chat-service/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ const port = process.env.PORT || 3006; // Ensure this matches SVC_PORTS for chat
const server = http.createServer();
const io = new Server(server, {
cors: {
origin: "*",
origin: ["http://localhost:3000", "https://peerprep-nine.vercel.app"],
methods: "GET, POST, DELETE, PUT, PATCH",
allowedHeaders: "Origin, X-Requested-With, Content-Type, Accept, Authorization, Cookie"
}
allowedHeaders:
"Origin, X-Requested-With, Content-Type, Accept, Authorization, Cookie",
credentials: true,
},
});

io.on("connection", (socket) => {
Expand All @@ -27,7 +29,7 @@ io.on("connection", (socket) => {
const roomName = `${roomId}`;
console.log(`Message in room ${roomName}: ${message}`);
// Broadcast message to all users in the room
io.to(roomName).emit("receive_message", {senderId, message});
io.to(roomName).emit("receive_message", { senderId, message });
});

// Handle disconnection
Expand Down
3 changes: 2 additions & 1 deletion server/collaboration-service/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const port = process.env.PORT || 3004;
const server = http.createServer();
const io = new Server(server, {
cors: {
origin: "*",
origin: ["http://localhost:3000", "https://peerprep-nine.vercel.app"],
methods: "GET, POST, DELETE, PUT, PATCH",
allowedHeaders:
"Origin, X-Requested-With, Content-Type, Accept, Authorization, Cookie",
credentials: true
},
});

Expand Down
5 changes: 3 additions & 2 deletions server/matching-service/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ const redis_url = process.env.REDIS_URL;
const server = http.createServer();
const io = new Server(server, {
cors: {
origin: "*",
origin: ["http://localhost:3000", "https://peerprep-nine.vercel.app"],
methods: "GET, POST, DELETE, PUT, PATCH",
allowedHeaders: "Origin, X-Requested-With, Content-Type, Accept, Authorization, Cookie"
allowedHeaders: "Origin, X-Requested-With, Content-Type, Accept, Authorization, Cookie",
credentials: true
}
});
const redisClient = redis.createClient({
Expand Down
5 changes: 3 additions & 2 deletions server/question-service/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const questionsRoutes = require("./routes/questionRoute");
dotenv.config();

const corsOptions = {
origin: "*",
origin: ["http://localhost:3000", "https://peerprep-nine.vercel.app"],
methods: "GET, POST, DELETE, PUT, PATCH",
allowedHeaders: "Origin, X-Requested-With, Content-Type, Accept, Authorization, Cookie"
allowedHeaders: "Origin, X-Requested-With, Content-Type, Accept, Authorization, Cookie",
credentials: true
}

app.use(cors(corsOptions));
Expand Down

0 comments on commit 2fd83a0

Please sign in to comment.