Skip to content

Commit 73a9287

Browse files
committed
Merge remote-tracking branch 'origin/post-ms6' into collab-post-ms6
2 parents e41e20f + c4a2387 commit 73a9287

File tree

19 files changed

+205
-76
lines changed

19 files changed

+205
-76
lines changed

collab/main.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
verify "collab/verify"
5+
"context"
56
"encoding/json"
67
"io"
78
"log"
@@ -226,7 +227,7 @@ func handleMessages(
226227
log.Println("Authentication failed - no token attached")
227228
client.conn.WriteMessage(
228229
websocket.TextMessage,
229-
[]byte("Authentication failed - no JWT token"),
230+
[]byte("Authentication failed"),
230231
)
231232
client.conn.Close()
232233
break
@@ -248,12 +249,16 @@ func handleMessages(
248249
if !isSuccess {
249250
client.conn.WriteMessage(
250251
websocket.TextMessage,
251-
[]byte("Authentication failed - failed to find a matching room"),
252+
[]byte("Authentication failed"),
252253
)
253254
client.conn.Close()
254255
break
255256
}
256257
client.authenticated = true
258+
client.conn.WriteMessage(
259+
websocket.TextMessage,
260+
[]byte("Auth Success"),
261+
)
257262
log.Println("Client authenticated successfully")
258263
}
259264
// i can comment out this whole block and it would work
@@ -277,6 +282,20 @@ func handleMessages(
277282
RoomID: client.roomID,
278283
Content: []byte("The session has been closed by a user."),
279284
}
285+
targetId := msgData["userId"].(string)
286+
data, err := persistMappings.Conn.HGetAll(context.Background(), targetId).Result()
287+
if err != nil {
288+
log.Printf("Error retrieving data for userID %s: %v", targetId, err)
289+
} else {
290+
_, err1 := persistMappings.Conn.Del(context.Background(), targetId).Result()
291+
if err1 != nil {
292+
log.Printf("Error deleting data for userID %s: %v", targetId, err1);
293+
}
294+
_, err2 := persistMappings.Conn.Del(context.Background(), data["otherUser"]).Result()
295+
if err2 != nil {
296+
log.Printf("Error deleting data for other user %s: %v", data["otherUser"], err2);
297+
}
298+
}
280299
hub.broadcast <- closeMessage
281300
}
282301

collab/verify/roomMappings.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,9 @@ func VerifyRoomAndMoveToPersist(
4444

4545
roomMappings.Conn.Del(ctx, matchHash);
4646
persistentRoom := map[string]interface{}{
47-
"roomId": roomID,
48-
"otherUser": data["otherUser"],
49-
"requestTime": data["requestTime"],
50-
51-
"title": data["title"],
52-
"titleSlug": data["titleSlug"],
53-
"difficulty": data["difficulty"],
54-
"topicTags": data["topicTags"],
55-
"content": data["content"],
56-
"schemas": data["schemas"],
57-
"id": data["id"],
47+
"roomId": roomID,
48+
"otherUser": data["otherUser"],
49+
"requestTime": data["requestTime"],
5850
}
5951

6052
// this always overrides the persistent room

comms/server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
const express = require("express");
22
const http = require("http");
33
const app = express();
4+
45
const server = http.createServer(app);
56
const io = require("socket.io")(server, {
7+
path: '/comms',
68
cors: {
79
// temporarily use * to allow all origins
8-
origin: `*`
10+
origin: `*`,
11+
methods: ["GET", "POST"]
912
}
1013
});
1114

compose.yaml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,27 @@ services:
165165
nginx:
166166
build: nginx
167167
image: distractedcat/nginx
168-
ports:
169-
- "80:80"
170168
volumes:
171-
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
169+
- ./nginx/nginx.conf:/etc/nginx/internal.conf
170+
ports:
171+
- "70:70"
172172
depends_on:
173173
- peerprep
174174
- backend
175175
- user-service
176176
- storage-blob-api
177177
- matching-service-api
178+
179+
inbound-gateway:
180+
build: inbound-gateway
181+
image: wzwren/inbound-gateway
182+
ports:
183+
- "80:80"
184+
volumes:
185+
- ./inbound-gateway/nginx.conf:/etc/nginx/external.conf
186+
depends_on:
187+
- peerprep
188+
- comms
178189
# mongo:
179190
# image: "mongo:latest"
180191
# ports:

inbound-gateway/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM nginx:alpine
2+
COPY nginx.conf /etc/nginx/external.conf
3+
EXPOSE 80
4+
CMD ["nginx", "-c", "external.conf", "-g", "daemon off;"]

inbound-gateway/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
nginx is dockerised, just have to run `docker compose up --build` as usual.
2+
3+
if edits are made to the local nginx.conf file, following command must be run to see changes reflected in docker:
4+
5+
`docker exec cs3219-ay2425s1-project-g14-nginx-1 nginx -s reload`
6+
7+
(or just exec `nginx -s reload` in the container directly)

inbound-gateway/nginx.conf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
worker_processes 1;
2+
3+
events {
4+
worker_connections 1024;
5+
}
6+
7+
http {
8+
include mime.types;
9+
10+
default_type application/octet-stream;
11+
12+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
13+
'$status $body_bytes_sent "$http_referer" '
14+
'"$http_user_agent" "$http_x_forwarded_for"';
15+
16+
sendfile on;
17+
#tcp_nopush on;
18+
19+
keepalive_timeout 65;
20+
21+
upstream peerprep {
22+
server peerprep:3000;
23+
}
24+
25+
server {
26+
listen 80;
27+
28+
location / {
29+
proxy_pass http://peerprep/;
30+
proxy_set_header Host $host;
31+
proxy_set_header X-Real-IP $remote_addr;
32+
proxy_set_header X-Forwarded-Proto $scheme;
33+
proxy_set_header X-Forwarded-Host $host; # port 80 implicitly removes this port
34+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
35+
proxy_http_version 1.1;
36+
proxy_set_header Upgrade $http_upgrade;
37+
proxy_set_header Connection "upgrade";
38+
proxy_read_timeout 86400;
39+
}
40+
41+
location /comms/ {
42+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
43+
proxy_set_header X-Real-IP $remote_addr;
44+
proxy_set_header Host $host;
45+
proxy_set_header X-NginX-Proxy false;
46+
47+
48+
proxy_pass http://comms:4001;
49+
50+
proxy_http_version 1.1;
51+
proxy_set_header Upgrade $http_upgrade;
52+
proxy_set_header Connection "upgrade";
53+
proxy_read_timeout 86400;
54+
}
55+
}
56+
}

nginx/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
FROM nginx:alpine
2-
COPY nginx.conf /etc/nginx/nginx.conf
3-
EXPOSE 80
4-
CMD ["nginx", "-g", "daemon off;"]
2+
COPY nginx.conf /etc/nginx/internal.conf
3+
EXPOSE 70
4+
CMD ["nginx", "-c", "internal.conf", "-g", "daemon off;"]

nginx/nginx.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ http {
5353
# }
5454

5555
server {
56-
listen 80;
56+
listen 70;
5757
location / {
5858
proxy_pass http://peerprep/;
5959
proxy_set_header Host $host;
@@ -63,6 +63,7 @@ http {
6363
proxy_http_version 1.1;
6464
proxy_set_header Upgrade $http_upgrade;
6565
proxy_set_header Connection "upgrade";
66+
proxy_read_timeout 86400;
6667
}
6768

6869
location /users/ {

peerprep/app/actions/server_actions.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ export async function signup(state: FormState, formData: FormData) {
3636
redirect("/auth/login");
3737
} else {
3838
// TODO: handle failure codes: 400, 409, 500.
39-
console.log(`${json.status}: ${json.error}`);
39+
console.log(`Error in signup: ${json.status}: ${json.error}`);
40+
return {
41+
errors: {
42+
username: ["Username is already in use."],
43+
email: ["Email is already in use."],
44+
},
45+
};
4046
}
4147
}
4248

@@ -59,7 +65,23 @@ export async function login(state: FormState, formData: FormData) {
5965
await createSession(json.data);
6066
redirect("/questions");
6167
} else {
62-
console.log("Get session login error: " + json.error + " : " + json.status);
68+
if (json.status === 401) {
69+
return {
70+
errors: {
71+
email: ["Invalid email or password."],
72+
},
73+
};
74+
} else if (json.status === 500) {
75+
console.log(
76+
"Get session login error: " + json.error + " : " + json.status,
77+
);
78+
79+
return {
80+
errors: {
81+
email: ["Please try again."],
82+
},
83+
};
84+
}
6385
}
6486
}
6587

@@ -73,7 +95,7 @@ export async function hydrateUid(): Promise<null | UserData> {
7395
if (isError(json)) {
7496
console.log("Failed to fetch user ID.");
7597
console.log(`Error ${json.status}: ${json.error}`);
76-
redirect("/api/internal/auth/expire");
98+
redirect("/auth/logout");
7799
}
78100
// TODO: handle error handling
79101
const response = json as UserServiceResponse;

0 commit comments

Comments
 (0)