diff --git a/bizdemo/tiktok_demo/main.go b/bizdemo/tiktok_demo/main.go index 54e50c55..29f98edc 100644 --- a/bizdemo/tiktok_demo/main.go +++ b/bizdemo/tiktok_demo/main.go @@ -32,11 +32,11 @@ import ( ) // Set up /src/*name route forwarding to access minio from external network -func minioReverseProxy(c context.Context, ctx *app.RequestContext) { +func minioReverseProxy(ctx context.Context, c *app.RequestContext) { proxy, _ := reverseproxy.NewSingleHostReverseProxy("http://localhost:18001") - ctx.URI().SetPath(ctx.Param("name")) - hlog.CtxInfof(c, string(ctx.Request.URI().Path())) - proxy.ServeHTTP(c, ctx) + c.URI().SetPath(c.Param("name")) + hlog.CtxInfof(ctx, string(c.Request.URI().Path())) + proxy.ServeHTTP(ctx, c) } func Init() { diff --git a/file/html-fs/main.go b/file/html-fs/main.go index 042c61d7..cda40d8b 100644 --- a/file/html-fs/main.go +++ b/file/html-fs/main.go @@ -33,8 +33,8 @@ func main() { fs := &app.FS{Root: root, PathRewrite: getPathRewriter(prefix)} h.StaticFS(prefix, fs) - h.GET("/", func(c context.Context, ctx *app.RequestContext) { - ctx.HTML(200, "index.html", nil) + h.GET("/", func(ctx context.Context, c *app.RequestContext) { + c.HTML(200, "index.html", nil) }) h.Spin() } diff --git a/file/html/main.go b/file/html/main.go index bfde00e6..9157d166 100644 --- a/file/html/main.go +++ b/file/html/main.go @@ -29,8 +29,8 @@ func main() { h.Static("/", "./assets") - h.GET("/", func(c context.Context, ctx *app.RequestContext) { - ctx.HTML(200, "index.html", nil) + h.GET("/", func(ctx context.Context, c *app.RequestContext) { + c.HTML(200, "index.html", nil) }) h.Spin() } diff --git a/file/staticFile/main.go b/file/staticFile/main.go index eebf9ed0..52abb088 100644 --- a/file/staticFile/main.go +++ b/file/staticFile/main.go @@ -36,8 +36,8 @@ func main() { h.StaticFS("/static1", &app.FS{ Root: "./", PathRewrite: app.NewPathSlashesStripper(1), - PathNotFound: func(_ context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusNotFound, "The requested resource does not exist") + PathNotFound: func(_ context.Context, c *app.RequestContext) { + c.JSON(consts.StatusNotFound, "The requested resource does not exist") }, CacheDuration: time.Second * 5, IndexNames: indexNames, diff --git a/hex/hex_trans_handler.go b/hex/hex_trans_handler.go index 43ba0604..2b66e99d 100644 --- a/hex/hex_trans_handler.go +++ b/hex/hex_trans_handler.go @@ -90,8 +90,8 @@ func initHertz() *route.Engine { h := hertzServer.New() // add a ping route to test - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) router.GeneratedRegister(h) diff --git a/hz/template/main.go b/hz/template/main.go index 7a48a4a9..14c32259 100755 --- a/hz/template/main.go +++ b/hz/template/main.go @@ -28,8 +28,8 @@ func main() { h := server.New(server.WithHostPorts(address)) // add a ping route to test - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) router.GeneratedRegister(h) diff --git a/hz/template/template/layout.yaml b/hz/template/template/layout.yaml index 1d8054d7..6eb5130b 100644 --- a/hz/template/template/layout.yaml +++ b/hz/template/template/layout.yaml @@ -34,8 +34,8 @@ layouts: h := server.New(server.WithHostPorts(address)) // add a ping route to test - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) router.GeneratedRegister(h) @@ -561,4 +561,4 @@ layouts: CURDIR=$(cd $(dirname $0); pwd) BinaryName={{.ServiceName}} echo "$CURDIR/bin/${BinaryName}" - exec $CURDIR/bin/${BinaryName} \ No newline at end of file + exec $CURDIR/bin/${BinaryName} diff --git a/hz_kitex_demo/hertz-server/main.go b/hz_kitex_demo/hertz-server/main.go index f60bb230..b95a03fb 100755 --- a/hz_kitex_demo/hertz-server/main.go +++ b/hz_kitex_demo/hertz-server/main.go @@ -25,9 +25,9 @@ func main() { // We can define some global middleware here, such as link tracing, telemetry, cross-domain requests, etc. // Currently, hertz provides a series of middleware extensions that you can refer to if you need them: https://github.com/hertz-contrib - h.Use(func(c context.Context, ctx *app.RequestContext) { + h.Use(func(ctx context.Context, c *app.RequestContext) { fmt.Println("pre-handler") - ctx.Next(c) + c.Next(ctx) fmt.Println("post-handler") }) diff --git a/middleware/csrf/custom_errorfunc/main.go b/middleware/csrf/custom_errorfunc/main.go index 1be6481c..65729adf 100644 --- a/middleware/csrf/custom_errorfunc/main.go +++ b/middleware/csrf/custom_errorfunc/main.go @@ -39,18 +39,18 @@ var ( ) // myErrFunc is executed when an error occurs in csrf middleware. -func myErrFunc(_ context.Context, ctx *app.RequestContext) { - err := ctx.Errors.Last() +func myErrFunc(_ context.Context, c *app.RequestContext) { + err := c.Errors.Last() switch err { case errMissingForm, errMissingParam, errMissingHeader, errMissingQuery: - ctx.String(http.StatusBadRequest, err.Error()) // extract csrf-token failed + c.String(http.StatusBadRequest, err.Error()) // extract csrf-token failed case errMissingSalt: fmt.Println(err.Error()) - ctx.String(http.StatusInternalServerError, err.Error()) // get salt failed,which is unexpected + c.String(http.StatusInternalServerError, err.Error()) // get salt failed,which is unexpected case errInvalidToken: - ctx.String(http.StatusBadRequest, err.Error()) // csrf-token is invalid + c.String(http.StatusBadRequest, err.Error()) // csrf-token is invalid } - ctx.Abort() + c.Abort() } func main() { @@ -60,11 +60,11 @@ func main() { h.Use(sessions.New("csrf-session", store)) h.Use(csrf.New(csrf.WithErrorFunc(myErrFunc))) - h.GET("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, csrf.GetToken(c)) }) - h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, "CSRF token is valid") + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, "CSRF token is valid") }) h.Spin() diff --git a/middleware/csrf/custom_extractor/main.go b/middleware/csrf/custom_extractor/main.go index fd658882..e2d89d99 100644 --- a/middleware/csrf/custom_extractor/main.go +++ b/middleware/csrf/custom_extractor/main.go @@ -27,8 +27,8 @@ import ( "github.com/hertz-contrib/sessions/cookie" ) -func myExtractor(_ context.Context, ctx *app.RequestContext) (string, error) { - token := ctx.FormValue("csrf-token") +func myExtractor(_ context.Context, c *app.RequestContext) (string, error) { + token := c.FormValue("csrf-token") if token == nil { return "", errors.New("missing token in form-data") // get csrf-token from form-data failed } @@ -42,11 +42,11 @@ func main() { h.Use(sessions.New("csrf-session", store)) h.Use(csrf.New(csrf.WithExtractor(myExtractor))) - h.GET("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, csrf.GetToken(c)) }) - h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, "CSRF token is valid") + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, "CSRF token is valid") }) h.Spin() diff --git a/middleware/csrf/custom_ignoremethods/main.go b/middleware/csrf/custom_ignoremethods/main.go index 330635fe..17504e50 100644 --- a/middleware/csrf/custom_ignoremethods/main.go +++ b/middleware/csrf/custom_ignoremethods/main.go @@ -33,12 +33,12 @@ func main() { h.Use(sessions.New("csrf-session", store)) h.Use(csrf.New(csrf.WithIgnoredMethods([]string{"GET", "HEAD", "TRACE"}))) - h.GET("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, csrf.GetToken(c)) }) - h.OPTIONS("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, "success") + h.OPTIONS("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, "success") }) h.Spin() } diff --git a/middleware/csrf/custom_keylookup/main.go b/middleware/csrf/custom_keylookup/main.go index 3d5368cd..6d5c2bbb 100644 --- a/middleware/csrf/custom_keylookup/main.go +++ b/middleware/csrf/custom_keylookup/main.go @@ -32,11 +32,11 @@ func main() { h.Use(sessions.New("csrf-session", store)) h.Use(csrf.New(csrf.WithKeyLookUp("form:csrf"))) - h.GET("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, csrf.GetToken(c)) }) - h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, "CSRF token is valid") + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, "CSRF token is valid") }) h.Spin() diff --git a/middleware/csrf/custom_next/main.go b/middleware/csrf/custom_next/main.go index 2da6f264..04b8b443 100644 --- a/middleware/csrf/custom_next/main.go +++ b/middleware/csrf/custom_next/main.go @@ -25,8 +25,8 @@ import ( "github.com/hertz-contrib/sessions/cookie" ) -func isPostMethod(_ context.Context, ctx *app.RequestContext) bool { - if string(ctx.Method()) == "POST" { +func isPostMethod(_ context.Context, c *app.RequestContext) bool { + if string(c.Method()) == "POST" { return true } else { return false @@ -42,8 +42,8 @@ func main() { // skip csrf middleware when request method is post h.Use(csrf.New(csrf.WithNext(isPostMethod))) - h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, "success even no csrf-token in header") + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, "success even no csrf-token in header") }) h.Spin() } diff --git a/middleware/csrf/custom_secret/main.go b/middleware/csrf/custom_secret/main.go index 54ecb84f..39e488a0 100644 --- a/middleware/csrf/custom_secret/main.go +++ b/middleware/csrf/custom_secret/main.go @@ -33,11 +33,11 @@ func main() { h.Use(sessions.New("csrf-session", store)) h.Use(csrf.New(csrf.WithSecret("your_secret"))) - h.GET("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, csrf.GetToken(c)) }) - h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, "CSRF token is valid") + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, "CSRF token is valid") }) h.Spin() diff --git a/middleware/csrf/default/main.go b/middleware/csrf/default/main.go index 3387260e..d7b36563 100644 --- a/middleware/csrf/default/main.go +++ b/middleware/csrf/default/main.go @@ -33,12 +33,12 @@ func main() { h.Use(sessions.New("csrf-session", store)) h.Use(csrf.New()) - h.GET("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, csrf.GetToken(c)) }) - h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, "CSRF token is valid") + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { + c.String(200, "CSRF token is valid") }) h.Spin() diff --git a/middleware/loadbalance/round_robin/server/main.go b/middleware/loadbalance/round_robin/server/main.go index 7998eb1e..6d778751 100644 --- a/middleware/loadbalance/round_robin/server/main.go +++ b/middleware/loadbalance/round_robin/server/main.go @@ -50,8 +50,8 @@ func main() { Tags: nil, }), ) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"addr": addr}) + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.JSON(consts.StatusOK, utils.H{"addr": addr}) }) h.Spin() wg.Done() diff --git a/middleware/pprof/custom_prefix/server.go b/middleware/pprof/custom_prefix/server.go index 75f0b3eb..cbca2c05 100644 --- a/middleware/pprof/custom_prefix/server.go +++ b/middleware/pprof/custom_prefix/server.go @@ -32,8 +32,8 @@ func main() { // default is "debug/pprof" pprof.Register(h, "dev/pprof") - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) h.Spin() diff --git a/middleware/pprof/custom_router_group/server.go b/middleware/pprof/custom_router_group/server.go index 794ed6eb..220f97a0 100644 --- a/middleware/pprof/custom_router_group/server.go +++ b/middleware/pprof/custom_router_group/server.go @@ -33,8 +33,8 @@ func main() { adminGroup := h.Group("/admin") - adminGroup.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + adminGroup.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) pprof.RouteRegister(adminGroup, "pprof") diff --git a/middleware/pprof/default/server.go b/middleware/pprof/default/server.go index ab25b965..e61ddc72 100644 --- a/middleware/pprof/default/server.go +++ b/middleware/pprof/default/server.go @@ -31,8 +31,8 @@ func main() { pprof.Register(h) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) h.Spin() diff --git a/monitoring/README.md b/monitoring/README.md index 1dc27331..4f457342 100644 --- a/monitoring/README.md +++ b/monitoring/README.md @@ -18,13 +18,13 @@ func main() { ... h := server.Default(server.WithHostPorts("127.0.0.1:8080"), server.WithTracer(prometheus.NewServerTracer(":9091", "/hertz"))) - h.GET("/metricGet", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, "hello get") + h.GET("/metricGet", func(ctx context.Context, c *app.RequestContext) { + c.String(200, "hello get") }) - h.POST("/metricPost", func(c context.Context, ctx *app.RequestContext) { + h.POST("/metricPost", func(ctx context.Context, c *app.RequestContext) { time.Sleep(100 * time.Millisecond) - ctx.String(200, "hello post") + c.String(200, "hello post") }) h.Spin() @@ -55,4 +55,4 @@ func main() { `histogram_quantile(0.9,sum(rate(hertz_server_latency_us_bucket{statusCode="200"}[1m]))by(le))` -For more information about hertz monitoring, please click [monitoring](https://www.cloudwego.io/zh/docs/hertz/tutorials/framework-exten/monitor/) \ No newline at end of file +For more information about hertz monitoring, please click [monitoring](https://www.cloudwego.io/zh/docs/hertz/tutorials/framework-exten/monitor/) diff --git a/monitoring/main.go b/monitoring/main.go index 938dbedf..87ccde09 100644 --- a/monitoring/main.go +++ b/monitoring/main.go @@ -35,13 +35,13 @@ func main() { ), ) - h.GET("/metricGet", func(c context.Context, ctx *app.RequestContext) { - ctx.String(200, "hello get") + h.GET("/metricGet", func(ctx context.Context, c *app.RequestContext) { + c.String(200, "hello get") }) - h.POST("/metricPost", func(c context.Context, ctx *app.RequestContext) { + h.POST("/metricPost", func(ctx context.Context, c *app.RequestContext) { time.Sleep(100 * time.Millisecond) - ctx.String(200, "hello post") + c.String(200, "hello post") }) h.Spin() diff --git a/multiple_service/main.go b/multiple_service/main.go index 13e572cb..0fa9a1ae 100644 --- a/multiple_service/main.go +++ b/multiple_service/main.go @@ -30,16 +30,16 @@ var wg sync.WaitGroup func hertz1() { h := server.Default(server.WithHostPorts(":8080")) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong1"}) + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.JSON(consts.StatusOK, utils.H{"ping": "pong1"}) }) h.Spin() } func hertz2() { h := server.Default(server.WithHostPorts(":8081")) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) }) h.Spin() } diff --git a/opentelemetry/README.md b/opentelemetry/README.md index 7f2bc496..3d4b06b7 100644 --- a/opentelemetry/README.md +++ b/opentelemetry/README.md @@ -108,14 +108,14 @@ func init() { #### log with context ```go -h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { +h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { req := &api.Request{Message: "my request"} - resp, err := client.Echo(c, req) + resp, err := client.Echo(ctx, req) if err != nil { hlog.Errorf(err.Error()) } hlog.CtxDebugf(c, "message received successfully: %s", req.Message) - ctx.JSON(consts.StatusOK, resp) + c.JSON(consts.StatusOK, resp) }) ``` diff --git a/opentelemetry/hertz/server/main.go b/opentelemetry/hertz/server/main.go index 7afd781e..c40e4311 100644 --- a/opentelemetry/hertz/server/main.go +++ b/opentelemetry/hertz/server/main.go @@ -65,14 +65,14 @@ func main() { hlog.Fatal(err) } - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { req := &api.Request{Message: "my request"} - resp, err := client.Echo(c, req) + resp, err := client.Echo(ctx, req) if err != nil { hlog.Errorf(err.Error()) } - hlog.CtxDebugf(c, "message received successfully: %s", req.Message) - ctx.JSON(consts.StatusOK, resp) + hlog.CtxDebugf(ctx, "message received successfully: %s", req.Message) + c.JSON(consts.StatusOK, resp) }) h.Spin() diff --git a/protocol/http1/main.go b/protocol/http1/main.go index 0325c395..3c190a15 100644 --- a/protocol/http1/main.go +++ b/protocol/http1/main.go @@ -28,8 +28,8 @@ import ( func main() { h := server.Default(server.WithHostPorts("127.0.0.1:8080")) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) h.Spin() diff --git a/protocol/tls/main.go b/protocol/tls/main.go index 856dee1d..a5c97cb2 100644 --- a/protocol/tls/main.go +++ b/protocol/tls/main.go @@ -50,14 +50,14 @@ func main() { h := server.Default(server.WithTLS(cfg), server.WithHostPorts(":8443")) - h.Use(func(c context.Context, ctx *app.RequestContext) { - fmt.Fprint(ctx, "Before real handle...\n") - ctx.Next(c) - fmt.Fprint(ctx, "After real handle...\n") + h.Use(func(ctx context.Context, c *app.RequestContext) { + fmt.Fprint(c, "Before real handle...\n") + c.Next(ctx) + fmt.Fprint(c, "After real handle...\n") }) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.String(consts.StatusOK, "TLS test\n") + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.String(consts.StatusOK, "TLS test\n") }) go func() { diff --git a/render/html/main.go b/render/html/main.go index ee2658e5..d0186b5e 100644 --- a/render/html/main.go +++ b/render/html/main.go @@ -42,14 +42,14 @@ func main() { }) h.LoadHTMLGlob("render/html/*") - h.GET("/index", func(c context.Context, ctx *app.RequestContext) { - ctx.HTML(http.StatusOK, "index.tmpl", utils.H{ + h.GET("/index", func(ctx context.Context, c *app.RequestContext) { + c.HTML(http.StatusOK, "index.tmpl", utils.H{ "title": "Main website", }) }) - h.GET("/raw", func(c context.Context, ctx *app.RequestContext) { - ctx.HTML(http.StatusOK, "template1.html", map[string]interface{}{ + h.GET("/raw", func(ctx context.Context, c *app.RequestContext) { + c.HTML(http.StatusOK, "template1.html", map[string]interface{}{ "now": time.Date(2017, 0o7, 0o1, 0, 0, 0, 0, time.UTC), }) }) diff --git a/reverseproxy/customize_error/main.go b/reverseproxy/customize_error/main.go index b4ff16b2..0213ed21 100644 --- a/reverseproxy/customize_error/main.go +++ b/reverseproxy/customize_error/main.go @@ -36,7 +36,7 @@ func main() { c.String(404, "fake 404 not found") }) - h.GET("/proxy/backend", func(cc context.Context, c *app.RequestContext) { + h.GET("/proxy/backend", func(ctx context.Context, c *app.RequestContext) { c.JSON(200, utils.H{ "msg": "proxy success!!", }) diff --git a/reverseproxy/discovery/registry/main.go b/reverseproxy/discovery/registry/main.go index b2f94b6d..df172927 100644 --- a/reverseproxy/discovery/registry/main.go +++ b/reverseproxy/discovery/registry/main.go @@ -38,7 +38,7 @@ func main() { Weight: 10, }), ) - h.GET("/backend", func(cc context.Context, c *app.RequestContext) { + h.GET("/backend", func(ctx context.Context, c *app.RequestContext) { c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) h.Spin() diff --git a/reverseproxy/standard/main.go b/reverseproxy/standard/main.go index cf7fe814..ee3e6dd8 100644 --- a/reverseproxy/standard/main.go +++ b/reverseproxy/standard/main.go @@ -30,7 +30,7 @@ func main() { if err != nil { panic(err) } - h.GET("/proxy/backend", func(cc context.Context, c *app.RequestContext) { + h.GET("/proxy/backend", func(ctx context.Context, c *app.RequestContext) { if param := c.Query("who"); param != "" { c.JSON(200, utils.H{ "who": param, diff --git a/reverseproxy/tls/main.go b/reverseproxy/tls/main.go index b5e29ddd..c166897d 100644 --- a/reverseproxy/tls/main.go +++ b/reverseproxy/tls/main.go @@ -64,7 +64,7 @@ func main() { server.WithTransport(standard.NewTransporter), ) - h.GET("/backend", func(cc context.Context, c *app.RequestContext) { + h.GET("/backend", func(ctx context.Context, c *app.RequestContext) { c.JSON(200, utils.H{"msg": "pong"}) }) h.Spin() diff --git a/reverseproxy/use_middleware/main.go b/reverseproxy/use_middleware/main.go index 83236871..a71c6ed4 100644 --- a/reverseproxy/use_middleware/main.go +++ b/reverseproxy/use_middleware/main.go @@ -35,24 +35,24 @@ func main() { panic(err) } - r.Use(func(c context.Context, ctx *app.RequestContext) { - if ctx.Query("country") == "cn" { - proxy.ServeHTTP(c, ctx) - ctx.Response.Header.Set("key", "value") - ctx.Abort() + r.Use(func(ctx context.Context, c *app.RequestContext) { + if c.Query("country") == "cn" { + proxy.ServeHTTP(ctx, c) + c.Response.Header.Set("key", "value") + c.Abort() } else { - ctx.Next(c) + c.Next(ctx) } }) - r.GET("/backend", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(200, utils.H{ + r.GET("/backend", func(ctx context.Context, c *app.RequestContext) { + c.JSON(200, utils.H{ "message": "pong1", }) }) - r2.GET("/backend", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(200, utils.H{ + r2.GET("/backend", func(ctx context.Context, c *app.RequestContext) { + c.JSON(200, utils.H{ "message": "pong2", }) }) diff --git a/route/main.go b/route/main.go index df8b2d35..79917cfe 100644 --- a/route/main.go +++ b/route/main.go @@ -118,8 +118,8 @@ func RegisterGroupRouteWithMiddleware(h *server.Hertz) { // // Bind the middleware directly to the routing group example1 := h.Group("/example1", basic_auth.BasicAuth(map[string]string{"test": "test"})) - example1.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.String(consts.StatusOK, "ping") + example1.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.String(consts.StatusOK, "ping") }) // Sample Code 2: @@ -127,8 +127,8 @@ func RegisterGroupRouteWithMiddleware(h *server.Hertz) { // use `Use` method example2 := h.Group("/example2") example2.Use(basic_auth.BasicAuth(map[string]string{"test": "test"})) - example2.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.String(consts.StatusOK, "ping") + example2.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.String(consts.StatusOK, "ping") }) } @@ -157,15 +157,15 @@ func RegisterParaRoute(h *server.Hertz) { // RegisterAnonFunOrDecRoute Use anonymous function or decorator to register routes func RegisterAnonFunOrDecRoute(h *server.Hertz) { - h.AnyEX("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.String(consts.StatusOK, app.GetHandlerName(ctx.Handler())) + h.AnyEX("/ping", func(ctx context.Context, c *app.RequestContext) { + c.String(consts.StatusOK, app.GetHandlerName(c.Handler())) }, "ping_handler") } // RegisterGetRoutesInfo Get route info func RegisterGetRoutesInfo(h *server.Hertz) { - h.GET("/getRoutesInfo", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + h.GET("/getRoutesInfo", func(ctx context.Context, c *app.RequestContext) { + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) routeInfo := h.Routes() hlog.Info(routeInfo) diff --git a/sentinel/hertz/server/main.go b/sentinel/hertz/server/main.go index 38d87bb5..6f499b97 100644 --- a/sentinel/hertz/server/main.go +++ b/sentinel/hertz/server/main.go @@ -54,18 +54,18 @@ func main() { h.Use(hertzSentinel.SentinelServerMiddleware( // customize resource extractor if required // method_path by default - hertzSentinel.WithServerResourceExtractor(func(c context.Context, ctx *app.RequestContext) string { + hertzSentinel.WithServerResourceExtractor(func(ctx context.Context, c *app.RequestContext) string { return "server_test" }), // customize block fallback if required // abort with status 429 by default - hertzSentinel.WithServerBlockFallback(func(c context.Context, ctx *app.RequestContext) { - ctx.AbortWithStatusJSON(400, utils.H{ + hertzSentinel.WithServerBlockFallback(func(ctx context.Context, c *app.RequestContext) { + c.AbortWithStatusJSON(400, utils.H{ "err": "too many request; the quota used up", "code": 10222, }) }), )) - h.GET("/server_test", func(c context.Context, ctx *app.RequestContext) {}) + h.GET("/server_test", func(ctx context.Context, c *app.RequestContext) {}) h.Spin() } diff --git a/tracer/hertz/server/main.go b/tracer/hertz/server/main.go index 363b4a58..0ed00dc2 100644 --- a/tracer/hertz/server/main.go +++ b/tracer/hertz/server/main.go @@ -84,23 +84,23 @@ func main() { // You can refer to the example to implement a tracer middleware yourself to get the metrics you want. h.Use(hertztracer.ServerCtx()) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { type PingReq struct { Name string `query:"name"` } var hertzReq PingReq - err := ctx.BindAndValidate(&hertzReq) + err := c.BindAndValidate(&hertzReq) if err != nil { hlog.Errorf(err.Error()) return } KitexReq := &api.Request{Message: hertzReq.Name} - resp, err := client.Echo(c, KitexReq) + resp, err := client.Echo(ctx, KitexReq) if err != nil { hlog.Errorf(err.Error()) } - ctx.JSON(consts.StatusOK, resp) + c.JSON(consts.StatusOK, resp) }) h.Spin()