-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (46 loc) · 1.45 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
package main
import (
"flag"
"fmt"
"net/http"
"github.com/bloom-chat/auth"
"github.com/bloom-chat/internal/managers"
_ "github.com/bloom-chat/internal/models"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
)
var address = flag.String("address", "0.0.0.0:8080", "HTTP service address")
var upgrader = websocket.Upgrader{}
var clientsManager *managers.ClientManager
func chat(w http.ResponseWriter, r *http.Request) {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade to websocket failed", err)
}
client := clientsManager.AddClient(conn)
conn.SetCloseHandler(func(code int, text string) error {
log.Printf("Client: %s disconnected!\n", client.Id)
clientsManager.RemoveClient(client.Id)
client.CloseCh <- true
return nil
})
client.Start()
}
func main() {
flag.Parse()
fmt.Println("Server blooming...: ", *address)
http.HandleFunc("/_ah/health", healthCheckHandler)
http.Handle("/", http.FileServer(http.Dir("static")))
http.HandleFunc("/ws", chat)
http.HandleFunc("/auth", auth.Welcome)
//initialize managers
clientsManager = managers.NewClientManager()
managers.NewRoomManager()
managers.NewSessionManager()
log.Fatal(http.ListenAndServe(*address, nil))
}
// healthCheckHandler is used by App Engine Flex to check instance health.
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, "ok")
}