Golang GinWeb框架9-編譯模板/自定義結(jié)構(gòu)體綁定/http2/操作Cookie
簡(jiǎn)介
本文接著上文(Golang GinWeb框架8-重定向/自定義中間件/認(rèn)證/HTTPS支持/優(yōu)雅重啟等)繼續(xù)探索GinWeb框架.
將模板文件一起編譯為一個(gè)二進(jìn)制單文件
使用go-assets, 你可以將模板文件和服務(wù)一起編譯為一個(gè)二進(jìn)制的單文件, 可以方便快捷的部署該服務(wù). 請(qǐng)參考go資產(chǎn)編譯器go-assets-builder
使用方法:
- 1.下載依賴包
- go get github.com/gin-gonic/gin
- go get github.com/jessevdk/go-assets-builder
- 2.將html文件夾(包含html代碼)生成為go資產(chǎn)文件assets.go
- go-assets-builder html -o assets.go
- 3.編譯構(gòu)建,將服務(wù)打包為單二進(jìn)制文件
- go build -o assets-in-binary
- 4.運(yùn)行服務(wù)
- ./assets-in-binary
go資產(chǎn)文件go-assets.go參考內(nèi)容如下:
- package main
- import (
- "time"
- "github.com/jessevdk/go-assets"
- )
- var _Assetsbfa8d115ce0617d89507412d5393a462f8e9b003 = "<!doctype html>\n<body>\n <p>Can you see this? → {{.Bar}}</p>\n</body>\n"
- var _Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2 = "<!doctype html>\n<body>\n <p>Hello, {{.Foo}}</p>\n</body>\n"
- // Assets returns go-assets FileSystem
- var Assets = assets.NewFileSystem(map[string][]string{"/": {"html"}, "/html": {"bar.tmpl", "index.tmpl"}}, map[string]*assets.File{
- "/": {
- Path: "/",
- FileMode: 0x800001ed,
- Mtime: time.Unix(1524365738, 1524365738517125470),
- Data: nil,
- }, "/html": {
- Path: "/html",
- FileMode: 0x800001ed,
- Mtime: time.Unix(1524365491, 1524365491289799093),
- Data: nil,
- }, "/html/bar.tmpl": {
- Path: "/html/bar.tmpl",
- FileMode: 0x1a4,
- Mtime: time.Unix(1524365491, 1524365491289611557),
- Data: []byte(_Assetsbfa8d115ce0617d89507412d5393a462f8e9b003),
- }, "/html/index.tmpl": {
- Path: "/html/index.tmpl",
- FileMode: 0x1a4,
- Mtime: time.Unix(1524365491, 1524365491289995821),
- Data: []byte(_Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2),
- }}, "")
main.go
- package main
- import (
- "github.com/gin-gonic/gin"
- "io/ioutil"
- "net/http"
- "strings"
- "html/template"
- )
- func main() {
- r := gin.New()
- t, err := loadTemplate() //加載go-assets-builder生成的模板
- if err != nil {
- panic(err)
- }
- r.SetHTMLTemplate(t)
- r.GET("/", func(c *gin.Context) {
- c.HTML(http.StatusOK, "/html/index.tmpl",nil)
- })
- r.Run(":8080")
- }
- // loadTemplate loads templates embedded by go-assets-builder
- // 加載go-assets-builder生成的資產(chǎn)文件, 返回模板的地址
- func loadTemplate() (*template.Template, error) {
- t := template.New("")
- for name, file := range Assets.Files {
- defer file.Close()
- if file.IsDir() || !strings.HasSuffix(name, ".tmpl") { //跳過目錄或沒有.tmpl后綴的文件
- continue
- }
- h, err := ioutil.ReadAll(file)
- if err != nil {
- return nil, err
- }
- t, err = t.New(name).Parse(string(h)) //新建一個(gè)模板, 文件名做為模板名, 文件內(nèi)容作為模板內(nèi)容
- if err != nil {
- return nil, err
- }
- }
- return t, nil
- }
完整示例請(qǐng)查看該目錄
使用自定義的結(jié)構(gòu)綁定請(qǐng)求表單
參考實(shí)例代碼:
- type StructA struct {
- FieldA string `form:"field_a"`
- }
- type StructB struct {
- NestedStruct StructA
- FieldB string `form:"field_b"`
- }
- type StructC struct {
- NestedStructPointer *StructA
- FieldC string `form:"field_c"`
- }
- type StructD struct {
- NestedAnonyStruct struct {
- FieldX string `form:"field_x"`
- }
- FieldD string `form:"field_d"`
- }
- func GetDataB(c *gin.Context) {
- var b StructB
- c.Bind(&b)
- c.JSON(200, gin.H{
- "a": b.NestedStruct,
- "b": b.FieldB,
- })
- }
- func GetDataC(c *gin.Context) {
- var b StructC
- c.Bind(&b)
- c.JSON(200, gin.H{
- "a": b.NestedStructPointer,
- "c": b.FieldC,
- })
- }
- func GetDataD(c *gin.Context) {
- var b StructD
- c.Bind(&b)
- c.JSON(200, gin.H{
- "x": b.NestedAnonyStruct,
- "d": b.FieldD,
- })
- }
- func main() {
- r := gin.Default()
- r.GET("/getb", GetDataB)
- r.GET("/getc", GetDataC)
- r.GET("/getd", GetDataD)
- r.Run()
- }
使用命令 curl 模擬請(qǐng)求測(cè)試和結(jié)果如下:
- $ curl "http://localhost:8080/getb?field_a=hello&field_b=world"
- {"a":{"FieldA":"hello"},"b":"world"}
- $ curl "http://localhost:8080/getc?field_a=hello&field_c=world"
- {"a":{"FieldA":"hello"},"c":"world"}
- $ curl "http://localhost:8080/getd?field_x=hello&field_d=world"
- {"d":"world","x":{"FieldX":"hello"}}
嘗試將請(qǐng)求體綁定到不同的結(jié)構(gòu)
常規(guī)的方法綁定請(qǐng)求體是調(diào)用c.Request.Body, 但是它不能多次被調(diào)用
- type formA struct {
- Foo string `json:"foo" xml:"foo" binding:"required"`
- }
- type formB struct {
- Bar string `json:"bar" xml:"bar" binding:"required"`
- }
- func SomeHandler(c *gin.Context) {
- objA := formA{}
- objB := formB{}
- // This c.ShouldBind consumes c.Request.Body and it cannot be reused.
- // 使用c.ShoudBind消費(fèi)c.Request.Body, 但是它只能調(diào)用一次
- if errA := c.ShouldBind(&objA); errA == nil {
- c.String(http.StatusOK, `the body should be formA`)
- // Always an error is occurred by this because c.Request.Body is EOF now.
- //這里會(huì)報(bào)錯(cuò),因?yàn)閏.Request.Body已經(jīng)被消費(fèi), 會(huì)返回文件結(jié)束符EOF
- } else if errB := c.ShouldBind(&objB); errB == nil {
- c.String(http.StatusOK, `the body should be formB`)
- } else {
- ...
- }
- }
為了解決這個(gè)問題, 可以使用c.ShouldBindBodyWith方法.
- func SomeHandler(c *gin.Context) {
- objA := formA{}
- objB := formB{}
- // This reads c.Request.Body and stores the result into the context.
- // c.ShouldBindBodyWith方法讀取c.Request.Body,并且將結(jié)果存儲(chǔ)到上下文
- if errA := c.ShouldBindBodyWith(&objA, binding.JSON); errA == nil {
- c.String(http.StatusOK, `the body should be formA`)
- // At this time, it reuses body stored in the context.
- //再次調(diào)用c.ShouldBindBodyWith時(shí), 可以從上下文中復(fù)用請(qǐng)求體內(nèi)容
- } else if errB := c.ShouldBindBodyWith(&objB, binding.JSON); errB == nil {
- c.String(http.StatusOK, `the body should be formB JSON`)
- // And it can accepts other formats 也可以接受其他類型的綁定,比如XML
- } else if errB2 := c.ShouldBindBodyWith(&objB, binding.XML); errB2 == nil {
- c.String(http.StatusOK, `the body should be formB XML`)
- } else {
- ...
- }
- }
- c.ShouldBindBodyWith 該方法在綁定前, 將請(qǐng)求體存儲(chǔ)到gin上下文中, 所以這會(huì)對(duì)性能有輕微的影響, 所以如果你只打算綁定一次的時(shí)候, 不應(yīng)該使用該方法.
- 這種方式僅僅支持以下格式: JSON, XML, MsgPack,ProtoBuf. 對(duì)于其他格式, Query, Form, FormPost, FormMultipart, 可以重復(fù)使用c.ShouldBind()方法, 而不會(huì)帶來類似的性能影響, 詳見(#1341)
http2服務(wù)推送
為了解決HTTP/1.X的網(wǎng)絡(luò)資源利用率不夠高, 延遲問題等, HTTP/2 引入了服務(wù)器推送機(jī)制來解決這些問題.
http.Pusher需要go1.8+版本支持. 詳見golang博客.
- package main
- import (
- "html/template"
- "log"
- "github.com/gin-gonic/gin"
- )
- //定義html模板
- var html = template.Must(template.New("https").Parse(`
- <html>
- <head>
- <title>Https Test</title>
- <script src="/assets/app.js"></script>
- </head>
- <body>
- <h1 style="color:red;">Welcome, Ginner!</h1>
- </body>
- </html>
- `))
- func main() {
- r := gin.Default()
- r.Static("/assets", "./assets")
- r.SetHTMLTemplate(html)
- r.GET("/", func(c *gin.Context) {
- if pusher := c.Writer.Pusher(); pusher != nil { //獲取推送器
- // use pusher.Push() to do server push
- // 使用pusher.Push()方法執(zhí)行服務(wù)端推送動(dòng)作, 嘗試推送app.js文件
- if err := pusher.Push("/assets/app.js", nil); err != nil {
- log.Printf("Failed to push: %v", err)
- }
- }
- c.HTML(200, "https", gin.H{
- "status": "success",
- })
- })
- // Listen and Server in https://127.0.0.1:8080
- r.RunTLS(":8080", "./testdata/server.pem", "./testdata/server.key")
- }
定義路由日志格式
默認(rèn)路由日志如下:
- [GIN-debug] POST /foo --> main.main.func1 (3 handlers)
- [GIN-debug] GET /bar --> main.main.func2 (3 handlers)
- [GIN-debug] GET /status --> main.main.func3 (3 handlers)
如果你想用給定的格式(如:JSON,鍵值對(duì)等)記錄路由日志, 你可以使用gin.DebugPrintRouteFunc方法自定義日志格式, 下面的示例, 我們用日志log標(biāo)準(zhǔn)庫記錄路由器日志, 當(dāng)然你也可以使用其他適合業(yè)務(wù)的日志工具.
- package main
- import (
- "log"
- "net/http"
- "github.com/gin-gonic/gin"
- )
- func main() {
- r := gin.Default()
- //使用DebugPrintRouteFunc設(shè)置路由日志記錄格式, 這里使用標(biāo)準(zhǔn)庫log包記錄請(qǐng)求方法/請(qǐng)求路徑/控制器名/控制器鏈個(gè)數(shù),
- gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
- log.Printf("endpoint %v %v %v %v\n", httpMethod, absolutePath, handlerName, nuHandlers)
- }
- r.POST("/foo", func(c *gin.Context) {
- c.JSON(http.StatusOK, "foo")
- })
- r.GET("/bar", func(c *gin.Context) {
- c.JSON(http.StatusOK, "bar")
- })
- r.GET("/status", func(c *gin.Context) {
- c.JSON(http.StatusOK, "ok")
- })
- // Listen and Server in http://0.0.0.0:8080
- r.Run()
- }
設(shè)置和讀取Cookie
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- )
- func main() {
- router := gin.Default()
- router.GET("/cookie", func(c *gin.Context) {
- //讀取Cookie
- cookie, err := c.Cookie("gin_cookie")
- if err != nil {
- cookie = "NotSet"
- //設(shè)置Cookie
- c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)
- }
- fmt.Printf("Cookie value: %s \n", cookie)
- })
- router.Run()
- }
測(cè)試
推薦使用net/http/httptest 包做HTTP測(cè)試.
- package main
- func setupRouter() *gin.Engine {
- r := gin.Default()
- r.GET("/ping", func(c *gin.Context) {
- c.String(200, "pong")
- })
- return r
- }
- func main() {
- r := setupRouter()
- r.Run(":8080")
- }
測(cè)試代碼示例:
- package main
- import (
- "net/http"
- "net/http/httptest"
- "testing"
- "github.com/stretchr/testify/assert"
- )
- func TestPingRoute(t *testing.T) {
- router := setupRouter()
- w := httptest.NewRecorder()
- req, _ := http.NewRequest("GET", "/ping", nil)
- router.ServeHTTP(w, req)
- //斷言
- assert.Equal(t, 200, w.Code)
- assert.Equal(t, "pong", w.Body.String())
- }
Gin框架用戶
其他優(yōu)質(zhì)的項(xiàng)目也使用GinWeb框架.
- gorush: 一個(gè)用GO實(shí)現(xiàn)的推送通知系統(tǒng)
- fnproject: 原生容器化, 無服務(wù)的跨云平臺(tái)
- photoprism: 使用Go和Google的TensorFlow框架支持的個(gè)人照片管理
- krakend: 帶有中間件的極致高性能API網(wǎng)關(guān)
- picfit: 使用Go實(shí)現(xiàn)的一款圖片編輯服務(wù)器
- brigade: 為Kubernetes服務(wù)的基于事件驅(qū)動(dòng)的腳本
- dkron: 分布式, 可靠的任務(wù)調(diào)度系統(tǒng)
參考文檔
Gin官方倉庫:https://github.com/gin-gonic/gin