This project implements a microservice for managing products and subscription plans using gRPC, Golang, and GORM. The microservice is designed to handle various types of products, each with specific fields and associated subscription plans.
- Overview
- Setup Instructions
- Running the gRPC Server
- Example gRPC Client Calls
- Assumptions and Constraints
The microservice exposes the following gRPC services:
- ProductService: Manages products with endpoints to create, retrieve, update, delete, and list products.
- SubscriptionService: Manages subscription plans associated with products, providing endpoints to create, retrieve, update, delete, and list subscription plans.
-
Clone the repository:
git clone https://github.com/yourusername/github.com/IgweDaniel/lithium.git cd github.com/IgweDaniel/lithium
-
Install dependencies:
go mod tidy
-
Set up the database:
- Ensure you have a SQL-based database (PostgreSQL or MySQL) running.
- Update the database connection settings in
pkg/db/connection.go
.
-
Copy the example environment file and update the environment variables:
cp .env.example .env
To run the gRPC server, execute the following command:
go run cmd/server/main.go
The server will start listening for incoming gRPC requests.
There are two ways to run the application with Docker Compose:
- Development Setup (only runs PostgreSQL):
make dev/compose
- Full Application Setup (only runs PostgreSQL):
docker compose up -d This will start both the application and PostgreSQL database with the following configuration: - Application runs on port 8080 - PostgreSQL runs on port 5432 - Default database name: lithium - Default database user: postgres - Default database password: postgres
- View logs (only runs PostgreSQL):
docker compose logs -f
To interact with the gRPC services, you can use the proto-gen
command in the Makefile
to generate client stubs. Here’s an example of how to call the CreateProduct
method:
-
Generate the gRPC client code:
make proto-gen
-
Use the generated client in your application to make calls to the gRPC server.
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() client := pb.NewProductServiceClient(conn) createRequest := &pb.CreateProductRequest{ Name: "New Product", Price: 25.0, } createResp, err := client.CreateProduct(context.Background(), createRequest) if err != nil { log.Fatalf("could not create product: %v", err) } log.Printf("Product created: %v", createResp.Product)
- The microservice is designed to be scalable and maintainable, following clean architecture principles.
- Proper error handling and data validation are implemented.
- The project is dockerized for easy deployment (see Dockerfile for instructions).
Feel free to explore the codebase and contribute to the project!