diff options
-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)) } |