A production-ready Go microservice template generator based on Kratos framework with Wire dependency injection.
- π Quick Start: Generate production-ready microservices in seconds with presets
- π§ Flexible Configuration: Multiple presets and customizable options
- π― Clean Architecture: Clear separation of layers (cmd/internal/api)
- π Dependency Injection: Wire-based automatic code generation
- ποΈ Database Support: PostgreSQL/MySQL with GORM ORM
- π Distributed ID: Sonyflake integration for unique ID generation
- π Observability: OpenTelemetry tracing support
- π Auto-generation: GORM models from database schema
- π Multiple Templates: Full CRUD or Simple GET operations
- π¦ Production Features: Health checks, middleware, caching, task scheduling
go install github.com/hay-kot/scaffold@v0.12.0Presets provide pre-configured settings for common use cases:
Full-featured microservice with Redis, cache, idempotent, task scheduler, and tracing:
scaffold new https://github.com/go-cinch/layout-v2 \
--output-dir=. \
--run-hooks=always \
--no-prompt \
--preset default \
Project=myserviceFeatures:
- β Full CRUD operations (Create/Get/Find/Update/Delete)
- β GORM ORM with auto-generated models from database
- β Sonyflake distributed ID generator
- β Redis connection support
- β Multi-layer cache system
- β Idempotent middleware (prevent duplicate requests)
- β Task/Cron worker scheduler
- β OpenTelemetry tracing
- β Transaction support with rollback
- β Health check endpoints (HTTP/gRPC)
- β Header middleware
- π¦ Binary size: ~32MB
Generated Structure:
myservice/
βββ api/ # Protobuf definitions
βββ cmd/ # Application entry points
βββ configs/ # Configuration files
βββ internal/
β βββ biz/ # Business logic layer
β βββ data/ # Data access layer (with Sonyflake)
β βββ server/ # HTTP/gRPC servers
β βββ service/ # Service layer
βββ Makefile # Build automation
Lightweight microservice without Redis features:
scaffold new https://github.com/go-cinch/layout-v2 \
--output-dir=. \
--run-hooks=always \
--no-prompt \
--preset simple \
Project=myserviceFeatures:
- β Simple Get operation only
- β GORM ORM with auto-generated models
- β Sonyflake distributed ID generator
- β OpenTelemetry tracing
- β Transaction support
- β Health check endpoints
- β No Redis/Cache/Task features
- π¦ Binary size: ~25MB
Full CRUD operations without any Redis dependencies:
scaffold new https://github.com/go-cinch/layout-v2 \
--output-dir=. \
--run-hooks=always \
--no-prompt \
--preset noredis \
Project=myserviceFeatures:
- β Full CRUD operations
- β GORM ORM with auto-generated models
- β Sonyflake distributed ID generator
- β OpenTelemetry tracing
- β Transaction support
- β Health check endpoints
- β No Redis connection
- β No Cache layer
- β No Idempotent middleware
- β No Task/Cron worker
- π¦ Binary size: ~27MB
| Feature | Default | Simple | NoRedis |
|---|---|---|---|
| Proto Template | Full CRUD | Simple GET | Full CRUD |
| CRUD Operations | β C/R/U/D | β R only | β C/R/U/D |
| Sonyflake ID | β | β | β |
| Database (GORM) | β PostgreSQL | β PostgreSQL | β PostgreSQL |
| Auto-gen Models | β | β | β |
| Transaction | β | β | β |
| Redis | β | β | β |
| Cache Layer | β | β | β |
| Idempotent | β | β | β |
| Task/Cron | β | β | β |
| OpenTelemetry | β | β | β |
| Health Check | β | β | β |
| Binary Size | ~32MB | ~25MB | ~27MB |
| Use Case | Production | Learning | Stateless API |
You can override specific preset options:
# Enable WebSocket on default preset
scaffold new https://github.com/go-cinch/layout-v2 \
--preset default \
Project=myservice \
enable_ws=true
# Use MySQL instead of PostgreSQL
scaffold new https://github.com/go-cinch/layout-v2 \
--preset default \
Project=myservice \
db_type=mysql
# Change HTTP/gRPC ports
scaffold new https://github.com/go-cinch/layout-v2 \
--preset simple \
Project=myservice \
http_port=9090 \
grpc_port=9190
# Customize service name
scaffold new https://github.com/go-cinch/layout-v2 \
--preset default \
Project=game \
service_name=user| Option | Values | Default | Description |
|---|---|---|---|
service_name |
string | Project name | Service name (used in logs, metrics) |
module_name |
string | service_name | Go module name |
http_port |
string | 8080 |
HTTP server port |
grpc_port |
string | 8180 |
gRPC server port |
proto_template |
full/simple |
varies | API template (full CRUD or simple GET) |
db_type |
postgres/mysql |
postgres |
Database type |
orm_type |
gorm/none |
gorm |
ORM framework |
enable_ws |
true/false |
false |
Enable WebSocket support |
enable_redis |
true/false |
varies | Enable Redis connection |
enable_cache |
true/false |
varies | Enable cache layer |
enable_idempotent |
true/false |
varies | Enable idempotent middleware |
enable_task |
true/false |
varies | Enable task/cron scheduler |
enable_trace |
true/false |
true |
Enable OpenTelemetry tracing |
enable_i18n |
true/false |
false |
Enable i18n support |
Answer prompts to configure all options:
scaffold new https://github.com/go-cinch/layout-v2 \
--output-dir=. \
--run-hooks=always \
Project=myservicecd myservice
make all # Install tools, generate proto/wire/config, lintStart PostgreSQL (Docker):
docker run -d --name postgres \
-e POSTGRES_USER=root \
-e POSTGRES_PASSWORD=password \
-p 5432:5432 \
postgres:17Configure in configs/db.yaml:
db:
driver: postgres
dsn: "host=localhost user=root password=password dbname=myservice port=5432 sslmode=disable TimeZone=UTC"
migrate: true # Auto-run migrationsdocker run -d --name redis \
-p 6379:6379 \
redis:7Configure in configs/redis.yaml:
redis:
dsn: "redis://:password@localhost:6379/0"make build # Output: ./bin/myservice./bin/myservice -c ./configsEndpoints:
- HTTP: http://localhost:8080
- gRPC: localhost:8180
- Health: http://localhost:8080/health
# Generate API from proto
make api
# Generate Wire dependency injection
make wire
# Run linter
make lint
# Run tests
make test
# Complete build pipeline
make all
# Clean generated files
make cleanAvoid project names ending with 's' (e.g., users, items, orders)
Why? GORM's gentool singularizes table names incorrectly for names ending in 's':
usersβ generatesUserβ (expectedUsers)ordersβ generatesOrderβ (expectedOrders)
Recommended Naming:
- β
user,order,pay - β
users,items,orders(will cause compilation errors)
If you must use plural names:
Manually correct the generated model struct names in internal/data/model/*.gen.go after generation.
Issues and pull requests are welcome!
Apache 2.0