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 --- index.html | 5 ----- index.html.in | 6 ++++++ main.go | 23 ++++++++++++++++++----- 3 files changed, 24 insertions(+), 10 deletions(-) delete mode 100644 index.html create mode 100644 index.html.in diff --git a/index.html b/index.html deleted file mode 100644 index 78dcc05..0000000 --- a/index.html +++ /dev/null @@ -1,5 +0,0 @@ - - - -rudone -

rudone — OK

diff --git a/index.html.in b/index.html.in new file mode 100644 index 0000000..ee76fdb --- /dev/null +++ b/index.html.in @@ -0,0 +1,6 @@ + + + +rudone +

rudone — OK

+

{{.}} entries 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