refactor/serializing (#1)

Co-authored-by: matst80 <mats.tornberg@gmail.com>
Reviewed-on: https://git.tornberg.me/mats/go-cart-actor/pulls/1
This commit was merged in pull request #1.
This commit is contained in:
2024-11-08 23:02:09 +01:00
parent 4cc41bcec6
commit 1b75f81119
19 changed files with 576 additions and 409 deletions

76
main.go
View File

@@ -1,18 +1,16 @@
package main
import (
"encoding/gob"
"encoding/json"
"log"
"net/http"
"os"
"time"
"git.tornberg.me/go-cart-actor/git.tornberg.me/go-cart-actor/messages"
messages "git.tornberg.me/go-cart-actor/proto"
)
func spawn(id string) Grain {
func spawn(id CartId) (*CartGrain, error) {
ret := &CartGrain{
Id: id,
Items: []CartItem{},
@@ -20,16 +18,11 @@ func spawn(id string) Grain {
TotalPrice: 0,
}
err := loadMessages(ret, id)
if err != nil {
log.Printf("Error loading messages for grain %s: %v\n", id, err)
}
return ret
return ret, err
}
func init() {
os.Mkdir("data", 0755)
gob.Register(CartItem{})
gob.Register(Message{})
}
type App struct {
@@ -39,7 +32,7 @@ type App struct {
func (a *App) HandleGet(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
grain, err := a.pool.GetOrSpawn(id, spawn)
grain, err := a.pool.Get(ToCartId(id))
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
@@ -52,25 +45,19 @@ func (a *App) HandleGet(w http.ResponseWriter, r *http.Request) {
func (a *App) HandleAddSku(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
sku := r.PathValue("sku")
grain, err := a.pool.GetOrSpawn(id, spawn)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
message := &Message{
Type: 1,
grain, err := a.pool.Process(ToCartId(id), Message{
Type: AddRequestType,
Content: &messages.AddRequest{Sku: sku},
}
var reply CartGrain
err = grain.HandleMessage(message, false, &reply)
})
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(reply)
json.NewEncoder(w).Encode(grain)
}
func (a *App) Save() error {
@@ -95,18 +82,55 @@ 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)
}
app := &App{
pool: NewGrainLocalPool(1000, 5*time.Minute),
pool: NewGrainLocalPool(1000, 5*time.Minute, spawn),
storage: storage,
}
rpcHandler, err := NewGrainHandler(app.pool, "localhost:1337")
if err != nil {
log.Fatalf("Error creating handler: %v\n", err)
}
go rpcHandler.Serve()
remotePool := NewRemoteGrainPool("localhost:1337")
mux := http.NewServeMux()
mux.HandleFunc("GET /{id}", app.HandleGet)
mux.HandleFunc("GET /{id}/add/{sku}", app.HandleAddSku)
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.HandleFunc("GET /save", app.HandleSave)
http.ListenAndServe(":8080", mux)