diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 23 |
1 files changed, 18 insertions, 5 deletions
@@ -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)) } |