A modern Go microservice template following DDD and Clean Architecture principles.
- gRPC & gRPC Gateway: Primary communication via gRPC with REST support.
- Kafka: Integrated event-driven communication (Producer/Consumer).
- DDD & Clean Architecture: Organized into Domain, Usecase, Infrastructure, and Interface layers.
- Multiple Entrypoints:
cmd/api: Runs gRPC and HTTP Gateway servers.cmd/worker: Runs background Kafka consumers.cmd/cli: Command-line interface using Cobra.
- Documentation:
- Swagger UI at
/swagger - Proto schema at
/proto
- Swagger UI at
- Environment Configuration: Managed via Viper (supports
.yamland env vars). - Local Development: Docker Compose for DB and Kafka.
- Developer & AI Skills: Specialized documentation for AI agents (Junie, Cursor) and developers in the
skills/and.junie/directories.
api/ # Protocol Buffer definitions
cmd/ # Application entry points
api/ # gRPC & Gateway server
worker/ # Kafka consumer
cli/ # Command line interface
configs/ # Configuration files
deployments/ # Docker Compose and K8s manifests
internal/
api/ # Generated gRPC & Gateway code
app/ # Application bootstrap
config/ # Configuration loading
domain/ # Domain entities and interfaces (DDD)
usecase/ # Business logic (Application Services)
infrastructure/ # External implementations (DB, Kafka)
interface/ # Adapters (gRPC, HTTP, Kafka Handlers)
migrations/ # Database migrations
skills/ # AI & Developer skills (patterns and rules)
.junie/ # Junie-specific AI guidelines
- Go 1.25+
- Docker & Docker Compose
buf(for code generation)
-
Clone the repository:
git clone <repo-url> cd go-api-template
-
Start local infrastructure:
make compose-up
-
Generate code:
make generate
-
Run migrations:
make migrate-up
- API Server:
make run-api - Worker:
make run-worker - CLI:
make run-cli user create "John Doe" "john@example.com"
- Define Proto: Add a new
.protofile inapi/or update existing ones. - Generate Code: Run
make generate. - Domain Layer: Define your entity in
internal/domain/. - Infrastructure Layer: Implement the repository interface in
internal/infrastructure/db/postgres/. - Usecase Layer:
- Implement the business logic in a new struct in
internal/usecase/your_domain/. - The struct should implement the
usecase.UseCase[I, O]interface with aDomethod.
- Implement the business logic in a new struct in
- Register Usecase:
- Add a new entry to the
UseCaseIDenum ininternal/usecase/interface.go. - Register your usecase in
internal/app/app.gowithin theregisterHandlersmethod by adding it to theHandlersmap usingusecase.NewHandler.
- Add a new entry to the
- Interface Layer:
- Use the registered handler in gRPC handler (
internal/interface/grpc/) by callingapp.GetHandler(usecase.YourUseCaseID). - (Optional) Register it in gRPC gateway in
internal/interface/http/server.go. - (Optional) Add Kafka consumer in
internal/interface/kafka/and register ininternal/app/worker.go. - (Optional) Add CLI command in
internal/interface/cli/and register ininternal/app/cli.go.
- Use the registered handler in gRPC handler (
- Swagger UI: Rendered UI is available at
http://localhost:8080/swagger/. It uses the generated Swagger JSON files. - Proto Files: Served at
http://localhost:8080/proto/.
cmd/apireceives aCreateUserrequest.usecasesaves the user to PostgreSQL viaUserRepository.usecasepublishes aUserCreatedevent viaEventProducer.cmd/worker(the consumer app) receives the event and logs it (or performs other actions like sending an email).
The CLI provides management commands.
# Create a user
go run cmd/cli/main.go user create "Alice" "alice@example.com"
# Reset password (example command)
go run cmd/cli/main.go user reset-password "alice@example.com"- Domain: Contains entities and repository interfaces. No external dependencies.
- Usecase: Implements business logic using domain entities and interfaces.
- Infrastructure: Implements repository interfaces using specific technologies (PostgreSQL, Kafka).
- Interface: Adapts external requests (gRPC, HTTP, Kafka) to usecases.