Skip to content

Commit 114b7f3

Browse files
committed
Merge branch 'post-ms6' into second-nginx
2 parents d9fd8fd + 9c75fcd commit 114b7f3

File tree

106 files changed

+5886
-970
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+5886
-970
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
**/.env
22
.idea
3-
*.log
3+
*.log
4+
main.exe

.husky/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cd ./peerprep && npx lint-staged

.lefthook.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pre-commit:
2+
# parallel: true
3+
# commands:
4+
# golangci-lint:
5+
# glob: "*.go"
6+
# run: golangci-lint run {staged_files}
7+
# lint-staged:
8+
# root: "peerprep/"
9+
# glob: "*.(ts|tsx|css|scss|md|json)"
10+
# run: npx lint-staged

backend/transport/replace_question.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ func ReplaceQuestionWithLogger(db *database.QuestionDB, logger *common.Logger) g
3333
return
3434
}
3535

36-
if id_param >= db.FindNextQuestionId() {
37-
logger.Log.Info(
38-
"Attempting to update a question with an ID greater than next ID, creating a new question",
39-
)
40-
status, err := db.AddQuestion(logger, &new_question)
41-
42-
if err != nil {
43-
ctx.JSON(status, err.Error())
44-
return
45-
}
46-
47-
ctx.JSON(status, "Question added successfully")
48-
logger.Log.Info("Question added successfully")
49-
return
50-
}
36+
//if id_param >= db.FindNextQuestionId() {
37+
// logger.Log.Info(
38+
// "Attempting to update a question with an ID greater than next ID, creating a new question",
39+
// )
40+
// status, err := db.AddQuestion(logger, &new_question)
41+
//
42+
// if err != nil {
43+
// ctx.JSON(status, err.Error())
44+
// return
45+
// }
46+
//
47+
// ctx.JSON(status, "Question added successfully")
48+
// logger.Log.Info("Question added successfully")
49+
// return
50+
//}
5151

5252
logger.Log.Info("Replacing question with ID: ", id_param)
5353
new_question.Id = id_param

collab/main.go

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"log"
88
"net/http"
99
"os"
10-
"sync"
1110
"strconv"
11+
"sync"
1212

1313
"github.com/gin-gonic/gin"
1414
"github.com/gorilla/websocket"
@@ -21,23 +21,25 @@ var upgrader = websocket.Upgrader{
2121
}
2222

2323
type Client struct {
24-
conn *websocket.Conn
25-
roomID string
24+
conn *websocket.Conn
25+
roomID string
2626
authenticated bool
2727
}
2828

2929
type Hub struct {
3030
clients map[*Client]bool
31-
workspaces map[string]string
31+
workspaces map[string]string
3232
broadcast chan Message
3333
register chan *Client
3434
unregister chan *Client
3535
mutex sync.Mutex
3636
}
3737

3838
type Message struct {
39-
roomID string
40-
content []byte
39+
Type string `json:"type"`
40+
RoomID string `json:"roomId"`
41+
Content []byte `json:"data"`
42+
UserID string `json:"userId"`
4143
}
4244

4345
func verifyToken(token string) (bool, string) {
@@ -71,7 +73,7 @@ func verifyToken(token string) (bool, string) {
7173
} `json:"data"`
7274
}
7375

74-
body, err := io.ReadAll(resp.Body)
76+
body, err := io.ReadAll(resp.Body)
7577
if err != nil {
7678
log.Println("Error reading response body:", err)
7779
return false, ""
@@ -97,7 +99,7 @@ func verifyToken(token string) (bool, string) {
9799
func NewHub() *Hub {
98100
return &Hub{
99101
clients: make(map[*Client]bool),
100-
workspaces: make(map[string]string),
102+
workspaces: make(map[string]string),
101103
broadcast: make(chan Message),
102104
register: make(chan *Client),
103105
unregister: make(chan *Client),
@@ -123,11 +125,11 @@ func (h *Hub) Run() {
123125

124126
case message := <-h.broadcast:
125127
h.mutex.Lock()
126-
// Update the current workspace for this roomID
127-
h.workspaces[message.roomID] = string(message.content)
128+
// Update the current workspace for this RoomID
129+
h.workspaces[message.RoomID] = string(message.Content)
128130
for client := range h.clients {
129-
if client.roomID == message.roomID {
130-
err := client.conn.WriteMessage(websocket.TextMessage, message.content)
131+
if client.roomID == message.RoomID {
132+
err := client.conn.WriteMessage(websocket.TextMessage, message.Content)
131133
if err != nil {
132134
log.Printf("Error sending message: %v", err)
133135
client.conn.Close()
@@ -142,9 +144,9 @@ func (h *Hub) Run() {
142144

143145
// ServeWs handles WebSocket requests
144146
func serveWs(
145-
hub *Hub, c *gin.Context,
146-
roomMappings *verify.RoomMappings,
147-
persistMappings *verify.PersistMappings,
147+
hub *Hub, c *gin.Context,
148+
roomMappings *verify.RoomMappings,
149+
persistMappings *verify.PersistMappings,
148150
) {
149151
log.Println("handler called!")
150152
roomID := c.Query("roomID")
@@ -166,9 +168,9 @@ func serveWs(
166168
}
167169

168170
func authenticateClient(
169-
token string, match string, client *Client,
170-
roomMappings *verify.RoomMappings,
171-
persistMappings *verify.PersistMappings,
171+
token string, match string, client *Client,
172+
roomMappings *verify.RoomMappings,
173+
persistMappings *verify.PersistMappings,
172174
) bool {
173175
ok, userID := verifyToken(token)
174176
if !ok {
@@ -180,8 +182,8 @@ func authenticateClient(
180182
}
181183

182184
func authenticateClientNoMatch(
183-
token string, client *Client,
184-
persistMappings *verify.PersistMappings,
185+
token string, client *Client,
186+
persistMappings *verify.PersistMappings,
185187
) bool {
186188
ok, userID := verifyToken(token)
187189
if !ok {
@@ -192,9 +194,9 @@ func authenticateClientNoMatch(
192194
}
193195

194196
func handleMessages(
195-
client *Client, hub *Hub,
196-
roomMappings *verify.RoomMappings,
197-
persistMappings *verify.PersistMappings,
197+
client *Client, hub *Hub,
198+
roomMappings *verify.RoomMappings,
199+
persistMappings *verify.PersistMappings,
198200
) {
199201
defer func() {
200202
hub.unregister <- client
@@ -212,18 +214,17 @@ func handleMessages(
212214
log.Printf("Failed to parse message: %v", err)
213215
continue
214216
}
215-
// Handle authentication message
216217
if msgData["type"] == "auth" {
217-
token, tokenOk := msgData["token"].(string)
218-
if !tokenOk {
219-
log.Println("Authentication failed - no token attached")
220-
client.conn.WriteMessage(
221-
websocket.TextMessage,
222-
[]byte("Authentication failed - no JWT token"),
223-
)
224-
client.conn.Close()
225-
break
226-
}
218+
token, tokenOk := msgData["token"].(string)
219+
if !tokenOk {
220+
log.Println("Authentication failed - no token attached")
221+
client.conn.WriteMessage(
222+
websocket.TextMessage,
223+
[]byte("Authentication failed - no JWT token"),
224+
)
225+
client.conn.Close()
226+
break
227+
}
227228
isSuccess := false
228229
match, matchOk := msgData["matchHash"].(string)
229230
if matchOk && !authenticateClient(token, match, client, roomMappings, persistMappings) {
@@ -246,20 +247,21 @@ func handleMessages(
246247
client.conn.Close()
247248
break
248249
}
249-
client.authenticated = true
250-
log.Println("Client authenticated successfully")
251-
}
250+
client.authenticated = true
251+
log.Println("Client authenticated successfully")
252+
}
252253

253254
if msgData["type"] == "close_session" {
254255
closeMessage := Message{
255-
roomID: client.roomID,
256-
content: []byte("The session has been closed by a user."),
256+
RoomID: client.roomID,
257+
Content: []byte("The session has been closed by a user."),
257258
}
258259
hub.broadcast <- closeMessage
259260
}
260261

261262
// Broadcast the message to other clients
262-
hub.broadcast <- Message{roomID: client.roomID, content: message}
263+
userID, _ := msgData["userId"].(string)
264+
hub.broadcast <- Message{RoomID: client.roomID, Content: message, UserID: userID}
263265
}
264266
}
265267

@@ -273,17 +275,17 @@ func statusHandler(hub *Hub) gin.HandlerFunc {
273275
for client := range hub.clients {
274276
roomID := client.roomID
275277
currentStatus, ok := status[roomID]
276-
if (!ok) {
278+
if !ok {
277279
// Initialize status for a new roomID
278280
status[roomID] = map[string]interface{}{
279-
"clients": 1,
280-
"workspace": hub.workspaces[roomID],
281+
"clients": 1,
282+
"workspace": hub.workspaces[roomID],
281283
}
282284
} else {
283285
// Update the client count for an existing roomID
284286
status[roomID] = map[string]interface{}{
285-
"clients": currentStatus.(map[string]interface{})["clients"].(int) + 1,
286-
"workspace": hub.workspaces[roomID],
287+
"clients": currentStatus.(map[string]interface{})["clients"].(int) + 1,
288+
"workspace": hub.workspaces[roomID],
287289
}
288290
}
289291
}
@@ -292,7 +294,6 @@ func statusHandler(hub *Hub) gin.HandlerFunc {
292294
}
293295
}
294296

295-
296297
func main() {
297298
r := gin.Default()
298299
hub := NewHub()
@@ -324,8 +325,8 @@ func main() {
324325
}
325326
}
326327

327-
roomMappings := verify.InitialiseRoomMappings(REDIS_URI, REDIS_ROOM_MAPPING)
328-
persistMappings := verify.InitialisePersistMappings(REDIS_URI, REDIS_ROOM_PERSIST);
328+
roomMappings := verify.InitialiseRoomMappings(REDIS_URI, REDIS_ROOM_MAPPING)
329+
persistMappings := verify.InitialisePersistMappings(REDIS_URI, REDIS_ROOM_PERSIST)
329330

330331
// WebSocket connection endpoint
331332
r.GET("/ws", func(c *gin.Context) {

comms/package-lock.json

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compose.yaml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ services:
1515
path: peerprep
1616
target: /frontend
1717

18+
1819
user-service:
1920
build: user-service
2021
image: distractedcat/user-service
@@ -135,18 +136,31 @@ services:
135136
path: collab
136137
target: collab/app
137138

139+
formatter:
140+
build: formatter
141+
image: distractedcat/formatter
142+
extra_hosts:
143+
- "host.docker.internal:host-gateway"
144+
ports:
145+
- "5000:5000"
146+
develop:
147+
watch:
148+
- action: sync
149+
path: formatter
150+
target: formatter/app
151+
138152
comms:
139153
build: comms
140-
image: wzwren/comms
141-
env_file:
142-
- comms/.env
154+
image: distractedcat/comms
155+
#env_file:
156+
#- comms/.env
143157
ports:
144158
- "4001:4001"
145159
develop:
146160
watch:
147161
- action: sync
148162
path: comms
149-
target: /comms
163+
target: comms/app
150164

151165
nginx:
152166
build: nginx

formatter/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM alpine:latest
2+
3+
RUN apk add --no-cache clang clang-extra-tools python3 py3-pip nodejs npm
4+
RUN python3 -m venv /src/.venv
5+
RUN /src/.venv/bin/pip install black fastapi uvicorn
6+
RUN npm install -g prettier esprima
7+
ENV PATH="/src/.venv/bin:$PATH"
8+
WORKDIR /src
9+
COPY src /src
10+
EXPOSE 5000
11+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "5000"]

0 commit comments

Comments
 (0)