add gin examples

pull/1207/head
longXboy 3 years ago
parent 5f678de2cc
commit 2920c12ead
  1. 1
      examples/go.mod
  2. 4
      examples/go.sum
  3. 30
      examples/http/gin/main.go

@ -13,6 +13,7 @@ require (
github.com/go-errors/errors v1.4.0 // indirect github.com/go-errors/errors v1.4.0 // indirect
github.com/go-kratos/consul v0.1.2 github.com/go-kratos/consul v0.1.2
github.com/go-kratos/etcd v0.1.0 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/kratos/v2 v2.0.0
github.com/go-kratos/nacos v0.1.0 github.com/go-kratos/nacos v0.1.0
github.com/go-kratos/prometheus v0.0.0-20210522055322-137e29e7cf47 github.com/go-kratos/prometheus v0.0.0-20210522055322-137e29e7cf47

@ -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/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 h1:ewAfloZcpcg2L1QB1VLXYUMdvfUU0i3kAlToHrxTv2g=
github.com/go-kratos/etcd v0.1.0/go.mod h1:RzWYbka3LjS0v2RbCtADN7d2Ng8eH774ehHlTGgbma4= 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 h1:sUvTsgZ4u88YaKF/jAi2Rdr+hzPCfQtmNjBpiyEbbJ0=
github.com/go-kratos/nacos v0.1.0/go.mod h1:qhVsyp6RyDDEKLtT0/iTMaN3D7SheRMrYb2bV0jYqjw= 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= github.com/go-kratos/prometheus v0.0.0-20210522055322-137e29e7cf47 h1:uJennLdg0rwhrIl0bjsObwub+I3U0vJGAhpK6CYASrM=

@ -1,17 +1,43 @@
package main package main
import ( import (
"context"
"fmt"
"log" "log"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
kgin "github.com/go-kratos/gin"
"github.com/go-kratos/kratos/v2" "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" "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() { func main() {
router := gin.Default() router := gin.Default()
router.GET("/home", func(ctx *gin.Context) { // 使用kratos中间件
ctx.String(200, "Hello Gin!") 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")) httpSrv := http.NewServer(http.Address(":8000"))

Loading…
Cancel
Save