more stuff

This commit is contained in:
matst80
2024-11-09 01:02:17 +01:00
parent 356f5effba
commit c3d30ea0b3
12 changed files with 153 additions and 102 deletions

117
main.go
View File

@@ -1,7 +1,6 @@
package main
import (
"encoding/json"
"log"
"net/http"
"os"
@@ -30,38 +29,11 @@ type App struct {
storage *DiskStorage
}
func (a *App) HandleGet(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
grain, err := a.pool.Get(ToCartId(id))
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(grain)
}
func (a *App) HandleAddSku(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
sku := r.PathValue("sku")
grain, err := a.pool.Process(ToCartId(id), Message{
Type: AddRequestType,
Content: &messages.AddRequest{Sku: sku},
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(grain)
}
func (a *App) Save() error {
for id, grain := range a.pool.GetGrains() {
if grain == nil {
continue
}
err := a.storage.Store(id, grain)
if err != nil {
log.Printf("Error saving grain %s: %v\n", id, err)
@@ -80,6 +52,53 @@ func (a *App) HandleSave(w http.ResponseWriter, r *http.Request) {
}
}
type PoolServer struct {
pool GrainPool
}
func NewPoolServer(pool GrainPool) *PoolServer {
return &PoolServer{
pool: pool,
}
}
func (s *PoolServer) HandleGet(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
data, err := s.pool.Get(ToCartId(id))
if err != nil {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
func (s *PoolServer) HandleAddSku(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
sku := r.PathValue("sku")
data, err := s.pool.Process(ToCartId(id), Message{
Type: AddRequestType,
Content: &messages.AddRequest{Sku: sku},
})
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)
}
func (s *PoolServer) Serve() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("GET /{id}", s.HandleGet)
mux.HandleFunc("GET /{id}/add/{sku}", s.HandleAddSku)
return mux
}
func main() {
// Create a new instance of the server
storage, err := NewDiskStorage("data/state.gob")
@@ -98,39 +117,11 @@ func main() {
go rpcHandler.Serve()
remotePool := NewRemoteGrainPool("localhost:1337")
remoteServer := NewPoolServer(remotePool)
localServer := NewPoolServer(app.pool)
mux := http.NewServeMux()
mux.HandleFunc("GET /api/{id}", app.HandleGet)
mux.HandleFunc("GET /api/{id}/add/{sku}", app.HandleAddSku)
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{
Type: AddRequestType,
TimeStamp: &ts,
Content: &messages.AddRequest{Sku: "49565"},
})
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 /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.Handle("/remote/", http.StripPrefix("/remote", remoteServer.Serve()))
mux.Handle("/local/", http.StripPrefix("/local", localServer.Serve()))
mux.HandleFunc("GET /save", app.HandleSave)
http.ListenAndServe(":8080", mux)