From 18c97d0f0b556cd29c9820ede2f9bddb06d39d34 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 31 Dec 2023 06:43:44 +0100 Subject: Print some status in the home view --- main.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index e28f72b..51ef34c 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "embed" "encoding/json" + "html/template" "log" "net/http" "os" @@ -12,7 +13,7 @@ import ( ) //go:embed *.js -//go:embed *.html +//go:embed *.in var content embed.FS func handleEntryOptions(w http.ResponseWriter, r *http.Request) { @@ -41,13 +42,12 @@ func handleEntryPost(w http.ResponseWriter, r *http.Request) { } type EntriesPage struct { - Total uint `json:"total"` - Offset uint `json:"offset"` - Size uint `json:"size"` + Total uint `json:"total"` + Offset uint `json:"offset"` + Size uint `json:"size"` Entries []Entry `json:"entries"` } - func handleEntryGet(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, POST, GET") @@ -75,11 +75,24 @@ func handleEntryGet(w http.ResponseWriter, r *http.Request) { } } +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 { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + t.Execute(w, count) + } +} + func handleRequests() { router := mux.NewRouter() router.HandleFunc("/entries", handleEntryOptions).Methods("OPTIONS") router.HandleFunc("/entries", handleEntryPost).Methods("POST") router.HandleFunc("/entries", handleEntryGet).Methods("GET") + router.HandleFunc("/", handleHome("index.html.in")).Methods("GET") router.PathPrefix("/").Handler(http.FileServer(http.FS(content))) log.Fatal(http.ListenAndServe(configure(), router)) } -- cgit v1.1