分享
  1. 首页
  2. 文章

Gin框架与《Web Development with Go》实践(二)

Cyberpunk_ZYM · · 2222 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

使用gin改写"Section Building RESTful APIs"

第三方包

需要提前准备的包有:

  • "gopkg.in/gin-gonic/gin.v1"

  • "gopkg.in/mgo.v2"

  • "gopkg.in/mgo.v2/bson"

  • "github.com/dgrijalva/jwt-go"

  • "github.com/dgrijalva/jwt-go/request"

应用结构

见"实践(一)"

数据模型

数据模型对应mongodb中的文档类型。(此处无需用到gin)

import (
 "gopkg.in/mgo.v2/bson"
 "time"
 )
 type (
 User struct {
 Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
 FirstName string `json:"firstname"`
 LastName string `json:"lastname"`
 Email string `json:"email"`
 Password string `json:"password,omitempty"`
 HashPassword []byte `json:"hashpassword,omitempty"`
 }
 Task struct {
 Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
 CreatedBy string `json:"createdby"`
 Name string `json:"name"`
 Description string `json:"description"`
 CreatedOn time.Time `json:"createdon,omitempty"`
 Due time.Time `json:"due,omitempty"`
 Status string `json:"status,omitempty"`
 Tags []string `json:"tags,omitempty"`
 }
 TaskNote struct {
 Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
 TaskId bson.ObjectId `json:"taskid"`
 Description string `json:"description"`
 CreatedOn time.Time `json:"createdon,omitempty"`
 }
 )

RESTful APIs的资源建模

以资源"task"为例,在RESTful APIs表中展示了其相应的资源路径和操作

资源"task"的路由

src/taskmanager2/routers2/task.go

package routers2
import (
 "gopkg.in/gin-gonic/gin.v1"
 "taskmanager2/controllers2"
 "taskmanager2/common2"
)
// SetTaskRoutes configures routes for task entity
func SetTaskRoutes(router *gin.Engine) *gin.Engine {
 taR := router.Group("/tm2/tasks")
 taR.Use(common2.Authorize())
 {
 taR.POST("", controllers2.CreateTask)
 taR.PUT(":id", controllers2.UpdateTask)
 taR.DELETE(":id", controllers2.DeleteTask)
 taR.GET("", controllers2.GetTasks)
 taR.GET("t/:id/", controllers2.GetTaskByID)
 taR.GET("users/:email/", controllers2.GetTasksByUser)
 }
 return router
}

使用了gin的POST、PUT、DELETE和GET功能、router group功能,中间件功能,

注意:在GET路径"t/:id/"处,需要加上"t"以示区别;否则会报出路径冲突的错误。具体原因是与其源代码中借用的httprouter项目里的路径分析部分的代码有关;目前,我还未找到更合适的解决办法。

添加具体路径的中间件

"taR.Use()"中使用Use()函数添加中间件;还可以针对某一个具体的路径的具体方法添加中间件,具体用法请查阅官网文档。

RESTful API的初始化

src/taskmanager2/routers2/router.go
函数InitRoutes()需要使用gin进行改写。

package routers2
import (
 "gopkg.in/gin-gonic/gin.v1"
)
func InitRoutes() *gin.Engine {
 router := gin.Default()
 // Routes for the User entity
 router = SetUserRoutes(router)
 // Routes for the Task entity
 router = SetTaskRoutes(router)
 // Routes for the TaskNote entity
 router = SetNoteRoutes(router)
 return router
}

有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:Cyberpunk_ZYM

查看原文:Gin框架与《Web Development with Go》实践(二)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
2222 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏