-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (97 loc) · 2.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"encoding/json"
"github.com/gorilla/websocket"
"github.com/joho/godotenv"
"gofr.dev/pkg/errors"
"gofr.dev/pkg/gofr"
"gofrproject/api/attendances"
"gofrproject/api/students"
"log"
"sync"
)
type Message struct {
Sender string `json:"sender"`
Content string `json:"content"`
}
var (
clients = make(map[*websocket.Conn]bool) // Map to store clients
mu sync.Mutex // Mutex to protect clients map
)
func checkWebSocketHeaders(c *gofr.Context) []string {
missingHeaders := []string{}
requiredHeaders := []string{"Connection", "Upgrade", "Sec-Websocket-Version", "Sec-WebSocket-Key"}
for _, key := range requiredHeaders {
if c.Header(key) == "" {
missingHeaders = append(missingHeaders, key)
}
}
return missingHeaders
}
func handleDisconnect(conn *websocket.Conn) {
mu.Lock()
delete(clients, conn)
mu.Unlock()
err := conn.Close()
if err != nil {
return
}
}
func broadcastMessage(message Message, sender *websocket.Conn) {
mu.Lock()
defer mu.Unlock()
messageJSON, err := json.Marshal(message)
if err != nil {
log.Printf("Error marshaling message: %v", err)
return
}
for client := range clients {
if client != sender {
if err := client.WriteMessage(websocket.TextMessage, messageJSON); err != nil {
handleDisconnect(client)
}
}
}
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
app := gofr.New()
// Define your routes and handlers
app.POST("/student", students.AddStudent)
app.GET("/student/{studentID}", students.GetStudent)
app.GET("/students", students.GetAllStudents)
app.PUT("/student/{studentID}", students.UpdateStudent)
app.DELETE("/student/{studentID}", students.DeleteStudent)
app.POST("/attendances", attendances.RecordAttendance)
app.PUT("/attendances/{recordID}", attendances.UpdateAttendance)
app.DELETE("/attendances/{recordID}", attendances.DeleteAttendance)
app.GET("/attendances/{studentID}", attendances.GetAttendance)
app.GET("/websocket", func(c *gofr.Context) (interface{}, error) {
conn := c.WebSocketConnection
if conn != nil {
mu.Lock()
clients[conn] = true
mu.Unlock()
for {
_, data, err := conn.ReadMessage()
if err != nil {
handleDisconnect(conn)
break
}
var msg Message
if err := json.Unmarshal(data, &msg); err != nil {
log.Printf("Error unmarshaling message: %v", err)
continue
}
broadcastMessage(msg, conn)
}
} else {
return nil, errors.MissingParam{Param: checkWebSocketHeaders(c)}
}
return nil, nil
})
app.Start()
}