cleanup and http proxy
This commit is contained in:
168
grpc_server.go
168
grpc_server.go
@@ -16,7 +16,6 @@ import (
|
||||
// cartActorGRPCServer implements the CartActor and ControlPlane gRPC services.
|
||||
// It delegates cart operations to a grain pool and cluster operations to a synced pool.
|
||||
type cartActorGRPCServer struct {
|
||||
messages.UnimplementedCartActorServer
|
||||
messages.UnimplementedControlPlaneServer
|
||||
|
||||
pool GrainPool // For cart state mutations and queries
|
||||
@@ -31,172 +30,6 @@ func NewCartActorGRPCServer(pool GrainPool, syncedPool *SyncedPool) *cartActorGR
|
||||
}
|
||||
}
|
||||
|
||||
// applyMutation routes a single cart mutation to the target grain (used by per-mutation RPC handlers).
|
||||
func (s *cartActorGRPCServer) applyMutation(cartID string, mutation interface{}) *messages.CartMutationReply {
|
||||
// Canonicalize or preserve legacy id (do NOT hash-rewrite legacy textual ids)
|
||||
cid, _, wasBase62, cerr := CanonicalizeOrLegacy(cartID)
|
||||
if cerr != nil {
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 500,
|
||||
Result: &messages.CartMutationReply_Error{Error: fmt.Sprintf("cart_id canonicalization failed: %v", cerr)},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}
|
||||
}
|
||||
_ = wasBase62 // placeholder; future: propagate canonical id in reply metadata
|
||||
legacy := CartIDToLegacy(cid)
|
||||
grain, err := s.pool.Apply(legacy, mutation)
|
||||
if err != nil {
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 500,
|
||||
Result: &messages.CartMutationReply_Error{Error: err.Error()},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}
|
||||
}
|
||||
cartState := ToCartState(grain)
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 200,
|
||||
Result: &messages.CartMutationReply_State{State: cartState},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *cartActorGRPCServer) AddRequest(ctx context.Context, req *messages.AddRequestRequest) (*messages.CartMutationReply, error) {
|
||||
if req.GetCartId() == "" {
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 400,
|
||||
Result: &messages.CartMutationReply_Error{Error: "cart_id is required"},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
return s.applyMutation(req.GetCartId(), req.GetPayload()), nil
|
||||
}
|
||||
|
||||
func (s *cartActorGRPCServer) AddItem(ctx context.Context, req *messages.AddItemRequest) (*messages.CartMutationReply, error) {
|
||||
if req.GetCartId() == "" {
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 400,
|
||||
Result: &messages.CartMutationReply_Error{Error: "cart_id is required"},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
return s.applyMutation(req.GetCartId(), req.GetPayload()), nil
|
||||
}
|
||||
|
||||
func (s *cartActorGRPCServer) RemoveItem(ctx context.Context, req *messages.RemoveItemRequest) (*messages.CartMutationReply, error) {
|
||||
if req.GetCartId() == "" {
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 400,
|
||||
Result: &messages.CartMutationReply_Error{Error: "cart_id is required"},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
return s.applyMutation(req.GetCartId(), req.GetPayload()), nil
|
||||
}
|
||||
|
||||
func (s *cartActorGRPCServer) RemoveDelivery(ctx context.Context, req *messages.RemoveDeliveryRequest) (*messages.CartMutationReply, error) {
|
||||
if req.GetCartId() == "" {
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 400,
|
||||
Result: &messages.CartMutationReply_Error{Error: "cart_id is required"},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
return s.applyMutation(req.GetCartId(), req.GetPayload()), nil
|
||||
}
|
||||
|
||||
func (s *cartActorGRPCServer) ChangeQuantity(ctx context.Context, req *messages.ChangeQuantityRequest) (*messages.CartMutationReply, error) {
|
||||
if req.GetCartId() == "" {
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 400,
|
||||
Result: &messages.CartMutationReply_Error{Error: "cart_id is required"},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
return s.applyMutation(req.GetCartId(), req.GetPayload()), nil
|
||||
}
|
||||
|
||||
func (s *cartActorGRPCServer) SetDelivery(ctx context.Context, req *messages.SetDeliveryRequest) (*messages.CartMutationReply, error) {
|
||||
if req.GetCartId() == "" {
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 400,
|
||||
Result: &messages.CartMutationReply_Error{Error: "cart_id is required"},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
return s.applyMutation(req.GetCartId(), req.GetPayload()), nil
|
||||
}
|
||||
|
||||
func (s *cartActorGRPCServer) SetPickupPoint(ctx context.Context, req *messages.SetPickupPointRequest) (*messages.CartMutationReply, error) {
|
||||
if req.GetCartId() == "" {
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 400,
|
||||
Result: &messages.CartMutationReply_Error{Error: "cart_id is required"},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
return s.applyMutation(req.GetCartId(), req.GetPayload()), nil
|
||||
}
|
||||
|
||||
/*
|
||||
Checkout RPC removed. Checkout is handled at the HTTP layer (PoolServer.HandleCheckout).
|
||||
*/
|
||||
|
||||
func (s *cartActorGRPCServer) SetCartItems(ctx context.Context, req *messages.SetCartItemsRequest) (*messages.CartMutationReply, error) {
|
||||
if req.GetCartId() == "" {
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 400,
|
||||
Result: &messages.CartMutationReply_Error{Error: "cart_id is required"},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
return s.applyMutation(req.GetCartId(), req.GetPayload()), nil
|
||||
}
|
||||
|
||||
func (s *cartActorGRPCServer) OrderCompleted(ctx context.Context, req *messages.OrderCompletedRequest) (*messages.CartMutationReply, error) {
|
||||
if req.GetCartId() == "" {
|
||||
return &messages.CartMutationReply{
|
||||
StatusCode: 400,
|
||||
Result: &messages.CartMutationReply_Error{Error: "cart_id is required"},
|
||||
ServerTimestamp: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
return s.applyMutation(req.GetCartId(), req.GetPayload()), nil
|
||||
}
|
||||
|
||||
// GetState retrieves the current state of a cart grain.
|
||||
func (s *cartActorGRPCServer) GetState(ctx context.Context, req *messages.StateRequest) (*messages.StateReply, error) {
|
||||
if req.GetCartId() == "" {
|
||||
return &messages.StateReply{
|
||||
StatusCode: 400,
|
||||
Result: &messages.StateReply_Error{Error: "cart_id is required"},
|
||||
}, nil
|
||||
}
|
||||
// Canonicalize / upgrade incoming cart id (preserve legacy strings)
|
||||
cid, _, _, cerr := CanonicalizeOrLegacy(req.GetCartId())
|
||||
if cerr != nil {
|
||||
return &messages.StateReply{
|
||||
StatusCode: 500,
|
||||
Result: &messages.StateReply_Error{Error: fmt.Sprintf("cart_id canonicalization failed: %v", cerr)},
|
||||
}, nil
|
||||
}
|
||||
legacy := CartIDToLegacy(cid)
|
||||
|
||||
grain, err := s.pool.Get(legacy)
|
||||
if err != nil {
|
||||
return &messages.StateReply{
|
||||
StatusCode: 500,
|
||||
Result: &messages.StateReply_Error{Error: err.Error()},
|
||||
}, nil
|
||||
}
|
||||
|
||||
cartState := ToCartState(grain)
|
||||
|
||||
return &messages.StateReply{
|
||||
StatusCode: 200,
|
||||
Result: &messages.StateReply_State{State: cartState},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ControlPlane: Ping
|
||||
func (s *cartActorGRPCServer) Ping(ctx context.Context, _ *messages.Empty) (*messages.PingReply, error) {
|
||||
// Expose cart owner cookie (first-touch owner = this host) for HTTP gateways translating gRPC metadata.
|
||||
@@ -269,7 +102,6 @@ func StartGRPCServer(addr string, pool GrainPool, syncedPool *SyncedPool) (*grpc
|
||||
grpcServer := grpc.NewServer()
|
||||
server := NewCartActorGRPCServer(pool, syncedPool)
|
||||
|
||||
messages.RegisterCartActorServer(grpcServer, server)
|
||||
messages.RegisterControlPlaneServer(grpcServer, server)
|
||||
reflection.Register(grpcServer)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user