Skip to content

wangluyu/gin-project-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gin-project-template

a project template for gin

Todo list

  1. 配置文件 2019.10.25
  2. 日志记录到文件 2019.10.25
  3. mysql
  4. redis
  5. docker部署
  6. swagger文档 2019.10.25
  7. jwt 2019.11.25
  8. 邮件 2019.11.25
  9. 腾讯云短信 2019.11.25

如何使用(How To Use)

配置文件

  • 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.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 调用者的身份
      • 申请安全凭证:
    • sms
      • sign: 短信签名内容,使用 UTF-8 编码,必须填写已审核通过的签名 签名信息可登录短信控制台查看。
      • sdkAppid: 短信SdkAppid在短信控制台添加应用后生成的实际SdkAppid,示例如1400006666。
  • 发送短信
    sms.Send($phone, $templateID, $params)
    

JWT

  • 在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.
    

swagger文档

swag init

  • main.go注释解释

    示例:
    // @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
    

About

a project template for gin

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages