diff options
author | Aki <please@ignore.pl> | 2023-12-31 06:43:44 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2023-12-31 06:44:13 +0100 |
commit | 18c97d0f0b556cd29c9820ede2f9bddb06d39d34 (patch) | |
tree | 8c35484fb0cb86583fd465c452847a269a9fac62 | |
parent | 121ac0b2457d5f5617916aad38f5c05d94570260 (diff) | |
download | rudone-18c97d0f0b556cd29c9820ede2f9bddb06d39d34.zip rudone-18c97d0f0b556cd29c9820ede2f9bddb06d39d34.tar.gz rudone-18c97d0f0b556cd29c9820ede2f9bddb06d39d34.tar.bz2 |
Print some status in the home view
-rw-r--r-- | index.html.in (renamed from index.html) | 1 | ||||
-rw-r--r-- | main.go | 23 |
2 files changed, 19 insertions, 5 deletions
diff --git a/index.html b/index.html.in index 78dcc05..ee76fdb 100644 --- a/index.html +++ b/index.html.in @@ -3,3 +3,4 @@ <meta charset="utf-8"> <title>rudone</title> <h1>rudone — <span style="color: green">OK</span></h1> +<p>{{.}} entries @@ -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)) } |