diff --git a/examples/go.mod b/examples/go.mod index d3a851a84..0e76d0ae8 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -13,6 +13,7 @@ require ( github.com/go-errors/errors v1.4.0 // indirect github.com/go-kratos/consul v0.1.2 github.com/go-kratos/etcd v0.1.0 + github.com/go-kratos/gin v0.1.0 github.com/go-kratos/kratos/v2 v2.0.0 github.com/go-kratos/nacos v0.1.0 github.com/go-kratos/prometheus v0.0.0-20210522055322-137e29e7cf47 diff --git a/examples/go.sum b/examples/go.sum index be93e19a5..4c786ec14 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -172,6 +172,10 @@ github.com/go-kratos/consul v0.1.2 h1:vryYF2o3wWQzFyzqKKKc1+GX/FY9T7DSkM3nc/Hy6/ github.com/go-kratos/consul v0.1.2/go.mod h1:FhxdCfkqaqIYfgpGbzGb8ywM/zV3jk+EBS+WSy1FzQQ= github.com/go-kratos/etcd v0.1.0 h1:ewAfloZcpcg2L1QB1VLXYUMdvfUU0i3kAlToHrxTv2g= github.com/go-kratos/etcd v0.1.0/go.mod h1:RzWYbka3LjS0v2RbCtADN7d2Ng8eH774ehHlTGgbma4= +github.com/go-kratos/gin v0.0.0-20210718022314-0365ef99696f h1:BrWyGMOTPEgu1lpecOCOiNClggXehjK6ox00Yb/GhyY= +github.com/go-kratos/gin v0.0.0-20210718022314-0365ef99696f/go.mod h1:KzE88+mV1QJdmiT+sU2D560ucOXnWZx+4m0OnVcSyzQ= +github.com/go-kratos/gin v0.1.0 h1:yq5GfZnSNo8cOIqxqPE0FVNQ8fm++oKQBd3/rTTp4oI= +github.com/go-kratos/gin v0.1.0/go.mod h1:KzE88+mV1QJdmiT+sU2D560ucOXnWZx+4m0OnVcSyzQ= github.com/go-kratos/nacos v0.1.0 h1:sUvTsgZ4u88YaKF/jAi2Rdr+hzPCfQtmNjBpiyEbbJ0= github.com/go-kratos/nacos v0.1.0/go.mod h1:qhVsyp6RyDDEKLtT0/iTMaN3D7SheRMrYb2bV0jYqjw= github.com/go-kratos/prometheus v0.0.0-20210522055322-137e29e7cf47 h1:uJennLdg0rwhrIl0bjsObwub+I3U0vJGAhpK6CYASrM= diff --git a/examples/http/gin/main.go b/examples/http/gin/main.go index 5bd92f1fe..82e9fec33 100644 --- a/examples/http/gin/main.go +++ b/examples/http/gin/main.go @@ -1,17 +1,43 @@ package main import ( + "context" + "fmt" "log" "github.com/gin-gonic/gin" + kgin "github.com/go-kratos/gin" "github.com/go-kratos/kratos/v2" + "github.com/go-kratos/kratos/v2/errors" + "github.com/go-kratos/kratos/v2/middleware" + "github.com/go-kratos/kratos/v2/middleware/recovery" + "github.com/go-kratos/kratos/v2/transport" "github.com/go-kratos/kratos/v2/transport/http" ) +func customMiddleware(handler middleware.Handler) middleware.Handler { + return func(ctx context.Context, req interface{}) (reply interface{}, err error) { + if tr, ok := transport.FromServerContext(ctx); ok { + fmt.Println("operation:", tr.Operation()) + } + reply, err = handler(ctx, req) + return + } +} + func main() { router := gin.Default() - router.GET("/home", func(ctx *gin.Context) { - ctx.String(200, "Hello Gin!") + // 使用kratos中间件 + router.Use(kgin.Middlewares(recovery.Recovery(), customMiddleware)) + + router.GET("/helloworld/:name", func(ctx *gin.Context) { + name := ctx.Param("name") + if name == "error" { + // 返回kratos error + kgin.Error(ctx, errors.Unauthorized("auth_error", "no authentication")) + } else { + ctx.JSON(200, map[string]string{"welcome": name}) + } }) httpSrv := http.NewServer(http.Address(":8000"))