go语言web开发系列之二:gin框架接口站统一返回restful格式的数据

一,演示项目的相关信息:

1,项目地址:

https://github.com/liuhongdi/digv02

2,功能:演示gin返回restful格式的数据,

             包括异常时的404/500等情况

3,项目结构:如图:

二,sql代码说明

  1.  
    CREATE TABLE `article` (
  2.  
    `articleId` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
  3.  
    `type` tinyint unsigned NOT NULL DEFAULT '0' COMMENT '类型',
  4.  
    `subject` varchar(500) NOT NULL DEFAULT '' COMMENT '标题',
  5.  
    `addTime` datetime NOT NULL DEFAULT '2020-11-19 00:00:00' COMMENT '添加时间',
  6.  
    `isHot` tinyint unsigned NOT NULL DEFAULT '0' COMMENT '是否热榜,0:非热,1,热',
  7.  
    `url` varchar(500) NOT NULL DEFAULT '' COMMENT '链接地址',
  8.  
    `domain` varchar(100) NOT NULL DEFAULT '' COMMENT '域',
  9.  
    `userId` bigint unsigned NOT NULL DEFAULT '0' COMMENT '推荐用户',
  10.  
    `approvalStaffId` int unsigned NOT NULL DEFAULT '0' COMMENT '批准员工',
  11.  
    `digSum` int unsigned NOT NULL DEFAULT '0' COMMENT '被顶的次数',
  12.  
    `commentSum` int unsigned NOT NULL DEFAULT '0' COMMENT '被评论的次数',
  13.  
    `isPublish` tinyint NOT NULL DEFAULT '0' COMMENT '0,未发布,1,已发布',
  14.  
    PRIMARY KEY (`articleId`),
  15.  
    KEY `isPublish` (`isPublish`)
  16.  
    ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='文章表'

插入6条演示数据:

  1.  
    INSERT INTO `article` (`articleId`, `type`, `subject`, `addTime`, `isHot`, `url`, `domain`, `userId`, `approvalStaffId`, `digSum`, `commentSum`, `isPublish`) VALUES
  2.  
    (1, 0, '【最近,南非发现一座大油田】石油和天然气两种重要资源是南非储量的最短板,“贫油”的帽子也一直扣在南非的头上摘不下来。可就在最近,在南非海域进行油气勘探已久的道达尔透露了其新的勘探成果:在距离南非南部海岸约175公里的奥特尼夸盆地的11B/12B地区,再次发现了重要的凝析气,可能蕴藏着大量天然气及原油。', '2020-11-19 00:00:00', 1, 'https://mp.weixin.qq.com/s/1btbmouH-2KuIHUMoucq2w', '', 1, 1, 0, 0, 1),
  3.  
    (2, 1, '让喵喵来开启周五的早晨吧!', '2020-11-19 00:00:00', 0, 'https://m.weibo.cn/status/4573112073720433?', 'm.weibo.cn', 0, 0, 0, 0, 1),
  4.  
    (3, 0, '汤姆·赫兰德、黛茜·雷德利、麦斯·米科尔森、尼克·乔纳斯主演的《混沌漫步》公开预告。影片由《明日边缘》导演道格·里曼执导,暂时定档明年1月22日上映。', '2020-11-19 00:00:00', 1, 'http://news.mtime.com/2020/11/19/1604795.html', 'news.mtime.com', 0, 0, 0, 0, 1),
  5.  
    (4, 1, '扫地机器人这个东西确实方便,大多数时候把房间扫的比较干净,但还有很多边边角角清扫不上,希望厂家能够在app上显示出机器人的路线图,明确告知哪些路段没有照顾到,要不然就再开发一个项目经理机器人,跟在扫地机器人屁股后面监督。//@大窑儿:可以弄个步数排行榜,让机器人们互相点赞,揣摩,攀比,内卷,无意义竞争。', '2020-11-19 00:00:00', 0, '', '', 0, 0, 0, 0, 1),
  6.  
    (5, 0, '【世卫组织建议医生不要使用瑞德西韦治疗新冠】世卫组织指导小组表示,证据显示,瑞德西韦对提高新冠肺炎患者的存活率没有显著影响。这项建议发表在上周五的《英国医学杂志》中。', '2020-11-19 00:00:00', 0, 'https://www.thepaper.cn/newsDetail_forward_10067542', 'thepaper.cn', 0, 0, 0, 0, 1),
  7.  
    (6, 0, '11月19日0—24时,31个省(自治区、直辖市)和新疆生产建设兵团报告新增确诊病例17例,均为境外输入病例(福建6例,上海4例,陕西3例,广东2例,北京1例,四川1例);无新增死亡病例;新增疑似病例1例,为本土病例(在天津)。', '2020-11-19 00:00:00', 0, 'http://m.news.cctv.com/2020/11/20/ARTIIR9o72TuDF80s6hY2IvD201120.shtml', 'm.news.cctv.com', 0, 0, 0, 0, 1);

三,go代码说明

1,global/result.go

  1.  
    package global
  2.  
     
  3.  
    import (
  4.  
    "github.com/gin-gonic/gin"
  5.  
    "net/http"
  6.  
    )
  7.  
     
  8.  
    type Result struct {
  9.  
    Ctx *gin.Context
  10.  
    }
  11.  
     
  12.  
    //返回的结果:
  13.  
    type ResultCont struct {
  14.  
    Code int `json:"code"` //提示代码
  15.  
    Msg string `json:"msg"` //提示信息
  16.  
    Data interface{} `json:"data"` //数据
  17.  
    }
  18.  
     
  19.  
    func NewResult(ctx *gin.Context) *Result {
  20.  
    return &Result{Ctx: ctx}
  21.  
    }
  22.  
     
  23.  
    //成功
  24.  
    func (r *Result) Success(data interface{}) {
  25.  
    if (data == nil) {
  26.  
    data = gin.H{}
  27.  
    }
  28.  
    res := ResultCont{}
  29.  
    res.Code = 0
  30.  
    res.Msg = ""
  31.  
    res.Data = data
  32.  
    r.Ctx.JSON(http.StatusOK,res)
  33.  
    }
  34.  
     
  35.  
    //出错
  36.  
    func (r *Result)Error(code int,msg string) {
  37.  
    res := ResultCont{}
  38.  
    res.Code = code
  39.  
    res.Msg = msg
  40.  
    res.Data = gin.H{}
  41.  
    r.Ctx.JSON(http.StatusOK,res)
  42.  
    }

2,controller/articleController.go

  1.  
    package controller
  2.  
     
  3.  
    import (
  4.  
    "fmt"
  5.  
    "github.com/gin-gonic/gin"
  6.  
    "github.com/liuhongdi/digv02/dao"
  7.  
    "github.com/liuhongdi/digv02/global"
  8.  
    "github.com/liuhongdi/digv02/model"
  9.  
    "github.com/liuhongdi/digv02/service"
  10.  
    "strconv"
  11.  
    )
  12.  
     
  13.  
    type ArticleController struct{}
  14.  
     
  15.  
    func NewArticleController() ArticleController {
  16.  
    return ArticleController{}
  17.  
    }
  18.  
    //得到一篇文章的详情
  19.  
    func (a *ArticleController) GetOne(c *gin.Context) {
  20.  
    result := global.NewResult(c)
  21.  
    id := c.Params.ByName("id")
  22.  
    fmt.Println("id:"+id);
  23.  
     
  24.  
    articleId,err := strconv.ParseInt(id, 10, 64);
  25.  
    if (err != nil) {
  26.  
    result.Error(400,"参数错误")
  27.  
    fmt.Println(err.Error())
  28.  
    return
  29.  
    }
  30.  
     
  31.  
    if (articleId == 100) {
  32.  
    var z int = 0
  33.  
    var i int = 100 / z
  34.  
    fmt.Println("i:%i",i)
  35.  
    }
  36.  
     
  37.  
    articleOne,err := service.GetOneArticle(articleId);
  38.  
    if err != nil {
  39.  
    result.Error(404,"数据查询错误")
  40.  
    } else {
  41.  
    result.Success(&articleOne);
  42.  
    }
  43.  
    return
  44.  
    }
  45.  
     
  46.  
    //得到多篇文章,按分页返回
  47.  
    func (a *ArticleController) GetList(c *gin.Context) {
  48.  
    result := global.NewResult(c)
  49.  
    page := c.DefaultQuery("page", "1")
  50.  
    pageInt, err := strconv.Atoi(page)
  51.  
    if (err != nil) {
  52.  
    //c.AbortWithStatus(400)
  53.  
    result.Error(400,"参数错误")
  54.  
    fmt.Println(err.Error())
  55.  
    return
  56.  
    }
  57.  
    pageSize := 2;
  58.  
    pageOffset := (pageInt-1) * pageSize
  59.  
     
  60.  
    articles,err := service.GetArticleList(pageOffset,pageSize)
  61.  
    if err != nil {
  62.  
    //c.AbortWithStatus(404)
  63.  
    result.Error(404,"数据查询错误");
  64.  
    fmt.Println(err.Error())
  65.  
    } else {
  66.  
    sum,_ := dao.SelectcountAll()
  67.  
    pageInfo,_ := model.GetPageInfo(pageInt,pageSize,sum)
  68.  
    result.Success(gin.H{"list":&articles,"pageinfo":pageInfo});
  69.  
    }
  70.  
    return
  71.  
    }

3,router/router.go

  1.  
    package router
  2.  
     
  3.  
    import (
  4.  
    "github.com/gin-gonic/gin"
  5.  
    "github.com/liuhongdi/digv02/controller"
  6.  
    "github.com/liuhongdi/digv02/global"
  7.  
    "log"
  8.  
    "runtime/debug"
  9.  
    )
  10.  
     
  11.  
    func Router() *gin.Engine {
  12.  
    router := gin.Default()
  13.  
    //处理异常
  14.  
    router.NoRoute(HandleNotFound)
  15.  
    router.NoMethod(HandleNotFound)
  16.  
    router.Use(Recover)
  17.  
     
  18.  
    // 路径映射
  19.  
    articlec:=controller.NewArticleController()
  20.  
    router.GET("/article/getone/:id", articlec.GetOne);
  21.  
    router.GET("/article/list", articlec.GetList);
  22.  
    return router
  23.  
    }
  24.  
     
  25.  
    //404
  26.  
    func HandleNotFound(c *gin.Context) {
  27.  
    global.NewResult(c).Error(404,"资源未找到")
  28.  
    return
  29.  
    }
  30.  
     
  31.  
    //500
  32.  
    func Recover(c *gin.Context) {
  33.  
    defer func() {
  34.  
    if r := recover(); r != nil {
  35.  
    //打印错误堆栈信息
  36.  
    log.Printf("panic: %v ", r)
  37.  
    debug.PrintStack()
  38.  
    global.NewResult(c).Error(500,"服务器内部错误")
  39.  
    }
  40.  
    }()
  41.  
    //继续后续接口调用
  42.  
    c.Next()
  43.  
    }

4,其他代码可以访问github上查看

四,测试效果

1,正常的输出

   访问:

http://127.0.0.1:8080/article/list?page=1

返回:

2,无效地址的访问:404

访问一个不存在的地址:如:

http://127.0.0.1:8080/abc

返回:

3,传递有问题的参数引发内部错误:500

例如:访问:

http://127.0.0.1:8080/article/getone/100

返回:

五,查看库的版本:

  1.  
    module github.com/liuhongdi/digv02
  2.  
     
  3.  
    go 1.15
  4.  
     
  5.  
    require (
  6.  
    github.com/gin-gonic/gin v1.6.3
  7.  
    github.com/jinzhu/gorm v1.9.16
  8.  
    )
原文地址:https://www.cnblogs.com/ExMan/p/14312148.html