-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
47 lines (39 loc) · 954 Bytes
/
handlers.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
package main
import (
"html/template"
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
// Configure the upgrader
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func ChatHandler(w http.ResponseWriter, r *http.Request) {
if !HasSession(r) {
logger.Warn("Not authenticated")
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
// Return results
template_file := "./templates/chat.html"
tmpl, _ := template.ParseFiles(template_file)
err := tmpl.Execute(w, GetUserFromSession(r))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func WebSocketHandler(w http.ResponseWriter, r *http.Request) {
// Upgrade initial GET request to a websocket
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
logger.Error(err)
return
}
vars := mux.Vars(r)
chatroom := vars["chatroom"]
hub := pool.Get(chatroom)
hub.Add(ws)
}