-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebserver.go
53 lines (44 loc) · 1.17 KB
/
webserver.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
package main
import (
"io/ioutil"
"net/http"
"fmt"
)
var indexPage string
func webServer() {
fs := http.FileServer(http.Dir("web/static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.Handle("/stop", http.HandlerFunc( StopHandler ))
http.Handle("/start", http.HandlerFunc( StartHandler ))
http.HandleFunc("/ws", wsHandler)
http.HandleFunc("/", serveTemplate)
Message("Listening...", false)
http.ListenAndServe(":5000", nil)
}
func loadIndex() {
Message("Load index", false)
content, err := ioutil.ReadFile("web/index.html")
if err != nil {
indexPage = err.Error()
} else {
indexPage = string(content)
}
}
func StopHandler(response http.ResponseWriter, request *http.Request){
response.Header().Set("Content-type", "text/html")
if runningClicker == true {
quit <- true
fmt.Fprint(response, "OK");
} else {
fmt.Fprint(response, "OK");
}
}
func StartHandler(response http.ResponseWriter, request *http.Request){
response.Header().Set("Content-type", "text/html")
go voter()
fmt.Fprint(response, "OK");
}
func serveTemplate(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "text/html")
fmt.Fprint(w, indexPage);
}