|
|
@ -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")) |
|
|
|