more data

This commit is contained in:
matst80
2024-11-08 23:00:28 +01:00
parent 65a969443a
commit 06ee7b1a27
12 changed files with 164 additions and 87 deletions

18
main.go
View File

@@ -82,7 +82,7 @@ func (a *App) HandleSave(w http.ResponseWriter, r *http.Request) {
func main() {
// Create a new instance of the server
storage, err := NewDiskStorage("data/state.json")
storage, err := NewDiskStorage("data/state.gob")
if err != nil {
log.Printf("Error loading state: %v\n", err)
}
@@ -102,7 +102,7 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("GET /api/{id}", app.HandleGet)
mux.HandleFunc("GET /api/{id}/add/{sku}", app.HandleAddSku)
mux.HandleFunc("GET /remote/{id}", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("GET /remote/{id}/add", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
ts := time.Now().Unix()
data, err := remotePool.Process(ToCartId(id), Message{
@@ -117,7 +117,19 @@ func main() {
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(data)
w.Write(data)
})
mux.HandleFunc("GET /remote/{id}", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
data, err := remotePool.Get(ToCartId(id))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(data)
})
mux.HandleFunc("GET /save", app.HandleSave)
http.ListenAndServe(":8080", mux)