项目作者: feizhiwu

项目描述 :
个人风格的go RESTful API项目结构,gin+gorm,简单易上手
高级语言: Go
项目地址: git://github.com/feizhiwu/toutGin.git
创建时间: 2019-10-17T06:50:27Z
项目社区:https://github.com/feizhiwu/toutGin

开源协议:

下载


stage

gin+gorm 面向接口(api)简易小demo,初学者也能快速上手

RESTful架构,结构清晰,传参灵活

项目结构

  1. |-app
  2. |-controller 控制器
  3. |-dao 负责curd
  4. |-middleware 中间件
  5. |-model 模型
  6. |-plugin 常用工具类
  7. |-driver 持久化工具类
  8. |-scene 统一输出工具类
  9. |-function.go 公共函数
  10. |-service 核心业务处理
  11. |-config 配置文件和统一路由管理
  12. |-conf
  13. |-conf.go 配置方法
  14. |-route
  15. |-route.go 路由配置文件
  16. |-config.yml 统一环境配置文件
  17. |-message.yml 统一状态码配置文件
  18. |-main.go 程序执行入口

RESTful API curl

  1. 添加用户:
  2. curl --location --request POST 'http://localhost:8080/v1/user' \
  3. --header 'action: add' \
  4. --header 'Content-Type: application/json' \
  5. --data-raw '{
  6. "name":"tout",
  7. "password":"123"
  8. }'
  9. 用户列表:
  10. curl --location --request GET 'http://localhost:8080/v1/user?page=1&limit=10' \
  11. --header 'action: list'
  12. 用户详情:
  13. curl --location --request GET 'http://localhost:8080/v1/user?id=1' \
  14. --header 'action: info'
  15. 修改用户:
  16. curl --location --request PUT 'http://localhost:8080/v1/user' \
  17. --header 'action: update' \
  18. --header 'Content-Type: application/json' \
  19. --data-raw '{
  20. "id":1,
  21. "password":"123456"
  22. }'
  23. 删除用户:
  24. curl --location --request DELETE 'http://localhost:8080/v1/user' \
  25. --header 'action: delete' \
  26. --header 'Content-Type: application/json' \
  27. --data-raw '{
  28. "id":1
  29. }'

注:一个router下的header action需与controller方法同名,联调时可以通过action快速定位接口

返参示例

  1. {
  2. "status": 10000,
  3. "msg": "请求成功",
  4. "body": null
  5. }

测试user表结构

  1. CREATE TABLE `user` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) DEFAULT NULL,
  4. `password` varchar(255) DEFAULT NULL,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;