-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
40 lines (36 loc) · 821 Bytes
/
http.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
package main
import (
"net/http"
"strconv"
"html/template"
)
type Page struct {
Temp float64
Regen string
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "template/index.html")
}
func DataHandler(w http.ResponseWriter, r *http.Request) {
TempList.Mutex.RLock()
defer TempList.Mutex.RUnlock()
lat, err := strconv.ParseFloat(r.FormValue("Lat"), 64)
if err != nil {
http.Error(w, "Latitude not given", 417)
return
}
lon, err := strconv.ParseFloat(r.FormValue("Lon"), 64)
if err != nil {
http.Error(w, "Longitude not given", 417)
return
}
temp := FindClosestTemp(lat, lon)
p := new(Page)
p.Temp = temp
p.Regen = getRain(lat, lon)
t, err := template.ParseFiles("template/page.html")
if err != nil {
http.Error(w, "AARG", 500)
}
t.Execute(w, p)
}