自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Golang GinWeb框架9-編譯模板/自定義結(jié)構(gòu)體綁定/http2/操作Cookie

開發(fā) 前端
本文接著上文(Golang GinWeb框架8-重定向/自定義中間件/認(rèn)證/HTTPS支持/優(yōu)雅重啟等)繼續(xù)探索GinWeb框架.

 [[356839]]

簡(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. 1.下載依賴包 
  2. go get github.com/gin-gonic/gin 
  3. go get github.com/jessevdk/go-assets-builder 
  4.  
  5. 2.將html文件夾(包含html代碼)生成為go資產(chǎn)文件assets.go 
  6. go-assets-builder html -o assets.go 
  7.  
  8. 3.編譯構(gòu)建,將服務(wù)打包為單二進(jìn)制文件 
  9. go build -o assets-in-binary 
  10.  
  11. 4.運(yùn)行服務(wù) 
  12. ./assets-in-binary 

go資產(chǎn)文件go-assets.go參考內(nèi)容如下:

  1. package main 
  2.  
  3. import ( 
  4.   "time" 
  5.  
  6.   "github.com/jessevdk/go-assets" 
  7.  
  8. var _Assetsbfa8d115ce0617d89507412d5393a462f8e9b003 = "<!doctype html>\n<body>\n  <p>Can you see this? → {{.Bar}}</p>\n</body>\n" 
  9. var _Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2 = "<!doctype html>\n<body>\n  <p>Hello, {{.Foo}}</p>\n</body>\n" 
  10.  
  11. // Assets returns go-assets FileSystem 
  12. var Assets = assets.NewFileSystem(map[string][]string{"/": {"html"}, "/html": {"bar.tmpl""index.tmpl"}}, map[string]*assets.File{ 
  13.   "/": { 
  14.     Path:     "/"
  15.     FileMode: 0x800001ed, 
  16.     Mtime:    time.Unix(1524365738, 1524365738517125470), 
  17.     Data:     nil, 
  18.   }, "/html": { 
  19.     Path:     "/html"
  20.     FileMode: 0x800001ed, 
  21.     Mtime:    time.Unix(1524365491, 1524365491289799093), 
  22.     Data:     nil, 
  23.   }, "/html/bar.tmpl": { 
  24.     Path:     "/html/bar.tmpl"
  25.     FileMode: 0x1a4, 
  26.     Mtime:    time.Unix(1524365491, 1524365491289611557), 
  27.     Data:     []byte(_Assetsbfa8d115ce0617d89507412d5393a462f8e9b003), 
  28.   }, "/html/index.tmpl": { 
  29.     Path:     "/html/index.tmpl"
  30.     FileMode: 0x1a4, 
  31.     Mtime:    time.Unix(1524365491, 1524365491289995821), 
  32.     Data:     []byte(_Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2), 
  33.   }}, ""

 main.go

  1. package main 
  2.  
  3. import ( 
  4.   "github.com/gin-gonic/gin" 
  5.   "io/ioutil" 
  6.   "net/http" 
  7.   "strings" 
  8.   "html/template" 
  9.  
  10.  
  11. func main() { 
  12.   r := gin.New() 
  13.  
  14.   t, err := loadTemplate() //加載go-assets-builder生成的模板 
  15.   if err != nil { 
  16.     panic(err) 
  17.   } 
  18.   r.SetHTMLTemplate(t) 
  19.  
  20.   r.GET("/", func(c *gin.Context) { 
  21.     c.HTML(http.StatusOK, "/html/index.tmpl",nil) 
  22.   }) 
  23.   r.Run(":8080"
  24.  
  25. // loadTemplate loads templates embedded by go-assets-builder 
  26. // 加載go-assets-builder生成的資產(chǎn)文件, 返回模板的地址 
  27. func loadTemplate() (*template.Template, error) { 
  28.   t := template.New(""
  29.   for name, file := range Assets.Files { 
  30.     defer file.Close() 
  31.     if file.IsDir() || !strings.HasSuffix(name".tmpl") {  //跳過目錄或沒有.tmpl后綴的文件 
  32.       continue 
  33.     } 
  34.     h, err := ioutil.ReadAll(file) 
  35.     if err != nil { 
  36.       return nil, err 
  37.     } 
  38.     t, err = t.New(name).Parse(string(h))  //新建一個(gè)模板, 文件名做為模板名, 文件內(nèi)容作為模板內(nèi)容 
  39.     if err != nil { 
  40.       return nil, err 
  41.     } 
  42.   } 
  43.   return t, nil 

完整示例請(qǐng)查看該目錄

使用自定義的結(jié)構(gòu)綁定請(qǐng)求表單

參考實(shí)例代碼:

  1. type StructA struct { 
  2.     FieldA string `form:"field_a"
  3.  
  4. type StructB struct { 
  5.     NestedStruct StructA 
  6.     FieldB string `form:"field_b"
  7.  
  8. type StructC struct { 
  9.     NestedStructPointer *StructA 
  10.     FieldC string `form:"field_c"
  11.  
  12. type StructD struct { 
  13.     NestedAnonyStruct struct { 
  14.         FieldX string `form:"field_x"
  15.     } 
  16.     FieldD string `form:"field_d"
  17.  
  18. func GetDataB(c *gin.Context) { 
  19.     var b StructB 
  20.     c.Bind(&b) 
  21.     c.JSON(200, gin.H{ 
  22.         "a": b.NestedStruct, 
  23.         "b": b.FieldB, 
  24.     }) 
  25.  
  26. func GetDataC(c *gin.Context) { 
  27.     var b StructC 
  28.     c.Bind(&b) 
  29.     c.JSON(200, gin.H{ 
  30.         "a": b.NestedStructPointer, 
  31.         "c": b.FieldC, 
  32.     }) 
  33.  
  34. func GetDataD(c *gin.Context) { 
  35.     var b StructD 
  36.     c.Bind(&b) 
  37.     c.JSON(200, gin.H{ 
  38.         "x": b.NestedAnonyStruct, 
  39.         "d": b.FieldD, 
  40.     }) 
  41.  
  42. func main() { 
  43.     r := gin.Default() 
  44.     r.GET("/getb", GetDataB) 
  45.     r.GET("/getc", GetDataC) 
  46.     r.GET("/getd", GetDataD) 
  47.  
  48.     r.Run() 

使用命令 curl 模擬請(qǐng)求測(cè)試和結(jié)果如下:

  1. $ curl "http://localhost:8080/getb?field_a=hello&field_b=world" 
  2. {"a":{"FieldA":"hello"},"b":"world"
  3. $ curl "http://localhost:8080/getc?field_a=hello&field_c=world" 
  4. {"a":{"FieldA":"hello"},"c":"world"
  5. $ curl "http://localhost:8080/getd?field_x=hello&field_d=world" 
  6. {"d":"world","x":{"FieldX":"hello"}} 

嘗試將請(qǐng)求體綁定到不同的結(jié)構(gòu)

常規(guī)的方法綁定請(qǐng)求體是調(diào)用c.Request.Body, 但是它不能多次被調(diào)用

  1. type formA struct { 
  2.   Foo string `json:"foo" xml:"foo" binding:"required"
  3.  
  4. type formB struct { 
  5.   Bar string `json:"bar" xml:"bar" binding:"required"
  6.  
  7. func SomeHandler(c *gin.Context) { 
  8.   objA := formA{} 
  9.   objB := formB{} 
  10.   // This c.ShouldBind consumes c.Request.Body and it cannot be reused. 
  11.   // 使用c.ShoudBind消費(fèi)c.Request.Body, 但是它只能調(diào)用一次 
  12.   if errA := c.ShouldBind(&objA); errA == nil { 
  13.     c.String(http.StatusOK, `the body should be formA`) 
  14.   // Always an error is occurred by this because c.Request.Body is EOF now. 
  15.   //這里會(huì)報(bào)錯(cuò),因?yàn)閏.Request.Body已經(jīng)被消費(fèi), 會(huì)返回文件結(jié)束符EOF 
  16.   } else if errB := c.ShouldBind(&objB); errB == nil { 
  17.     c.String(http.StatusOK, `the body should be formB`) 
  18.   } else { 
  19.     ... 
  20.   } 

為了解決這個(gè)問題, 可以使用c.ShouldBindBodyWith方法.

  1. func SomeHandler(c *gin.Context) { 
  2.   objA := formA{} 
  3.   objB := formB{} 
  4.   // This reads c.Request.Body and stores the result into the context. 
  5.   // c.ShouldBindBodyWith方法讀取c.Request.Body,并且將結(jié)果存儲(chǔ)到上下文 
  6.   if errA := c.ShouldBindBodyWith(&objA, binding.JSON); errA == nil { 
  7.     c.String(http.StatusOK, `the body should be formA`) 
  8.   // At this time, it reuses body stored in the context. 
  9.   //再次調(diào)用c.ShouldBindBodyWith時(shí), 可以從上下文中復(fù)用請(qǐng)求體內(nèi)容 
  10.   } else if errB := c.ShouldBindBodyWith(&objB, binding.JSON); errB == nil { 
  11.     c.String(http.StatusOK, `the body should be formB JSON`) 
  12.   // And it can accepts other formats 也可以接受其他類型的綁定,比如XML 
  13.   } else if errB2 := c.ShouldBindBodyWith(&objB, binding.XML); errB2 == nil { 
  14.     c.String(http.StatusOK, `the body should be formB XML`) 
  15.   } else { 
  16.     ... 
  17.   } 
  • 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博客.

  1. package main 
  2.  
  3. import ( 
  4.   "html/template" 
  5.   "log" 
  6.  
  7.   "github.com/gin-gonic/gin" 
  8.  
  9. //定義html模板 
  10. var html = template.Must(template.New("https").Parse(` 
  11. <html> 
  12. <head> 
  13.   <title>Https Test</title> 
  14.   <script src="/assets/app.js"></script> 
  15. </head> 
  16. <body> 
  17.   <h1 style="color:red;">Welcome, Ginner!</h1> 
  18. </body> 
  19. </html> 
  20. `)) 
  21.  
  22. func main() { 
  23.   r := gin.Default() 
  24.   r.Static("/assets""./assets"
  25.   r.SetHTMLTemplate(html) 
  26.  
  27.   r.GET("/", func(c *gin.Context) { 
  28.     if pusher := c.Writer.Pusher(); pusher != nil { //獲取推送器 
  29.       // use pusher.Push() to do server push 
  30.       // 使用pusher.Push()方法執(zhí)行服務(wù)端推送動(dòng)作, 嘗試推送app.js文件 
  31.       if err := pusher.Push("/assets/app.js", nil); err != nil { 
  32.         log.Printf("Failed to push: %v", err) 
  33.       } 
  34.     } 
  35.     c.HTML(200, "https", gin.H{ 
  36.       "status""success"
  37.     }) 
  38.   }) 
  39.  
  40.   // Listen and Server in https://127.0.0.1:8080 
  41.   r.RunTLS(":8080""./testdata/server.pem""./testdata/server.key"

 定義路由日志格式

默認(rèn)路由日志如下:

  1. [GIN-debug] POST   /foo                      --> main.main.func1 (3 handlers) 
  2. [GIN-debug] GET    /bar                      --> main.main.func2 (3 handlers) 
  3. [GIN-debug] GET    /status                   --> main.main.func3 (3 handlers) 

如果你想用給定的格式(如:JSON,鍵值對(duì)等)記錄路由日志, 你可以使用gin.DebugPrintRouteFunc方法自定義日志格式, 下面的示例, 我們用日志log標(biāo)準(zhǔn)庫記錄路由器日志, 當(dāng)然你也可以使用其他適合業(yè)務(wù)的日志工具.

  1. package main 
  2.  
  3. import ( 
  4.   "log" 
  5.   "net/http" 
  6.  
  7.   "github.com/gin-gonic/gin" 
  8.  
  9. func main() { 
  10.   r := gin.Default() 
  11.   //使用DebugPrintRouteFunc設(shè)置路由日志記錄格式, 這里使用標(biāo)準(zhǔn)庫log包記錄請(qǐng)求方法/請(qǐng)求路徑/控制器名/控制器鏈個(gè)數(shù), 
  12.   gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) { 
  13.     log.Printf("endpoint %v %v %v %v\n", httpMethod, absolutePath, handlerName, nuHandlers) 
  14.   } 
  15.  
  16.   r.POST("/foo", func(c *gin.Context) { 
  17.     c.JSON(http.StatusOK, "foo"
  18.   }) 
  19.  
  20.   r.GET("/bar", func(c *gin.Context) { 
  21.     c.JSON(http.StatusOK, "bar"
  22.   }) 
  23.  
  24.   r.GET("/status", func(c *gin.Context) { 
  25.     c.JSON(http.StatusOK, "ok"
  26.   }) 
  27.  
  28.   // Listen and Server in http://0.0.0.0:8080 
  29.   r.Run() 

設(shè)置和讀取Cookie

  1. import ( 
  2.     "fmt" 
  3.  
  4.     "github.com/gin-gonic/gin" 
  5.  
  6. func main() { 
  7.  
  8.     router := gin.Default() 
  9.  
  10.     router.GET("/cookie", func(c *gin.Context) { 
  11.         //讀取Cookie 
  12.         cookie, err := c.Cookie("gin_cookie"
  13.  
  14.         if err != nil { 
  15.             cookie = "NotSet" 
  16.             //設(shè)置Cookie 
  17.             c.SetCookie("gin_cookie""test", 3600, "/""localhost"falsetrue
  18.         } 
  19.  
  20.         fmt.Printf("Cookie value: %s \n", cookie) 
  21.     }) 
  22.  
  23.     router.Run() 

測(cè)試

推薦使用net/http/httptest 包做HTTP測(cè)試.

  1. package main 
  2.  
  3. func setupRouter() *gin.Engine { 
  4.   r := gin.Default() 
  5.   r.GET("/ping", func(c *gin.Context) { 
  6.     c.String(200, "pong"
  7.   }) 
  8.   return r 
  9.  
  10. func main() { 
  11.   r := setupRouter() 
  12.   r.Run(":8080"

測(cè)試代碼示例:

  1. package main 
  2.  
  3. import ( 
  4.   "net/http" 
  5.   "net/http/httptest" 
  6.   "testing" 
  7.  
  8.   "github.com/stretchr/testify/assert" 
  9.  
  10. func TestPingRoute(t *testing.T) { 
  11.   router := setupRouter() 
  12.  
  13.   w := httptest.NewRecorder() 
  14.   req, _ := http.NewRequest("GET""/ping", nil) 
  15.   router.ServeHTTP(w, req) 
  16.   //斷言 
  17.   assert.Equal(t, 200, w.Code) 
  18.   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

 

責(zé)任編輯:姜華 來源: 云原生云
相關(guān)推薦

2020-11-25 09:18:15

Golang GinW

2020-12-03 09:28:05

Golang GinW

2020-11-26 10:08:17

Golang GinW

2020-11-25 09:10:39

Golang GinW

2020-12-08 12:05:48

Golang GinW框架HTTPS

2020-11-23 10:48:39

Golang GinW

2010-03-01 11:10:41

WCF綁定元素

2015-08-13 10:31:18

Java 9新功能

2023-09-06 10:33:40

夜鶯監(jiān)控數(shù)據(jù)庫

2021-05-28 08:58:41

Golang網(wǎng)卡metrics

2020-12-02 11:18:28

Golang GinW

2020-11-27 07:54:53

Golang GinW

2017-09-22 10:53:52

HTTPHTTP2TCP協(xié)議

2024-11-11 00:45:54

Gin框架字段

2023-10-31 09:10:39

2011-03-02 10:24:23

DashboardAndroid用戶界面設(shè)計(jì)模板

2009-06-25 14:53:35

自定義UI組件JSF框架

2023-07-28 09:26:43

GolangZap

2019-10-15 08:00:00

HTTP2HTTP前端

2021-01-14 19:04:36

框架數(shù)據(jù)庫mybatis
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)