-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
121 lines (103 loc) · 3.41 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"github.com/gorilla/mux"
)
var (
addr = flag.String("addr", ":8080", "http service address")
// Create our UDP socket
addrs, _ = net.ResolveUDPAddr("udp", ":20777")
sock, err = net.ListenUDP("udp", addrs)
)
// Prints out if we connect to our databases and then the table scheme
func logPrintFormat() {
_ = <-redis_ping_done
fmt.Print("\n")
fmt.Println(" DATE TIME IP PORT ADDRESS/EVENT ")
fmt.Println("----------|--------|------|------|-----------------")
return
}
// Called when when a client lands on localhost:8080/
func liveHandler(w http.ResponseWriter, r *http.Request) {
log.Println("", r.RemoteAddr, " ", r.URL)
if r.URL.Path != "/" {
http.Error(w, "Not found", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
http.ServeFile(w, r, "./web/live_dashboard.html")
}
// Called when when a client lands on localhost:8080/history
func historyHandler(w http.ResponseWriter, r *http.Request) {
log.Println("", r.RemoteAddr, " ", r.URL)
if r.URL.Path != "/history" {
http.Error(w, "Not found", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
http.ServeFile(w, r, "./web/history_dashboard.html")
}
// Called when when a client lands on localhost:8080/time
func timeHandler(w http.ResponseWriter, r *http.Request) {
log.Println("", r.RemoteAddr, " ", r.URL)
if r.URL.Path != "/time" {
http.Error(w, "Not found", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
http.ServeFile(w, r, "./web/time_dashboard.html")
}
func main() {
// Sets the location in which to serve our static files from for our webpage
var dir string
flag.StringVar(&dir, "dir", "./web", "the directory to serve files from. Defaults to the current dir")
flag.Parse()
// Connect to or create and use F1_GO_MYSQL database
start_mysql()
// Outputs a table like visual to keep track of what is outputed by our log.Orintlns
go logPrintFormat()
// Create a hub object to handle our client connections and needs
live_hub := newHub()
go live_hub.run()
// Start getting data from the game
// Add to redis and send to hub
go getGameData(live_hub)
// Creates a new mux
router := mux.NewRouter()
// WHen our html page calls its static files from /static/file, this sets the location to grab them from
// TODO: Is this all this code needed? Couldn't we just set it web/?
router.PathPrefix("/web/").Handler(http.StripPrefix("/web/", http.FileServer(http.Dir(dir))))
// Our handler functions for each page
// Landing page /aka live telemetry or telemetry_dashboard
router.HandleFunc("/", liveHandler)
router.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serve_ws("dashboard", live_hub, w, r)
})
// History page /aka history_dashboard
router.HandleFunc("/history", historyHandler)
router.HandleFunc("/history/ws", func(w http.ResponseWriter, r *http.Request) {
serve_ws("history", live_hub, w, r)
})
// Live time page /aka time_dashboard
router.HandleFunc("/time", timeHandler)
router.HandleFunc("/time/ws", func(w http.ResponseWriter, r *http.Request) {
serve_ws("time", live_hub, w, r)
})
err := http.ListenAndServe(":8080", router)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}