Skip to content

jumayevgadam/golang-microservice-practise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

65 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Cart and stock Services

Overview

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.

Getting Started

Build

make build

Run All Services

make run

Starts cart, and stocks services.

Run Linter Services

make lint

Service Endpoints

Both services expose HTTP endpoints on port 8080 (cart) and 8081 (stocks).

Inter-Service Communication

  • Uses HTTP

Testing

  • Unit tests for core business logic:
    make test 

Documentation

Create service skeletons for cart and stocks services according to the documentation.


1. Write a Dockerfile

  • 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

2. Write a docker-compose.yml

  • Should run your app and any required dependencies (e.g., PostgreSQL)

  • All containers must start and communicate correctly

3. Push your app's Docker image to a public registry

  • 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:hw7

I should be able to pull your image using:

    docker pull username/my-app:hw7

4. Document the following in every service's README:

  • 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

TESTING

Requirements:

  • 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=1

Prepare 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.

KAFKA

Homework 10

βœ… Task Overview

  • 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.

🧱 Project Structure

.
β”œβ”€β”€ 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

πŸ§ͺ How to Generate Code from .proto

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@latest

Then 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.proto

🧰 Testing the Service

gRPC Call (CLI)

grpcurl -plaintext localhost:9090 list

Bonus HTTP Call (REST via gRPC-Gateway)

curl "http://localhost:8080/v1/data?id=123"

UI Test (gRPC UI)

grpcui -plaintext localhost:9090

Then open the browser at: http://localhost:8080

🧾 Notes

  • 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.

🐳 Docker Instructions

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.


Logging, Tracing, and Metrics

Overview

In this assignment, we focused on improving observability in our services by implementing:

  1. Structured Logging using a logging library.
  2. Distributed Tracing for tracking requests through the system.
  3. Prometheus Metrics:
    • failed_requests_total: Count of failed requests.
    • response_time_seconds: Duration of each request.

Objectives

Logging

  • 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)

Tracing

  • 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

Metrics

  1. failed_requests_total

    • Type: Counter
    • Increments on every failed HTTP request
  2. response_time_seconds

    • Type: Histogram
    • Measures response time of each HTTP request

πŸ’‘ Example

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •