Add /today Returns today's activities
Anirudh Oppiliappan x@icyphox.sh
Thu, 30 Dec 2021 12:22:47 +0530
1 files changed,
88 insertions(+),
7 deletions(-)
jump to
M
main.go
→
main.go
@@ -19,8 +19,54 @@ Activity string `json:"activity"`
Notes string `json:"notes,omitempty"` } +func dateEqual(date1, date2 time.Time) bool { + y1, m1, d1 := date1.Date() + y2, m2, d2 := date2.Date() + return y1 == y2 && m1 == m2 && d1 == d2 +} + +func ReadTSV(fname string) ([]Habit, error) { + f, err := os.Open(fname) + if err != nil { + return nil, err + } + + r := csv.NewReader(f) + r.Comma = '\t' + records, err := r.ReadAll() + if err != nil { + return nil, err + } + + habits := []Habit{} + + for _, record := range records { + h := Habit{} + h.Time, _ = time.Parse(time.RFC1123, record[0]) + h.Activity = record[1] + if len(record) == 2 { + h.Notes = "" + } else { + h.Notes = record[2] + } + habits = append(habits, h) + } + + return habits, nil +} + +func getTodaysHabits(habits []Habit) []Habit { + todays := []Habit{} + for _, h := range habits { + if dateEqual(h.Time, time.Now()) { + todays = append(todays, h) + } + } + return todays +} + func (h Habit) String() string { - t := h.Time.Format(time.RFC1123Z) + t := h.Time.Format(time.RFC1123) return fmt.Sprintf( "time: %s activity: %s notes: %s", t,@@ -30,7 +76,7 @@ )
} func (h Habit) WriteTSV(fname string) error { - record := []string{} + records := []string{} f, err := os.OpenFile(fname, os.O_APPEND|os.O_WRONLY, 0644) if err != nil {@@ -41,8 +87,8 @@ w := csv.NewWriter(f)
w.Comma = '\t' t := h.Time.Format(time.RFC1123) - record = append(record, t, h.Activity, h.Notes) - if err := w.Write(record); err != nil { + records = append(records, t, h.Activity, h.Notes) + if err := w.Write(records); err != nil { return err }@@ -56,11 +102,14 @@
return nil } -func getKey(r *http.Request) string { +func getKey(r *http.Request) (string, error) { var key string header := r.Header.Get("Authorization") + if len(header) == 0 { + return "", errors.New("missing Authorization header") + } key = strings.Split(header, " ")[1] - return key + return key, nil } func main() {@@ -77,12 +126,19 @@ }
http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) { h := Habit{} - key := getKey(r) + key, err := getKey(r) + if err != nil { + log.Printf("error: %v\n", err) + w.WriteHeader(401) + return + } + if *secretKey != key { log.Printf("incorrect key: %v\n", key) w.WriteHeader(401) return } + json.NewDecoder(r.Body).Decode(&h) log.Printf(h.String())@@ -92,6 +148,31 @@ w.WriteHeader(500)
return } w.WriteHeader(204) + }) + + http.HandleFunc("/today", func(w http.ResponseWriter, r *http.Request) { + key, err := getKey(r) + if err != nil { + log.Printf("error: %v\n", err) + w.WriteHeader(401) + return + } + + if *secretKey != key { + log.Printf("incorrect key: %v\n", key) + w.WriteHeader(401) + return + } + + 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) + return }) http.ListenAndServe(":8585", nil)