Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContentType ¶
ContentType returns the content-type with base prefix.
func Error ¶
Error encodes the object to the HTTP response. It is recommended that you return error directly.
func FromGinContext ¶
FromGinContext returns the gin.Context value stored in ctx, if any.
func Middlewares ¶
func Middlewares(m ...middleware.Middleware) gin.HandlerFunc
Middlewares return middlewares wrapper
Example ¶
gin.SetMode(gin.ReleaseMode) r := gin.New() r.Use(kgin.Middlewares( recovery.Recovery(), tracing.Server(), metadata.Server(), )) generator := func(value string) string { return fmt.Sprintf("hello %s", value) } r.GET("/hellworld", func(ctx *gin.Context) { v := ctx.Query("name") ctx.JSON(200, gin.H{ "message": generator(v), }) }) r.GET("/panic", func(ctx *gin.Context) { kgin.Error(ctx, errors.New(500, "want panic", "panic")) }) r.GET("/nilerr", func(ctx *gin.Context) { kgin.Error(ctx, nil) }) r.GET("/err", func(ctx *gin.Context) { ctx.Request.Header.Set("Accept", "application/xml") kgin.Error(ctx, errors.New(500, "want err xml", "json xml")) }) listener, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { panic(err) } addr := listener.Addr().String() httpSrv := http.NewServer(http.Listener(listener)) httpSrv.HandlePrefix("/", r) app := kratos.New( kratos.Name("gin-test"), kratos.Server( httpSrv, ), ) go func() { ticker := time.NewTicker(2 * time.Second) <-ticker.C _ = app.Stop() }() go func() { ticker := time.NewTicker(200 * time.Millisecond) <-ticker.C client := resty.New() resp, err := client.NewRequest().Get(fmt.Sprintf("http://%s/hellworld?name=gin", addr)) if err != nil { panic(err) } fmt.Println(resp.String()) resp, err = client.NewRequest().Get(fmt.Sprintf("http://%s/panic", addr)) if err != nil { panic(err) } fmt.Println(resp.String()) resp, err = client.NewRequest().Get(fmt.Sprintf("http://%s/nilerr", addr)) if err != nil { panic(err) } fmt.Println(resp.String()) resp, err = client.NewRequest().Get(fmt.Sprintf("http://%s/err", addr)) if err != nil { panic(err) } fmt.Println(resp.String()) }() if err := app.Run(); err != nil { log.Fatal(err) }
Output: {"message":"hello gin"} {"code":500, "reason":"want panic", "message":"panic", "metadata":{}}
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.