a project template for gin
配置文件2019.10.25日志记录到文件2019.10.25- mysql
- redis
- docker部署
swagger文档2019.10.25jwt2019.11.25邮件2019.11.25腾讯云短信2019.11.25
- 在
config/app.yaml
指定evn
的值,evn
的值为配置文件名称 - 配置文件每增加一个配置项,需要在
pkg/util/conf.go
里添加/修改对应的struct - 获取配置项
// 获取app配置 appConf, err := util.FetchAppConf() // 获取自定义配置 conf, err := util.FetchConf() mysqlConf := conf.Mysql mysqlHost := conf.Mysql.Host // mysqlConf.Host
- 在config/app.yaml配置mail和mailProduct
- mail
- from: 发送邮件的邮箱地址
- host: smtp服务器,根据from定义
- port: smtp服务器端口
- password: 发送邮箱的登陆密码
- mailProduct
- name: 落款名
- link: 公司主页
- logo: 公司logo
- copyright: Copyright 显示在邮件最下方
- mail
- 生成邮件以及发送邮件
// mail.Welcome为自定义模版 // 模版参考https://github.com/matcornic/hermes email := new(mail.Welcome) // 生成邮件内容 body := mail.Generate(email.Email(...$params)) // 发送邮件 mail.Send($subject, body, $to)
- 在config/app.yaml配置tencent和sms
- tencent
- secretId: 用于标识 API 调用者身份
- secretKey: 用于验证 API 调用者的身份
- 申请安全凭证:
- 在 腾讯云管理中心控制台-云API密钥 页面,单击【新建密钥】即可以创建一对密钥。
- sms
- tencent
- 发送短信
sms.Send($phone, $templateID, $params)
- 在config/app.yaml配置jwt
- expire: token过期时间 单位为小时
- issuer: 字符串
- 生成token方法详见app/api/v1/auth.go
- 使用方法详见router
- 在
config
指定logPath
的值,logPath
的值为日志目录 - info日志文件名称形如20191025.log,error日志文件名称形如20191025.log.wf
- 记录日志
// 记录info日志 log.Info["hello"] = "world" log.Info["great"] = "wall" // 输出日志文本为: time="2019-10-25T18:29:39+08:00" level=info hello=world great=wall // 记录warning日志 _ = log.Warn("You should probably take a look at this") // 输出日志文本为: time="2019-10-25T18:28:44+08:00" level=warning msg=You should probably take a look at this // 记录error日志 _ = log.Error("Something failed but I'm not quitting.") // 输出日志文本为: time="2019-10-25T18:28:44+08:00" level=error msg=Something failed but I'm not quitting.
swag init
-
main.go注释解释
@title
文档名称@version
文档版本@description
描述@host
访问地址@BasePath
例:/api/v1@license.name
Apache 2.0@license.url
http://www.apache.org/licenses/LICENSE-2.0.html
示例: // @title gin-project-template // @version 1.0 // @description gin-project-template demo // @license.name Apache 2.0 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html // @host 127.0.0.1:8080 // @BasePath /api/v1
-
api注释解释
@Summary
简介@Description
解释@Tags
api组名@Produce
Response content type@Param
参数名称 path/query/body 参数类型 是否必须true/false "参数解释"@Success
200 {object} struct 用于展示success结果@Failure
400 {object} struct 用于展示error结果@Router
路由 [get/post/put/del/...]
示例: // @title gin-project-template // @version 1.0 // @description gin-project-template demo // @license.name Apache 2.0 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html // @host 127.0.0.1:8080 // @BasePath /api