gin-kit 是一个基于 Gin 的 Go 后端基础项目脚手架,面向“新项目开箱即用”的场景设计。它保留了常见服务端项目最容易重复搭建的基础能力,让新业务可以直接在稳定骨架上继续开发。
Gin路由与中间件链Wire依赖注入Viper配置加载与环境变量覆盖Zap + Lumberjack日志MySQL + Redis + Elasticsearch初始化PostgreSQL备用接入实现- 启动初始化器与周期任务 ticker
pprof- 优雅停机
- 优雅重启
- 事务封装
GET /healthGET /pingGET /examplePOST /example
example 模块是一个最小示例,用来展示 api -> service -> biz -> data 的分层接线方式,不承载具体业务语义。
- 复制
configs/config.yaml.example为configs/config.yaml - 修改数据库、Redis、Elasticsearch 连接信息
- 执行以下命令
make init
make tidy
make wire
make test
make run如果本机没有 make,也可以直接执行:
go mod tidy
go generate ./cmd
go test ./...
go run ./cmd- 当前默认启用的是
MySQL PostgreSQL代码和配置项已经保留在脚手架中,但还没有切换成默认注入- 如果新项目更偏向
PostgreSQL,可以把Wire注入从NewMysqlClient切换为NewPgClient
pprof已集成,通过server.pprof.enabled和server.pprof.port控制- 优雅停机已集成,监听
SIGINT和SIGTERM - 优雅重启已集成,监听
SIGHUP - 收到
SIGHUP后会拉起新进程并继承监听 socket,旧进程会在新进程就绪后退出 - 零停机重启基于
github.com/cloudflare/tableflip - Linux 和 macOS 支持零停机重启;Windows 下会自动退化为仅优雅停机
- 如果要验证优雅重启,建议使用构建后的二进制,而不是
go run
gin-kit/
├── api/
│ ├── example.go
│ └── health.go
├── cmd/
│ ├── main.go
│ ├── wire.go
│ └── wire_gen.go
├── configs/
│ ├── dev/
│ │ └── config.yaml
│ ├── prod/
│ │ └── config.yaml
│ ├── sit/
│ │ └── config.yaml
│ ├── config.yaml.example
│ ├── config.yaml.backup
│ └── config.yaml.prod
├── deploy/
│ └── README.md
├── docs/
│ ├── ARCHITECTURE.md
│ ├── DEVELOPMENT.md
│ ├── GRACEFUL_SHUTDOWN.md
│ ├── MAKEFILE.md
│ └── README.md
├── internal/
│ ├── biz/
│ │ ├── biz.go
│ │ ├── example.go
│ │ └── startup.go
│ ├── conf/
│ │ └── conf.go
│ ├── data/
│ │ ├── database/
│ │ │ ├── database.go
│ │ │ ├── db.go
│ │ │ ├── elasticsearch.go
│ │ │ ├── mysql.go
│ │ │ ├── postgres.go
│ │ │ ├── redis.go
│ │ │ └── transaction.go
│ │ ├── model/
│ │ │ ├── base.go
│ │ │ └── example.go
│ │ ├── data.go
│ │ └── example.go
│ ├── pkg/
│ │ ├── gormtype/
│ │ │ └── time.go
│ │ ├── response/
│ │ │ └── response.go
│ │ ├── startup/
│ │ │ ├── init.go
│ │ │ └── ticker.go
│ │ └── README.md
│ └── service/
│ ├── example.go
│ └── service.go
├── middleware/
│ └── middleware.go
├── pkg/
│ ├── const/
│ │ └── const.go
│ ├── logger/
│ │ └── logger.go
│ ├── tool/
│ │ ├── README.md
│ │ └── tool.go
│ └── README.md
├── router/
│ ├── app.go
│ └── router.go
├── scripts/
│ ├── sql/
│ │ └── init_postgres.sql
│ ├── init-config.ps1
│ └── kill-port.ps1
├── third_party/
│ └── README.md
├── .gitignore
├── LICENSE
├── Makefile
├── go.mod
├── go.sum
└── README.md
api/请求与响应 DTO 定义,保持轻量,不放业务逻辑。cmd/应用入口与Wire注入入口。configs/配置样例和不同环境的占位配置。docs/架构、开发、停机重启、Makefile 等文档说明。internal/biz/用例层,负责组合业务流程、封装事务边界、组织后台任务。internal/conf/配置加载入口。internal/data/仓储实现、数据库访问、模型定义。internal/pkg/仅供项目内部使用的基础能力。internal/service/HTTP handler,负责请求绑定、参数校验、响应组织。middleware/请求链路中间件。pkg/可被多个模块复用的公共能力。router/路由注册与Ginapp 初始化。scripts/本地开发脚本与初始化 SQL。
- 先在
api/定义输入输出结构 - 在
internal/data/model/定义模型 - 在
internal/data/实现 repo - 在
internal/biz/实现 use case - 在
internal/service/暴露 handler - 在
router/注册接口 - 执行
make wire和make test
gin-kit is a Go backend project scaffold built on top of Gin. It is designed as a reusable starting point for new services, with the common infrastructure already wired in.
Ginrouting and middleware chainWiredependency injectionViperconfig loading and environment overridesZap + LumberjackloggingMySQL + Redis + Elasticsearchinitialization- Optional
PostgreSQLintegration - Startup initializer and background ticker tasks
pprof- Graceful shutdown
- Graceful restart
- Transaction wrapper
GET /healthGET /pingGET /examplePOST /example
The example module is intentionally minimal and exists only to demonstrate the api -> service -> biz -> data layering pattern.
- Copy
configs/config.yaml.exampletoconfigs/config.yaml - Update database, Redis, and Elasticsearch settings
- Run:
make init
make tidy
make wire
make test
make runIf make is not available on your machine, you can use raw Go commands instead:
go mod tidy
go generate ./cmd
go test ./...
go run ./cmdMySQLis the default active database clientPostgreSQLcode and config are preserved, but it is not the default injected client yet- If a new project prefers
PostgreSQL, switch theWirebinding fromNewMysqlClienttoNewPgClient
pprofis integrated and controlled byserver.pprof.enabledandserver.pprof.port- Graceful shutdown listens for
SIGINTandSIGTERM - Graceful restart listens for
SIGHUP - On restart, a child process inherits the listeners and the old process exits after the child becomes ready
- Zero-downtime restart is implemented with
github.com/cloudflare/tableflip - Linux and macOS support zero-downtime restarts; Windows falls back to graceful shutdown only
- Use a built binary instead of
go runwhen verifying graceful restart behavior
See the expanded tree above. The main structure is:
api/for DTOscmd/for the entrypoint andWireinternal/biz/for use casesinternal/data/for repositories and modelsinternal/service/for HTTP handlersmiddleware/for request middlewarepkg/for reusable shared packagesrouter/for route wiringconfigs/for config templatesdocs/for project documentation