notfall/main.go

64 lines
1.3 KiB
Go
Raw Normal View History

2022-01-16 10:55:37 +00:00
package main
import (
2022-01-16 11:27:54 +00:00
_ "embed"
2022-01-16 10:55:37 +00:00
"encoding/json"
2022-01-16 11:27:54 +00:00
"html/template"
2022-01-16 10:55:37 +00:00
"net/http"
)
2022-01-16 11:39:26 +00:00
var lastData map[string]interface{}
2022-01-16 10:55:37 +00:00
2022-01-16 11:27:54 +00:00
//go:embed notfall.tmpl
var templateContent []byte
2022-01-16 12:04:33 +00:00
var active = false
var getData = false
2022-01-16 11:27:54 +00:00
2022-01-16 10:55:37 +00:00
func main() {
http.HandleFunc("/owntrack", func(writer http.ResponseWriter, request *http.Request) {
2022-01-16 11:27:54 +00:00
var p map[string]interface{}
2022-01-16 10:55:37 +00:00
err := json.NewDecoder(request.Body).Decode(&p)
if err != nil {
2022-01-16 10:56:34 +00:00
writer.WriteHeader(http.StatusInternalServerError)
return
2022-01-16 10:55:37 +00:00
}
2022-01-16 11:27:54 +00:00
if p["_type"] == "location"{
lastData = p
}
2022-01-16 12:04:33 +00:00
getData = true
2022-01-16 11:27:54 +00:00
writer.Write([]byte("[]"))
2022-01-16 10:55:37 +00:00
})
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
2022-01-16 11:27:54 +00:00
templ := template.Must(template.New("page").Parse(string(templateContent)))
templ.Execute(writer, map[string]interface{}{
})
2022-01-16 10:55:37 +00:00
})
2022-01-16 11:39:26 +00:00
http.HandleFunc("/json", func(writer http.ResponseWriter, request *http.Request) {
2022-01-16 12:04:33 +00:00
if getData == false || active == false {
writer.WriteHeader(http.StatusInternalServerError)
return
}
2022-01-16 11:41:06 +00:00
data := make(map[string]interface{})
2022-01-16 11:39:26 +00:00
data["lat"] = lastData["lat"]
data["lon"] = lastData["lon"]
data["tst"] = lastData["tst"]
2022-01-16 11:46:43 +00:00
data["acc"] = lastData["acc"]
2022-01-16 11:39:26 +00:00
r, err := json.Marshal(data)
if err != nil {
panic(err)
}
writer.Write(r)
})
2022-01-16 10:55:37 +00:00
http.ListenAndServe(":8080", nil)
}