summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go29
1 files changed, 26 insertions, 3 deletions
diff --git a/main.go b/main.go
index 73de3e1..f5129ce 100644
--- a/main.go
+++ b/main.go
@@ -8,6 +8,7 @@ import (
"net/http"
"os"
"strconv"
+ "time"
"github.com/gorilla/mux"
)
@@ -74,15 +75,37 @@ func handleEntryGet(w http.ResponseWriter, r *http.Request) {
}
}
+type Stat struct {
+ Title string
+ Amount uint
+}
+
+type Home struct {
+ Stats []Stat
+}
+
func handleHome(pathname string) func(http.ResponseWriter, *http.Request) {
t := template.Must(template.ParseFS(content, pathname))
return func(w http.ResponseWriter, r *http.Request) {
- count, err := CountEntries()
- if err != nil {
+ var home Home
+ now := time.Now().UTC()
+ last_day, err := EntriesSince(now.AddDate(0, 0, -1))
+ if err == nil {
+ home.Stats = append(home.Stats, Stat{"Last 24 hours", uint(len(last_day))})
+ }
+ last_month, err := EntriesSince(now.AddDate(0, 0, -30))
+ if err == nil {
+ home.Stats = append(home.Stats, Stat{"Last 30 days", uint(len(last_month))})
+ }
+ total, err := CountEntries()
+ if err == nil {
+ home.Stats = append(home.Stats, Stat{"Total", total})
+ }
+ if len(home.Stats) < 1 {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
- t.Execute(w, count)
+ t.Execute(w, home)
}
}