A project template for creating new Kratos services with HTTP and gRPC transports, protobuf-first APIs, Wire dependency injection, OpenAPI generation, and a small CRUD example.
Use this repository as a starting point for a new service. The included sample resource is only reference code for API shape, layering, code generation, and testing. Replace it with your own domain model when creating a real project.
- Copy or generate a repository from this template.
- Update the Go module path:
go mod edit -module github.com/your-org/your-service- Replace existing import paths that reference this template module.
- Rename the command, service metadata, and sample API package to match your service.
- Replace the sample CRUD resource with your own resource.
- Regenerate code and verify the project:
make all
go test ./...- Kratos HTTP and gRPC server setup.
- Protobuf API definitions and generated Go code.
- OpenAPI generation.
- Wire-based dependency injection.
- Layered
service,biz, anddatapackages. - A lightweight in-memory repository for the sample resource.
- Unit tests for the service layer.
- Server-streaming and bidirectional-streaming examples.
api/ Protobuf APIs and generated bindings
cmd/ Application entrypoints
configs/ Local configuration
internal/server/ HTTP and gRPC server construction
internal/service/ Transport-facing service methods
internal/biz/ Usecases, entities, errors, repository interfaces
internal/data/ Repository implementations
third_party/ Protobuf dependencies
openapi.yaml Generated OpenAPI document
The sample CRUD API demonstrates common conventions for Kratos projects:
- Resource-oriented methods: create, get, list, update, delete.
- HTTP annotations with
google.api.http. - Required fields with
google.api.field_behavior. - List requests with
page_size,page_token,filter, andorder_by. - Pagination with
go.einride.tech/aip/pagination. - Partial updates with
google.protobuf.FieldMaskandfieldmask.Update. - Streaming RPC definitions for one-way and bidirectional streams.
The in-memory data layer intentionally stays simple. It demonstrates flow across layers, but does not implement a full query engine. Real repositories can apply parsed filters and ordering in SQL, Ent, or another storage layer.
Install generators:
make initRegenerate API bindings and OpenAPI:
make apiRegenerate config protobufs:
make configRun all generation steps, Wire, and module cleanup:
make allBuild:
make buildTest:
go test ./...go run ./cmd/server -conf ./configsDefault local ports are configured in configs/config.yaml:
- HTTP:
0.0.0.0:8000 - gRPC:
0.0.0.0:9000
docker build -t <your-image-name> .
docker run --rm -p 8000:8000 -p 9000:9000 \
-v </path/to/your/configs>:/data/conf \
<your-image-name>