This repository contains two microservices implemented in Go:
- Cart Service (
cart): Manages shopping cart operations. - Stocks Service (
stocks): Manages how many items do we have and it's price.
Both services follow a layered (clean/hexagonal) architecture and communicate via net/http package.
make buildmake runStarts cart, and stocks services.
make lintBoth services expose HTTP endpoints on port 8080 (cart) and 8081 (stocks).
- Uses HTTP
- Unit tests for core business logic:
make test
Create service skeletons for cart and stocks services according to the documentation.
-
Must build and run your application (e.g., cart, stock, etc.)
-
Should expose the correct internal port via EXPOSE
-
Must define CMD or ENTRYPOINT to start the app
-
Should run your app and any required dependencies (e.g., PostgreSQL)
-
All containers must start and communicate correctly
-
Use Docker Hub or another accessible container registry
-
Tag the image like:
docker tag my-app username/my-app:hw7
docker push username/my-app:hw7I should be able to pull your image using:
docker pull username/my-app:hw7-
The Docker image name & tag
-
App port (e.g. 8080)
-
Required environment variables (e.g., DB_HOST, DB_PORT, etc.)
-
Sample requests or endpoints if available
- Cover the handlers and use cases with unit tests. Minimum coverage: 40%.
- Cover the handlers with integration tests. Minimum test cases: successful execution and (receiving an error due to invalid input data or receiving not found error).
INTEGRATION_TEST=1Prepare a Makefile for each service that includes the following commands:
- starting the test environment using docker-compose,
- running integration tests,
- running unit tests.
After completing all changes, donβt forget to update your Docker Hub images.
- Replacing all HTTP handlers with gRPC service definitions.
- Compatible with tools like grpcui.
- (Bonus) Adding gRPC-Gateway support to allow access via both HTTP/REST and gRPC clients.
.
βββ pkg/
β βββ api/ # Generated gRPC & Gateway code
β βββ service.proto
| | service.pb.go
β βββ service_grpc.pb.go
β βββ service.pb.gw.go
βββ internal/
β βββ service/ # Business logic
β βββ server/
β βββ grpc.go # gRPC server setup
β βββ gateway.go # gRPC-Gateway HTTP server setup
βββ cmd/
β βββ main.go # Entrypoint
βββ go.mod
βββ README.md # You are here
Install protoc plugins if not already:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latestThen generate the code:
protoc -I proto \
--go_out=pkg/api --go_opt=paths=source_relative \
--go-grpc_out=pkg/api --go-grpc_opt=paths=source_relative \
--grpc-gateway_out=pkg/api --grpc-gateway_opt=paths=source_relative \
proto/service.protogrpcurl -plaintext localhost:9090 listcurl "http://localhost:8080/v1/data?id=123"grpcui -plaintext localhost:9090Then open the browser at: http://localhost:8080
- All proto definitions and generated Go code are stored in
pkg/api/. - This project supports both gRPC and REST clients.
- Fully testable with grpcurl and grpcui.
Make sure to rebuild your Docker images after applying the gRPC and gRPC-Gateway changes:
# Example Docker build for the service
docker build -t service_name:hw10 .
β οΈ Donβt forget to update your Dockerfile to install.
In this assignment, we focused on improving observability in our services by implementing:
- Structured Logging using a logging library.
- Distributed Tracing for tracking requests through the system.
- Prometheus Metrics:
failed_requests_total: Count of failed requests.response_time_seconds: Duration of each request.
- Used a structured logger (e.g., Zap) to log meaningful events.
- Included contextual information like:
- HTTP method, path
- Request ID or trace ID
- Error messages (if any)
- Added trace support (e.g., OpenTelemetry, Jaeger).
- Propagated trace context across services.
- Every request should have:
- A trace ID
- Optional span names for internal operations
-
failed_requests_total- Type: Counter
- Increments on every failed HTTP request
-
response_time_seconds- Type: Histogram
- Measures response time of each HTTP request
A log entry:
{
"level": "error",
"msg": "Request failed",
"method": "GET",
"path": "/products",
"status": 500,
"trace_id": "abc123",
"error": "database connection failed"
}
β οΈ Donβt forget to update your Docker images with tag hw11.