toutGin
个人风格的golang版RESTful API项目结构,gin+gorm,简单易上手
结构清晰简单,代码洁癖患者的福音
获取源码
项目结构
|-app
|-common 公共方法
|-display.go 统一json格式输出
|-function.go 公共函数
|-controller 控制器
|-dao 负责curd的
|-middleware 中间件
|-model 模型
|-service 核心业务处理
|-config 配置文件和统一路由管理
|-route
|-route.go 路由配置文件
|-message.yml 状态码配置文件
|-config.go 配置方法
|-main.go 程序执行入口
模块调用流程
controller -> service -> dao
# controller严禁复杂业务,严禁直接调用dao,更严禁写sql语句
# 要优雅,不要凌乱,乖~
REST URL
GET http://localhost:8080/v1/user
POST http://localhost:8080/v1/user
PUT http://localhost:8080/v1/user
DELETE http://localhost:8080/v1/user
# api POST,PUT,DELETE 推荐使用 body json 传参,GET兼容 body 和 url 传参
JSON RESULT
{
"status": 10000,
"msg": "请求成功",
"body": null
}
ROUTE 示例
type Route struct {
Engine *gin.Engine
}
//路由路口
func (r *Route) Run() {
r.Engine.Use(new(middleware.RequestMiddleware).InitRequest)
r.index()
r.v1()
}
func (r *Route) index() {
r.Engine.Any("", new(controller.IndexController).Run)
}
func (r *Route) v1() {
v1 := r.Engine.Group("v1")
{
v1.Any("/user", controller.User)
}
}
CONTROLLER 示例
type UserController struct {
display *common.Display
data map[string]interface{}
us *se