all repos — privychka @ 9e327f2fa97646ef9737ab74b66a19fe7c653bbb

habit tracking service

Add /all, display in a table
Anirudh Oppiliappan x@icyphox.sh
Thu, 30 Dec 2021 15:22:33 +0530
commit

9e327f2fa97646ef9737ab74b66a19fe7c653bbb

parent

d0b8351afa7ff5e358f3393410f67a9d09582bb8

2 files changed, 59 insertions(+), 8 deletions(-)

jump to
A display.html

@@ -0,0 +1,40 @@

+<html> + <title>today's activities</title> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <style type="text/css"> + body { + background: #f4f4f4; + margin: 40px auto; + max-width: 650px; + line-height: 1.6; + font-size: 18px; + font-family: sans-serif; + padding: 0 10px; + } + + td, th { + border: 1px solid; + text-align: left; + padding: 8px; + } + </style> + <main> + <body> + <table> + <tr> + <th>time</th> + <th>activity</th> + <th>notes</th> + </tr> + {{ range $_, $activity := . }} + <tr> + <td>{{ .Time.Format "02 Jan, 2006 15:04:05" }}</td> + <td>{{ .Activity }}</td> + <td><em>{{ .Notes }}</em></td> + </tr> + {{ end }} + </table> + </body> + </main> +</html>
M main.gomain.go

@@ -6,6 +6,7 @@ "encoding/json"

"errors" "flag" "fmt" + "html/template" "log" "net/http" "os"

@@ -151,27 +152,37 @@ w.WriteHeader(204)

}) http.HandleFunc("/today", func(w http.ResponseWriter, r *http.Request) { - key, err := getKey(r) + habits, err := ReadTSV(*hFile) if err != nil { log.Printf("error: %v\n", err) - w.WriteHeader(401) + w.WriteHeader(500) return } - - if *secretKey != key { - log.Printf("incorrect key: %v\n", key) - w.WriteHeader(401) + todays := getTodaysHabits(habits) + t, err := template.ParseFiles("display.html") + if err != nil { + log.Printf("error: %v\n", err) + w.WriteHeader(500) return } + t.Execute(w, todays) + return + }) + http.HandleFunc("/all", func(w http.ResponseWriter, r *http.Request) { habits, err := ReadTSV(*hFile) if err != nil { log.Printf("error: %v\n", err) w.WriteHeader(500) return } - todays := getTodaysHabits(habits) - json.NewEncoder(w).Encode(todays) + t, err := template.ParseFiles("display.html") + if err != nil { + log.Printf("error: %v\n", err) + w.WriteHeader(500) + return + } + t.Execute(w, habits) return })