-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (40 loc) · 995 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
41
42
43
44
45
46
47
48
package main
import (
"log"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
func getWebsocketHandler() http.HandlerFunc {
s := newServer()
go s.runForever()
upgrader := websocket.Upgrader{}
return func(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
newClient(s, ws).runForever()
}
}
func getPort() string {
portNum := "3000"
if len(os.Args) > 1 {
portNum = os.Args[1]
}
return portNum
}
func main() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
fileserver := http.StripPrefix("/", http.FileServer(http.Dir("frontend/dist")))
r := mux.NewRouter()
r.HandleFunc("/ws", getWebsocketHandler())
r.PathPrefix("/").Handler(fileserver)
loggedRouter := handlers.LoggingHandler(os.Stdout, r)
portNum := getPort()
log.Println("Starting tanks at port " + portNum)
log.Fatal(http.ListenAndServe(":"+portNum, loggedRouter))
}