Role-based shopping cart service built with Go, Gin, gRPC, PostgreSQL, Redis, and Kafka.
- Cart lifecycle: create, fetch, update, delete, merge
- Cart item management: add/update/remove and save-for-later
- Coupon apply/remove and checkout validation
- Order placement and order history APIs
- Cart sharing and cart version restore
- Role-based APIs on separate ports:
admin,service-admin,supplier,customer - gRPC server plus gRPC-Gateway bridge
- WebSocket cart sync endpoint
- Metrics, tracing, and dashboard stack via Docker Compose
- Go
1.25.x - Gin (REST), gRPC, grpc-gateway
- PostgreSQL (GORM)
- Redis
- Kafka (Sarama)
- OpenTelemetry + Jaeger
- Prometheus + Grafana + Alertmanager + Loki
- Swagger (
swag)
cart-service/
cmd/
api/main.go # Main app: REST + gRPC + gateway + role routers
consumer/main.go # Kafka consumer example
config/ # Env configuration loader
docs/ # Swagger docs (base + role-specific)
internal/
handlers/ # HTTP handlers
grpc/ # gRPC server implementation
middleware/ # Auth, rate limit, tracing, logging, CORS, headers
models/ # Domain models
repository/ # Data access layer
service/ # Business logic
websocket/ # WebSocket sync hub
jobs/ # Background jobs (cleanup, price sync)
migrations/ # SQL migrations
monitoring/ # Prometheus/Grafana/Alertmanager/Loki configs
pkg/ # Shared packages (db, cache, kafka, metrics, tracing, etc.)
proto/cart.proto # gRPC contract
scripts/ # Utility scripts (token, swagger generation)
tests/ # integration, kafka, grpc, load tests
8081Admin REST + Swagger +/health+/metrics8082Service Admin REST + Swagger +/health+/metrics8083Supplier REST + Swagger +/health+/metrics8084Customer REST + Swagger +/health+/metrics8085gRPC-Gateway9090gRPC server
Infrastructure ports from docker-compose.yml:
5432PostgreSQL6379Redis8005Redis Stack UI8006Kafka UI9091Prometheus3000Grafana16686Jaeger UI9093Alertmanager3100Loki
Customer/public routes (/api/v1, served on 8084):
- Auth:
POST /auth/signup,POST /auth/login - Cart public:
POST /cart/create,GET /cart/guest/:session_id,GET /cart/:id,GET /cart/:id/summary,POST /cart/validate-checkout,GET /cart/coupon/:code,GET /cart/shared/:token - Cart auth:
GET /cart,POST /cart/items,PUT /cart/items/:id,DELETE /cart/items/:id,DELETE /cart/:id,POST /cart/merge,POST /cart/:id/share - Cart versions:
GET /cart/:id/versions,POST /cart/:id/versions/:version/restore - Coupons/checkout:
POST /cart/apply-coupon,DELETE /cart/remove-coupon,POST /cart/calculate,POST /cart/checkout - Saved items:
POST /cart/items/:id/save-for-later,GET /cart/saved - Orders:
POST /orders,GET /orders,GET /orders/:id,DELETE /orders/:id
Admin routes (/api/v1/admin, served on 8081):
- Carts/analytics/users/orders/requests management endpoints
- Examples:
GET /carts,GET /carts/abandoned,GET /analytics,POST /cleanup/expired
Service admin routes (/api/v1/service-admin, served on 8082):
- Catalog visibility and request workflows
- Examples:
GET /items,POST /requests/delete-item/:id,POST /requests/modify-item/:id
Supplier routes (/api/v1/supplier, served on 8083):
GET /products,PUT /inventory/:id/quantity,PUT /products/:id/rate
WebSocket:
GET /ws/cart(available on admin/customer routers)- Test page:
web-socket.html
Defined in proto/cart.proto:
CreateCartGetCartAddItemUpdateItemRemoveItemApplyCouponCheckout
gRPC runs on localhost:9090, gateway on localhost:8085.
- Go
1.25+ - Docker + Docker Compose
makemigrateCLI (formake migrate-*targets)- Optional:
swag,protoc,grpcurl,jq,k6
- Clone and enter project.
- Create local env file from example.
- Start dependencies.
- Apply migrations.
- Run the API.
cp .env.example .env
docker-compose up -d postgres redis zookeeper kafka
make migrate-up
make runTo run everything (app + infra) in containers with hot reload:
docker-compose up -dFrom Makefile:
make run # go run cmd/api/main.go
make build # build binary to ./bin/cart-service
make test # go test -v ./...
make docker-up # docker-compose up -d
make docker-down # docker-compose down
make docker-logs # docker-compose logs -f
make migrate-up # apply migrations
make migrate-down # rollback migrations
make migrate-create name=<migration_name>
make proto # regenerate protobuf + grpc + gateway code
make deps # go mod download && go mod tidyRole-specific Swagger docs are available at:
http://localhost:8081/swagger/index.htmlhttp://localhost:8082/swagger/index.htmlhttp://localhost:8083/swagger/index.htmlhttp://localhost:8084/swagger/index.html
Regenerate role docs (PowerShell):
./scripts/generate-swagger.ps1Unit/all tests:
go test -v ./...Targeted suites:
go test -v ./tests/integration/...
go test -v ./tests/kafka/...gRPC smoke script:
./tests/grpc/test_grpc.shLoad test (k6):
k6 run tests/load/basic_load_test.jsSee .env.example for the complete list. Core variables:
APP_PORT,GRPC_PORTPOSTGRES_HOST,POSTGRES_PORT,POSTGRES_USER,POSTGRES_PASSWORD,POSTGRES_DB,POSTGRES_SSL_MODEREDIS_HOST,REDIS_PORT,REDIS_PASSWORD,REDIS_DBKAFKA_BROKERSJWT_SECRET
- Prometheus config:
monitoring/prometheus.yml - Alert rules:
monitoring/alert_rules.yml - Grafana datasource:
monitoring/grafana-datasources.yml - Dashboard JSON:
monitoring/grafana-dashboard.json - Alertmanager config:
monitoring/alertmanager.yml - Loki config:
monitoring/loki-config.yml
cmd/api/main.gohardcodes role servers on ports8081-8084, gRPC gateway8085, and gRPC9090.cmd/consumer/main.gois a separate Kafka consumer process you can run independently.scripts/generate_token.gocan generate a JWT for local testing.