Hero Image
Gin中文文档

自定义路由日志的格式 Gin 运行多个服务 XML、JSON、YAML 和 ProtoBuf 渲染(输出格式) 自定义路由日志的格式 default [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) import ( "log" "net/http" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() 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() } Gin 运行多个服务 package main import ( "log" "net/http" "time" "github.com/gin-gonic/gin" "golang.org/x/sync/errgroup" ) var ( g errgroup.Group ) func router01() http.Handler { e := gin.New() e.Use(gin.Recovery()) e.GET("/", func(c *gin.Context) { c.JSON( http.StatusOK, gin.H{ "code": http.StatusOK, "error": "Welcome server 01", }, ) }) return e } func router02() http.Handler { e := gin.New() e.Use(gin.Recovery()) e.GET("/", func(c *gin.Context) { c.JSON( http.StatusOK, gin.H{ "code": http.StatusOK, "error": "Welcome server 02", }, ) }) return e } func main() { server01 := &http.Server{ Addr: ":8080", Handler: router01(), ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, } server02 := &http.Server{ Addr: ":8081", Handler: router02(), ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, } g.Go(func() error { return server01.ListenAndServe() }) g.Go(func() error { return server02.ListenAndServe() }) if err := g.Wait(); err != nil { log.Fatal(err) } } XML、JSON、YAML 和 ProtoBuf 渲染(输出格式) SecureJSON 使用 SecureJSON 可以防止 json 劫持,如果返回的数据是数组,则会默认在返回值前加上"while(1)"

Hero Image
多數程式設計師不知道的 6 個 YAML 特性

多數程式設計師不知道的 6 個 YAML 特性 還有更多類似的危險例子,正如 Tom Ritchford 所指出 013 會被對應為 11,因為前導 0 會觸發八進位表示法 4:30 會被對應為 270。Max Werner Kaul-Gothe 與 Niklas Baumstark 告訴我,這會被自動轉換為分鐘(或秒)並被視為一段持續時間:4*60 + 30 = 270。有趣的是,這個模式在 1:1:1:1:1:1:1:1:4:30 仍然「可運作」。 多行字串 mail_signature: | Martin Thoma Tel. +49 123 4567 { "mail_signature": "Martin Thoma\nTel. +49 123 4567" } 錨點 & 會定義一個名為 emailAddress 的變數,值為 "info@example.de"。* 則表示接著的是變數名稱。 email: &emailAddress "info@example.de" id: *emailAddress { "email": "info@example.de", "id": "info@example.de" } 也可以對映射這樣做 foo: &default_settings db: host: localhost name: main_db port: 1337 email: admin: admin@example.com prod: <<: *default_settings app: port: 80 dev: *default_settings { "foo": { "db": { "host": "localhost", "name": "main_db", "port": 1337 }, "email": { "admin": "admin@example.com" } }, "prod": { "app": { "port": 80 }, "db": { "host": "localhost", "name": "main_db", "port": 1337 }, "email": { "admin": "admin@example.com" } }, "dev": { "db": { "host": "localhost", "name": "main_db", "port": 1337 }, "email": { "admin": "admin@example.com" } } } 型別轉換 雙驚嘆號 !! 在 YAML 中有特殊意義。它稱為「secondary tag handle」,是 !tag:yaml.org,2002 的簡寫。