How nhuson/microservice-grpc-gin-golang works
Project Type and Purpose
• This is a Go‑based microservices system combining Gin (HTTP REST) and gRPC.
• It implements three services: a Gateway (REST façade) and two backend services (Posts, Users), each with its own private database (“sidecar” pattern).
• Purpose: expose a unified REST API to clients, forward calls internally via gRPC to dedicated services, each handling its own data and persistence.
File Structure Analysis
• Top‑level:
• core/ shared configs, DB connection logic, helper functions, and common protobuf definitions
• services/ contains three subfolders: gateway, posts, users
• Dockerfile, docker-compose.yaml, Jenkinsfile for containerization and CI/CD
• main.go at root is likely a bootstrap or older entry point (services each have their own main.go)
• services/gateway:
• handler/ Gin handlers for HTTP endpoints (posts.go, users.go)
• routers/ router definitions mapping paths to handlers
• middleware/ auth, logging, etc.
• validator/ request validation logic
• services/posts and services/users:
• handler/ gRPC server handlers
• repo/ database access layer (entity, CRUD methods)
• pb/ protocol buffer definitions (.proto) and generated Go code
Insights from README
• Each backend service communicates via gRPC.
• The gateway exposes REST endpoints on port 8080.
Main Components
• External Client (mobile app, browser, other services)
• API Gateway Service (Gin HTTP server)
• Posts Service (gRPC server + private DB)
• Users Service (gRPC server + private DB)
• Shared Core Library (configs, helpers, common protobuf schemas)
• CI/CD Pipeline (Jenkins)
• Container Infrastructure (Docker, docker‑compose, Kubernetes or Docker host)
Relationships and Data Flows
• External Client sends HTTP requests to API Gateway over REST.
• Gateway middleware authenticates and validates, then routes to appropriate HTTP handler.
• Handlers invoke gRPC clients (auto‑generated from core/pb) to the Posts or Users service.
• Posts/Users service handlers receive gRPC calls, use repo layer to read/write their private database.
• Responses flow back gRPC → Gateway → HTTP response to client.
Architectural Patterns and Principles
• Microservice architecture (each service is independently deployable, owns its data store).
• Sidecar pattern (each service encapsulates its own database).
• API Gateway pattern (REST to gRPC translation, central authentication).
• Hexagonal/Onion style inside each service (handler → business logic → repo).
• Protocol Buffers for IDL and message serialization.
Key Technologies and Frameworks
• Go language
• Gin Gonic for REST in Gateway
• gRPC + Protobuf for internal RPC
• Docker & docker‑compose for containerization
• Jenkins for CI/CD
• Gopkg.toml (dep) for dependency management