-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
307 lines (270 loc) · 7.32 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Main.go
package main
import (
"flag"
"html/template"
"log"
"net/http"
"time"
"sync"
)
// Logger is a middleware that logs HTTP requests
func Logger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// Create a ResponseRecorder to capture the status code
rr := &ResponseRecorder{ResponseWriter: w, statusCode: http.StatusOK}
next.ServeHTTP(rr, r)
duration := time.Since(start)
log.Printf("Method: %s, Route: %s, Status: %d, Duration: %v\n",
r.Method, r.URL.Path, rr.statusCode, duration)
})
}
// ResponseRecorder wraps the ResponseWriter to capture the status code
type ResponseRecorder struct {
http.ResponseWriter
statusCode int
}
// WriteHeader captures the status code
func (rr *ResponseRecorder) WriteHeader(code int) {
rr.statusCode = code
rr.ResponseWriter.WriteHeader(code)
}
// User struct
type User struct {
Username string
HashedPassword string
SessionToken string
}
// RoomManager manages multiple chat rooms and user sessions.
type RoomManager struct {
Rooms map[string]*Hub // Maps room IDs to corresponding hubs.
Usernames map[string]*User // Maps usernames to users.
Sessions map[string]*User // Maps session tokens to usernames.
mu sync.Mutex // Mutex for safe concurrent access to maps.
db *DB // Pointer to database
}
//
// Serves 404 page
//
func serve404(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "templates/404.html")
}
//
// Serves home page
//
func serveHome(rm *RoomManager, w http.ResponseWriter) {
type TemplateData struct {
Rooms map[string]int
UserCount int
}
rooms, err := rm.db.GetRooms()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
userCount, err := rm.db.GetUserCount()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := TemplateData{
Rooms: rooms,
UserCount: userCount,
}
tmpl, err := template.ParseFiles("templates/home.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func serveStart(rm *RoomManager, w http.ResponseWriter, r *http.Request) {
// Parse and check for username and password
r.ParseForm()
username := r.FormValue("username")
password := r.FormValue("password")
if username == "" {
http.Error(w, "Username is required", http.StatusBadRequest)
return
}
if password == "" {
http.Error(w, "Password is required", http.StatusBadRequest)
return
}
rm.mu.Lock()
defer rm.mu.Unlock()
user, err := rm.db.GetUser(username)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if user == nil {
// User doesn't exist, create new user
hashedPassword, err := hashPassword(password)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = rm.db.CreateUser(username, hashedPassword)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
user = &User{Username: username, HashedPassword: hashedPassword}
} else {
// User exists, verify password
if !verifyPassword(user.HashedPassword, password) {
http.Error(w, "Incorrect password", http.StatusUnauthorized)
return
}
}
// Create session
sessionToken := generateSessionToken()
err = rm.db.CreateSession(sessionToken, username)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
user.SessionToken = sessionToken
rm.Sessions[sessionToken] = user
// Set session cookie
http.SetCookie(w, &http.Cookie{
Name: "SessionToken",
Value: sessionToken,
Path: "/",
HttpOnly: true,
})
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusFound)
}
//
// Serves chat room page
//
func serveChat(rm *RoomManager, w http.ResponseWriter, r *http.Request) {
// Check username
user := getUserFromSession(rm, r)
if user == nil {
roomID := r.PathValue("chatRoom")
tmpl, err := template.ParseFiles("templates/start.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, roomID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
http.ServeFile(w, r, "templates/room.html")
}
//
// serveWs handles websocket requests from the peer.
//
func serveWs(rm *RoomManager, w http.ResponseWriter, r *http.Request) {
// Check username
user := getUserFromSession(rm, r)
if user == nil {
err := "Username is required"
http.Error(w, err, http.StatusBadRequest)
return
}
// Check room id
roomID := r.PathValue("chatRoom")
if roomID == "" {
err := "Room ID is required"
http.Error(w, err, http.StatusBadRequest)
return
}
// Get or create hub
rm.mu.Lock()
hub, exists := rm.Rooms[roomID]
if !exists {
err := rm.db.CreateRoom(roomID)
if err != nil {
log.Printf("Error creating room: %v", err)
rm.mu.Unlock()
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
hub = &Hub{
broadcast: make(chan Message),
register: make(chan *Client),
unregister: make(chan *Client),
Clients: make(map[*Client]bool),
roomID: roomID,
db: rm.db,
}
rm.Rooms[roomID] = hub
go hub.run()
}
rm.mu.Unlock()
// Upgrade the HTTP connection to a WebSocket connection.
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
// Create a new client and register it with the hub.
client := &Client{
hub: hub,
conn: conn,
send: make(chan Message, 256),
user: user,
}
client.hub.register <- client
// Start the read and write pumps for the client.
// Allows collection of memory referenced by the caller by doing all work in new goroutines.
go client.writePump()
go client.readPump()
}
func main() {
// Init database and create tables
db, err := InitDB("chat.db", false)
if err != nil {
log.Fatal("Database initialization failed: ", err)
}
defer db.Close()
err = db.CreateTables()
if err != nil {
log.Fatal("Table creation failed: ", err)
}
var roomManager = &RoomManager{
Rooms: make(map[string]*Hub),
Usernames: make(map[string]*User),
Sessions: make(map[string]*User),
db: db,
}
// Setup MUX
mux := http.NewServeMux()
// Static assets
mux.Handle("GET /assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
// Routes
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
serve404(w, r)
})
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
serveHome(roomManager, w)
})
mux.HandleFunc("POST /start", func(w http.ResponseWriter, r *http.Request) {
serveStart(roomManager, w, r)
})
mux.HandleFunc("GET /c/{chatRoom}", func(w http.ResponseWriter, r *http.Request) {
serveChat(roomManager, w, r)
})
mux.HandleFunc("GET /ws/{chatRoom}", func(w http.ResponseWriter, r *http.Request) {
serveWs(roomManager, w, r)
})
// Start server
port := flag.String("port", "8000", "specify the port to listen on")
flag.Parse()
err = http.ListenAndServe(":" + *port, mux)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}