This project implements a microservice for processing customer orders as part of an e-commerce system. It handles order creation, retrieval, and publishes events to Kafka for downstream services.
- Order Creation: Allows customers to create new orders with multiple items.
- Order Retrieval: Fetch details of an order by its unique ID.
- Event Publishing: Publishes "Order Placed" events to a Kafka topic.
- Persistence: Stores order data in a PostgreSQL database.
- API Documentation: Interactive OpenAPI (Swagger) documentation.
- Structured Logging: Uses
zerologfor clear, machine-readable logs. - Metrics: Exposes Prometheus-compatible metrics for monitoring.
The service follows a clean architecture pattern, separating concerns into distinct layers:
domain: Core business entities and rules (e.g.,Order,OrderItem, validation logic).service: Business logic, orchestrating interactions between domain, repository, and external systems (Kafka).repository: Handles data persistence (currently PostgreSQL).kafka: Producer client for Kafka interactions.api: HTTP handlers for exposing functionality via a RESTful API.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
- Go (1.22 or higher recommended)
- Docker
- Docker Compose
swagCLI tool:go install github.com/swaggo/swag/cmd/swag@latest
-
Clone the repository:
git clone [https://github.com/jonamarkin/e-commerce-order-processing.git](https://github.com/jonamarkin/e-commerce-order-processing.git) cd e-commerce-order-processing -
Set up environment variables: Create a
.envfile in the project root based onenv.example.cp env.example .env
Edit
.envto configure your database and Kafka settings.# .env PORT=8080 DATABASE_URL="postgres://postgres:postgres@localhost:5432/ecommerce_db?sslmode=disable" KAFKA_BROKERS="localhost:9092"
Note: If running services inside Docker Compose,
localhost:9092andlocalhost:5432refer to the host machine's exposed ports. If running from another Docker container, use service names likekafka:9092anddb:5432. -
Start supporting services (PostgreSQL, Kafka, Zookeeper) with Docker Compose:
docker compose up -d db kafka zookeeper
Wait a few moments for services to fully start. You can check their status with
docker compose ps. -
Run Database Migrations: This project uses
golang-migrate. You'll need to install it if you haven't already:go install -tags 'postgres' [github.com/golang-migrate/migrate/v4/cmd/migrate@latest](https://github.com/golang-migrate/migrate/v4/cmd/migrate@latest)Apply the migrations:
migrate -path database/migrations -database "$DATABASE_URL" up(Ensure
DATABASE_URLis correctly set in your environment or substitute the full string.) -
Generate Swagger Documentation:
swag init
-
Run the application:
go run cmd/orderservice/main.go
The service will start on
http://localhost:8080.
The API is served under /api/v1.
- Interactive API Docs (Swagger UI):
http://localhost:8080/swagger/index.html - Metrics (Prometheus format):
http://localhost:8080/metrics
Example cURL requests:
-
Create Order (POST /api/v1/orders)
curl -X POST http://localhost:8080/api/v1/orders \ -H "Content-Type: application/json" \ -d '{ "customer_id": "a1b2c3d4-e5f6-7890-1234-567890abcdef", "items": [ { "product_id": "fedcba98-7654-3210-fedc-ba9876543210", "quantity": 1, "unit_price": 49.99 }, { "product_id": "12345678-abcd-efgh-ijkl-mnopqrstuvwx", "quantity": 2, "unit_price": 25.00 } ] }'
-
Get Order by ID (GET /api/v1/orders/{id}) (Replace
<ORDER_ID>with an ID from a created order)curl http://localhost:8080/api/v1/orders/<ORDER_ID>
- Unit Tests:
go test ./... - Integration Tests (Repository layer):
Ensure your database is running and migrated before running integration tests.
DATABASE_URL="postgres://postgres:postgres@localhost:5432/ecommerce_db?sslmode=disable" go test -v ./internal/orderservice/repository/...
├── cmd/ # Main application entry points
│ └── orderservice/ # Order Service main executable
├── config/ # Application configuration loading
├── database/ # Database schema migrations
│ └── migrations/
├── internal/ # Internal application code (not directly importable by other modules)
│ └── orderservice/
│ ├── api/ # HTTP handlers and API request/response models
│ ├── domain/ # Core business entities, value objects, and rules
│ ├── kafka/ # Kafka producer client
│ ├── metrics/ # Prometheus metric definitions
│ ├── repository/# Data access layer (PostgreSQL implementation)
│ └── service/ # Business logic, orchestrating domain, repo, and external calls
├── docs/ # Generated Swagger documentation
├── go.mod # Go module definition
├── go.sum # Go module checksums
├── README.md # This file
└── env.example # Example environment variables
Feel free to open issues or submit pull requests.
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.