-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (39 loc) · 1.05 KB
/
main.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 (
"fmt"
"log"
"net/http"
"html/template"
"time"
)
var lastWatered time.Time;
func handleIndex(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("index.html"));
tmpl.Execute(w, nil);
}
func isWatered(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
lastWatered = time.Now();
}
water_next := lastWatered.Add(time.Hour * 24);
watered := !time.Now().After(water_next);
var tmpl *template.Template;
if watered {
tmpl = template.Must(template.ParseFiles("watered.html"));
data := water_next.String();
tmpl.Execute(w, data);
} else {
tmpl = template.Must(template.ParseFiles("dry.html"));
tmpl.Execute(w, nil);
}
}
func main() {
fmt.Println("listening...");
lastWatered = time.Now();
mux := http.NewServeMux();
fs := http.FileServer(http.Dir("static"));
mux.Handle("/static/", http.StripPrefix("/static/", fs));
mux.HandleFunc("/", handleIndex);
mux.HandleFunc("/is-watered", isWatered)
log.Fatal(http.ListenAndServe(":8000", mux));
}