diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 14 |
1 files changed, 6 insertions, 8 deletions
@@ -1,6 +1,7 @@ package main import ( + "embed" "encoding/json" "log" "net/http" @@ -8,9 +9,9 @@ import ( "github.com/gorilla/mux" ) -func handleHome(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("stats OK")) -} +//go:embed *.js +//go:embed *.html +var content embed.FS func handleEntryOptions(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") @@ -23,28 +24,25 @@ func handleEntryPost(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") - var entry Entry - err := json.NewDecoder(r.Body).Decode(&entry) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } - err = InsertEntry(&entry) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } + log.Println("New entry for", entry.Location) } func handleRequests() { router := mux.NewRouter() - router.HandleFunc("/", handleHome) router.HandleFunc("/entry", handleEntryPost).Methods("POST") router.HandleFunc("/entry", handleEntryOptions).Methods("OPTIONS") - + router.PathPrefix("/").Handler(http.FileServer(http.FS(content))) log.Fatal(http.ListenAndServe(":8080", router)) } |