-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
80 lines (63 loc) · 1.61 KB
/
server.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
package main
import (
"github.com/gorilla/mux"
"html/template"
"log"
"net/http"
"time"
)
var tmpl *template.Template
func init(){
tmpl = template.Must(template.ParseGlob("templates/*.html"))
}
func main() {
wwwServer()
}
type ServerStatus struct {
WiFiScanUp bool
GPSScanUp bool
Timestamp time.Time
}
func home(w http.ResponseWriter, r *http.Request) {
data := ServerStatus{true, true, time.Now()}
err := tmpl.ExecuteTemplate(w, "index.html", data)
if err != nil {
log.Println(err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
// Handle data page
// In final version read data from database
func data(w http.ResponseWriter, r *http.Request) {
ScannedData := readDB()
err := tmpl.ExecuteTemplate(w, "data.html", ScannedData)
if err != nil {
log.Println(err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
func rendermap(w http.ResponseWriter, r *http.Request) {
ScannedData := readDB()
err := tmpl.ExecuteTemplate(w, "map.html", ScannedData)
if err != nil {
log.Println(err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
func NotFound(w http.ResponseWriter, r *http.Request) {
err := tmpl.ExecuteTemplate(w, "404.html", nil)
if err != nil {
log.Println(err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
func wwwServer() {
r := mux.NewRouter()
r.HandleFunc("/", home)
r.HandleFunc("/data", data)
r.HandleFunc("/map", rendermap)
r.NotFoundHandler = http.HandlerFunc(NotFound)
http.Handle("/", r)
log.Print("Starting server www")
http.ListenAndServe(":8080", nil)
}