refactor
This commit is contained in:
+10
-50
@@ -12,7 +12,6 @@ package backofficeadmin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -22,10 +21,8 @@ import (
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/cart"
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/checkout"
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/profile"
|
||||
"git.k6n.net/mats/go-redis-inventory/pkg/inventory"
|
||||
"git.k6n.net/mats/platform/rabbit"
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// CartFileInfo is the metadata returned by the cart listing endpoint.
|
||||
@@ -52,9 +49,6 @@ type Config struct {
|
||||
CheckoutDataDir string
|
||||
// ProfileDataDir holds the profile event logs (optional, default "" — customer endpoints disabled).
|
||||
ProfileDataDir string
|
||||
// RedisAddress / RedisPassword reach the inventory store.
|
||||
RedisAddress string
|
||||
RedisPassword string
|
||||
}
|
||||
|
||||
// App is the commerce admin control plane. Construct with New, register its
|
||||
@@ -62,15 +56,15 @@ type Config struct {
|
||||
type App struct {
|
||||
fs *FileServer
|
||||
hub *Hub
|
||||
rdb *redis.Client
|
||||
inv *inventory.RedisInventoryService
|
||||
cs *CustomerServer
|
||||
}
|
||||
|
||||
// New constructs the commerce admin: it opens the Redis inventory service, the
|
||||
// cart/checkout disk event-log stores, the file server, and the WebSocket hub.
|
||||
// It does not register routes (RegisterRoutes), start background work (Start),
|
||||
// or open a listener.
|
||||
// New constructs the commerce admin: it opens the cart/checkout disk event-log
|
||||
// stores, the file server, and the WebSocket hub. It does not register routes
|
||||
// (RegisterRoutes), start background work (Start), or open a listener.
|
||||
//
|
||||
// Inventory writes no longer live here — they go through the standalone inventory
|
||||
// service's HTTP API (/api/inventory/batch), reverse-proxied by the host.
|
||||
func New(cfg Config) (*App, error) {
|
||||
if cfg.DataDir == "" {
|
||||
cfg.DataDir = "data"
|
||||
@@ -79,16 +73,6 @@ func New(cfg Config) (*App, error) {
|
||||
cfg.CheckoutDataDir = "checkout-data"
|
||||
}
|
||||
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: cfg.RedisAddress,
|
||||
Password: cfg.RedisPassword,
|
||||
DB: 0,
|
||||
})
|
||||
inventoryService, err := inventory.NewRedisInventoryService(rdb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_ = os.MkdirAll(cfg.DataDir, 0755)
|
||||
reg := cart.NewCartMultationRegistry(cart.NewCartMutationContext(nil))
|
||||
diskStorage := actor.NewDiskStorage[cart.CartGrain](cfg.DataDir, reg)
|
||||
@@ -109,8 +93,6 @@ func New(cfg Config) (*App, error) {
|
||||
return &App{
|
||||
fs: NewFileServer(cfg.DataDir, cfg.CheckoutDataDir, diskStorage, diskStorageCheckout),
|
||||
hub: NewHub(),
|
||||
rdb: rdb,
|
||||
inv: inventoryService,
|
||||
cs: customerSrv,
|
||||
}, nil
|
||||
}
|
||||
@@ -123,7 +105,6 @@ func (a *App) RegisterRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /cart/{id}", a.fs.CartHandler)
|
||||
mux.HandleFunc("GET /checkouts", a.fs.CheckoutsHandler)
|
||||
mux.HandleFunc("GET /checkout/{id}", a.fs.CheckoutHandler)
|
||||
mux.HandleFunc("PUT /inventory/{locationId}/{sku}", a.updateInventory)
|
||||
mux.HandleFunc("/promotions", a.fs.PromotionsHandler)
|
||||
mux.HandleFunc("/vouchers", a.fs.VoucherHandler)
|
||||
mux.HandleFunc("/promotion/{id}", a.fs.PromotionPartHandler)
|
||||
@@ -134,28 +115,7 @@ func (a *App) RegisterRoutes(mux *http.ServeMux) {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) updateInventory(w http.ResponseWriter, r *http.Request) {
|
||||
locationID := inventory.LocationID(r.PathValue("locationId"))
|
||||
sku := inventory.SKU(r.PathValue("sku"))
|
||||
pipe := a.rdb.Pipeline()
|
||||
var payload struct {
|
||||
Quantity int64 `json:"quantity"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||
http.Error(w, "invalid payload", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
a.inv.UpdateInventory(r.Context(), pipe, sku, locationID, payload.Quantity)
|
||||
if _, err := pipe.Exec(r.Context()); err != nil {
|
||||
http.Error(w, "failed to update inventory", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := a.inv.SendInventoryChanged(r.Context(), sku, locationID); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
|
||||
// Start runs the WebSocket hub loop and, when conn is non-nil, the RabbitMQ
|
||||
// mutation consumer that broadcasts cart mutations to connected clients. The
|
||||
@@ -169,10 +129,10 @@ func (a *App) Start(ctx context.Context, conn *rabbit.Conn) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shutdown releases the App's resources (the Redis client). The background
|
||||
// goroutines are stopped by cancelling the ctx passed to Start.
|
||||
// Shutdown releases the App's resources. The background goroutines are stopped
|
||||
// by cancelling the ctx passed to Start.
|
||||
func (a *App) Shutdown(_ context.Context) error {
|
||||
return a.rdb.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
// startMutationConsumer subscribes to the cart "mutation" topic and forwards
|
||||
|
||||
Reference in New Issue
Block a user