-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
60 lines (52 loc) · 1.44 KB
/
router.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
package main
import (
"html/template"
"io/ioutil"
"net/http"
"os"
"raspi/server/db"
"github.com/gorilla/mux"
)
// Page Response page
type Page struct {
Title string
Heading string
}
var templates = template.Must(template.ParseGlob("templates/*.html"))
var defaultPageData = Page{Title: "RaspiHome", Heading: "Hello world from Go!"}
func root(writer http.ResponseWriter, request *http.Request) {
err := templates.ExecuteTemplate(writer, "index.html", defaultPageData)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
}
func moisture_data(writer http.ResponseWriter, b []byte) {
data, error := ParseSensorData(b)
if error != nil {
ErrorLogger.Printf("Failed to parse sensor data JSON from request bytes: %b\n", b)
http.Error(writer, error.Error(), http.StatusBadRequest)
return
}
if data.Location == "" {
data.Location = "unknown"
}
db.WriteMoisture(os.Getenv("INFLUX_ORG"), os.Getenv("INFLUX_BUCKET"), data.Location, data.Value)
}
func debug(writer http.ResponseWriter, request *http.Request) {
b, err := ioutil.ReadAll(request.Body)
if err != nil {
ErrorLogger.Println(err)
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
moisture_data(writer, b)
InfoLogger.Printf("Body of the request was: %s", b)
}
func router() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", root)
r.HandleFunc("/debug", debug).
Methods("POST")
//r.HandleFunc("/api", api)
return r
}