-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (43 loc) · 1.12 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
package main
import (
"./controller"
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"time"
)
func main() {
//Intercept the kill signal to ensure proper shutdown
//of the process
signalChan := make(chan os.Signal)
signal.Notify(signalChan, os.Interrupt)
var p *int = flag.Int("p", 3000, "the port for the webpage and websocket")
flag.Parse()
ctr, err := controller.CreateController()
if err != nil {
log.Fatal(err.Error())
}
//Create each of the possible websocket connections
mux := http.NewServeMux()
mux.HandleFunc("/api/ws", ctr.HandleFunc)
mux.Handle("/", http.FileServer(http.Dir("client/build")))
defer ctr.Shutdown()
srv := &http.Server{Addr: ":" + strconv.Itoa(*p), Handler: mux}
log.Println("http server started on :" + strconv.Itoa(*p))
//Go routine to listen to kill signals for this process
go func() {
<-signalChan
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
srv.Shutdown(ctx)
}()
//Start and listen for websocket connections
err = srv.ListenAndServe()
if err != nil {
log.Println("ListenAndServer: ", err)
}
log.Println("Shutting down")
}