diff options
author | Aki <please@ignore.pl> | 2023-12-31 06:18:38 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2023-12-31 06:18:38 +0100 |
commit | 121ac0b2457d5f5617916aad38f5c05d94570260 (patch) | |
tree | 790c7705bf01c063bf690cfd13791e7ecadfdc37 /main.go | |
parent | a06e585bb3d3c67afd300cfd750d27fb3404a792 (diff) | |
download | rudone-121ac0b2457d5f5617916aad38f5c05d94570260.zip rudone-121ac0b2457d5f5617916aad38f5c05d94570260.tar.gz rudone-121ac0b2457d5f5617916aad38f5c05d94570260.tar.bz2 |
Added GET /entries?[from=N]
Endpoint name changed to plural
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 45 |
1 files changed, 41 insertions, 4 deletions
@@ -6,6 +6,7 @@ import ( "log" "net/http" "os" + "strconv" "github.com/gorilla/mux" ) @@ -16,14 +17,14 @@ var content embed.FS func handleEntryOptions(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-Methods", "OPTIONS, POST, GET") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") w.WriteHeader(http.StatusNoContent) } 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-Methods", "OPTIONS, POST, GET") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") var entry Entry err := json.NewDecoder(r.Body).Decode(&entry) @@ -39,10 +40,46 @@ func handleEntryPost(w http.ResponseWriter, r *http.Request) { log.Println("New entry for", entry.Location) } +type EntriesPage struct { + 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") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") + from64, _ := strconv.ParseUint(r.FormValue("from"), 10, 32) + from := uint(from64) + count, err := CountEntries() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + entries, err := ListEntries(from, 1000) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if entries == nil { + entries = []Entry{} + } + page := EntriesPage{Total: count, Offset: from, Size: uint(len(entries)), Entries: entries} + err = json.NewEncoder(w).Encode(&page) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + func handleRequests() { router := mux.NewRouter() - router.HandleFunc("/entry", handleEntryPost).Methods("POST") - router.HandleFunc("/entry", handleEntryOptions).Methods("OPTIONS") + router.HandleFunc("/entries", handleEntryOptions).Methods("OPTIONS") + router.HandleFunc("/entries", handleEntryPost).Methods("POST") + router.HandleFunc("/entries", handleEntryGet).Methods("GET") router.PathPrefix("/").Handler(http.FileServer(http.FS(content))) log.Fatal(http.ListenAndServe(configure(), router)) } |