package main import ( "encoding/json" "log" "net/http" "os" "time" messages "git.tornberg.me/go-cart-actor/proto" ) func spawn(id CartId) (*CartGrain, error) { ret := &CartGrain{ Id: id, Items: []CartItem{}, storageMessages: []Message{}, TotalPrice: 0, } err := loadMessages(ret, id) return ret, err } func init() { os.Mkdir("data", 0755) } type App struct { pool *GrainLocalPool 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() { err := a.storage.Store(id, grain) if err != nil { log.Printf("Error saving grain %s: %v\n", id, err) } } return a.storage.saveState() } func (a *App) HandleSave(w http.ResponseWriter, r *http.Request) { err := a.Save() if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) } else { w.WriteHeader(http.StatusCreated) } } func main() { // Create a new instance of the server 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, 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 /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) }