From de8976a056f0b549acaac9d3c2841bbbd7e92c54 Mon Sep 17 00:00:00 2001 From: Isak Ruas Date: Fri, 25 Oct 2024 20:14:01 -0300 Subject: [PATCH] feat: remove unrelated files to optimize project structure --- app/backend/go.mod | 7 - app/backend/go.sum | 4 - app/backend/main.go | 123 ------ app/frontend/index.html | 860 ---------------------------------------- 4 files changed, 994 deletions(-) delete mode 100644 app/backend/go.mod delete mode 100644 app/backend/go.sum delete mode 100644 app/backend/main.go delete mode 100644 app/frontend/index.html diff --git a/app/backend/go.mod b/app/backend/go.mod deleted file mode 100644 index f72bdf1..0000000 --- a/app/backend/go.mod +++ /dev/null @@ -1,7 +0,0 @@ -module server - -go 1.19 - -require github.com/gorilla/websocket v1.5.1 - -require golang.org/x/net v0.17.0 // indirect diff --git a/app/backend/go.sum b/app/backend/go.sum deleted file mode 100644 index 272772f..0000000 --- a/app/backend/go.sum +++ /dev/null @@ -1,4 +0,0 @@ -github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= -github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= diff --git a/app/backend/main.go b/app/backend/main.go deleted file mode 100644 index f756077..0000000 --- a/app/backend/main.go +++ /dev/null @@ -1,123 +0,0 @@ -package main - -import ( - "encoding/json" - "log" - "net/http" - "sync" - "time" - - "github.com/gorilla/websocket" -) - -type Message struct { - Type string `json:"type"` - Room string `json:"room"` - Text string `json:"text"` -} - -type RoomManager struct { - rooms map[string]map[*websocket.Conn]bool - mutex sync.RWMutex -} - -func NewRoomManager() *RoomManager { - return &RoomManager{ - rooms: make(map[string]map[*websocket.Conn]bool), - } -} - -func (rm *RoomManager) AddClientToRoom(room string, client *websocket.Conn) { - rm.mutex.Lock() - defer rm.mutex.Unlock() - - if rm.rooms[room] == nil { - rm.rooms[room] = make(map[*websocket.Conn]bool) - } - rm.rooms[room][client] = true - log.Printf("[%s] client added to room %s\n", time.Now().UTC().Format(time.RFC3339), room) -} - -func (rm *RoomManager) RemoveClientFromRoom(room string, client *websocket.Conn) { - rm.mutex.Lock() - defer rm.mutex.Unlock() - - if rm.rooms[room] != nil { - delete(rm.rooms[room], client) - if len(rm.rooms[room]) == 0 { - delete(rm.rooms, room) - log.Printf("[%s] room %s is now empty and removed\n", time.Now().UTC().Format(time.RFC3339), room) - } - } -} - -func (rm *RoomManager) BroadcastMessage(room string, message []byte, sender *websocket.Conn) { - rm.mutex.RLock() - defer rm.mutex.RUnlock() - - if rm.rooms[room] != nil { - for client := range rm.rooms[room] { - if client != sender { - go func(c *websocket.Conn) { - if err := c.WriteMessage(websocket.TextMessage, message); err != nil { - log.Printf("[%s] error broadcasting message to client: %v\n", time.Now().UTC().Format(time.RFC3339), err) - rm.RemoveClientFromRoom(room, c) - } - }(client) - } - } - log.Printf("[%s] message broadcasted to room %s\n", time.Now().UTC().Format(time.RFC3339), room) - } -} - -func wsHandler(rm *RoomManager, w http.ResponseWriter, r *http.Request) { - upgrader := websocket.Upgrader{ - CheckOrigin: func(r *http.Request) bool { - // Allow all connections by default - return true - }, - } - - conn, err := upgrader.Upgrade(w, r, nil) - if err != nil { - log.Printf("[%s] error upgrading to websocket: %v\n", time.Now().UTC().Format(time.RFC3339), err) - return - } - defer conn.Close() - - for { - _, message, err := conn.ReadMessage() - if err != nil { - log.Printf("[%s] error reading message: %v\n", time.Now().UTC().Format(time.RFC3339), err) - rm.RemoveClientFromRoom("", conn) - break - } - - var msg Message - if err := json.Unmarshal(message, &msg); err != nil { - log.Printf("[%s] error unmarshalling message: %v\n", time.Now().UTC().Format(time.RFC3339), err) - continue - } - - switch msg.Type { - case "join": - rm.AddClientToRoom(msg.Room, conn) - case "text": - go rm.BroadcastMessage(msg.Room, message, conn) - case "audio": - go rm.BroadcastMessage(msg.Room, message, conn) - default: - log.Printf("[%s] invalid message type: %s\n", time.Now().UTC().Format(time.RFC3339), msg.Type) - } - } -} - -func main() { - rm := NewRoomManager() - - http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { - wsHandler(rm, w, r) - }) - log.SetFlags(0) - log.Fatal(http.ListenAndServe(":8080", nil)) -} diff --git a/app/frontend/index.html b/app/frontend/index.html deleted file mode 100644 index 505c8e1..0000000 --- a/app/frontend/index.html +++ /dev/null @@ -1,860 +0,0 @@ - - - - - - - - - - - - - - - -
- -
- -

Chat with {{ currentFriend.name }}

- -
- -
- -
- -

- {{ renderText(message.raw) }} : -

- -

You sent an audio message :

- - -
- -
- -

- : {{ renderText(message.raw) }} -

- -

: You received an audio message

- - -
-
-
- - - -
- - -
-
- - -
- -

Friends List

- -
    - -
  • - - - - - - - - -
  • -
-
- - -
- -

Add/Edit Friend

- - - - - - - - - - -
- - -
- - - - - - {{privateKeyInputError}} - - - -
- - -
-

Connection lost, attempting to reconnect...

-
-
- - - - -