-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
40 lines (31 loc) · 851 Bytes
/
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
package main
import (
"context"
"log"
"net/http"
"time"
"github.com/alextanhongpin/go-websocket/socket"
"github.com/alextanhongpin/pkg/grace"
)
func main() {
var shutdowns grace.Shutdowns
hub := socket.NewHub()
shutdowns.Append(hub.Start())
fs := http.FileServer(http.Dir("public"))
mux := http.NewServeMux()
mux.Handle("/", fs)
// mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, `{"john"}`, nil)
// })
mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
// Perform authentication here? Use a middleware instead.
hub.ServeWs(w, r)
})
shutdowns.Append(grace.New(mux, "8080"))
quit := grace.Signal()
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
shutdowns.Close(ctx)
log.Println("graceful shutdown")
}