From 3ffc942c01baf73f2fe92be73f225716c3780c0b Mon Sep 17 00:00:00 2001 From: Skyenought Date: Mon, 29 Jul 2024 14:44:27 +0800 Subject: [PATCH 1/5] style: unified `app.HandleFunc` style --- bizdemo/tiktok_demo/main.go | 2 +- file/html-fs/main.go | 2 +- file/html/main.go | 2 +- hex/hex_trans_handler.go | 2 +- hz/template/main.go | 2 +- hz/template/template/layout.yaml | 4 ++-- hz_kitex_demo/hertz-server/main.go | 2 +- middleware/csrf/custom_errorfunc/main.go | 4 ++-- middleware/csrf/custom_extractor/main.go | 4 ++-- middleware/csrf/custom_ignoremethods/main.go | 4 ++-- middleware/csrf/custom_keylookup/main.go | 4 ++-- middleware/csrf/custom_next/main.go | 2 +- middleware/csrf/custom_secret/main.go | 4 ++-- middleware/csrf/default/main.go | 4 ++-- middleware/loadbalance/round_robin/server/main.go | 2 +- middleware/pprof/custom_prefix/server.go | 2 +- middleware/pprof/custom_router_group/server.go | 2 +- middleware/pprof/default/server.go | 2 +- monitoring/README.md | 6 +++--- monitoring/main.go | 4 ++-- multiple_service/main.go | 4 ++-- opentelemetry/README.md | 2 +- opentelemetry/hertz/server/main.go | 2 +- protocol/http1/main.go | 2 +- protocol/tls/main.go | 4 ++-- render/html/main.go | 4 ++-- reverseproxy/customize_error/main.go | 2 +- reverseproxy/discovery/registry/main.go | 2 +- reverseproxy/standard/main.go | 2 +- reverseproxy/tls/main.go | 2 +- reverseproxy/use_middleware/main.go | 6 +++--- route/main.go | 8 ++++---- sentinel/hertz/server/main.go | 6 +++--- tracer/hertz/server/main.go | 2 +- 34 files changed, 54 insertions(+), 54 deletions(-) diff --git a/bizdemo/tiktok_demo/main.go b/bizdemo/tiktok_demo/main.go index 54e50c55..d33725dd 100644 --- a/bizdemo/tiktok_demo/main.go +++ b/bizdemo/tiktok_demo/main.go @@ -32,7 +32,7 @@ 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())) diff --git a/file/html-fs/main.go b/file/html-fs/main.go index 042c61d7..9c7d767c 100644 --- a/file/html-fs/main.go +++ b/file/html-fs/main.go @@ -33,7 +33,7 @@ func main() { fs := &app.FS{Root: root, PathRewrite: getPathRewriter(prefix)} h.StaticFS(prefix, fs) - h.GET("/", func(c context.Context, ctx *app.RequestContext) { + h.GET("/", func(ctx context.Context, c *app.RequestContext) { ctx.HTML(200, "index.html", nil) }) h.Spin() diff --git a/file/html/main.go b/file/html/main.go index bfde00e6..e488b590 100644 --- a/file/html/main.go +++ b/file/html/main.go @@ -29,7 +29,7 @@ func main() { h.Static("/", "./assets") - h.GET("/", func(c context.Context, ctx *app.RequestContext) { + h.GET("/", func(ctx context.Context, c *app.RequestContext) { ctx.HTML(200, "index.html", nil) }) h.Spin() diff --git a/hex/hex_trans_handler.go b/hex/hex_trans_handler.go index 43ba0604..56f831b4 100644 --- a/hex/hex_trans_handler.go +++ b/hex/hex_trans_handler.go @@ -90,7 +90,7 @@ func initHertz() *route.Engine { h := hertzServer.New() // add a ping route to test - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) diff --git a/hz/template/main.go b/hz/template/main.go index 7a48a4a9..b9efc8f8 100755 --- a/hz/template/main.go +++ b/hz/template/main.go @@ -28,7 +28,7 @@ func main() { h := server.New(server.WithHostPorts(address)) // add a ping route to test - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) diff --git a/hz/template/template/layout.yaml b/hz/template/template/layout.yaml index 1d8054d7..7cdaea98 100644 --- a/hz/template/template/layout.yaml +++ b/hz/template/template/layout.yaml @@ -34,7 +34,7 @@ layouts: h := server.New(server.WithHostPorts(address)) // add a ping route to test - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) @@ -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..6238db2d 100755 --- a/hz_kitex_demo/hertz-server/main.go +++ b/hz_kitex_demo/hertz-server/main.go @@ -25,7 +25,7 @@ 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) fmt.Println("post-handler") diff --git a/middleware/csrf/custom_errorfunc/main.go b/middleware/csrf/custom_errorfunc/main.go index 1be6481c..a443863a 100644 --- a/middleware/csrf/custom_errorfunc/main.go +++ b/middleware/csrf/custom_errorfunc/main.go @@ -60,10 +60,10 @@ 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) { + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, csrf.GetToken(ctx)) }) - h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, "CSRF token is valid") }) diff --git a/middleware/csrf/custom_extractor/main.go b/middleware/csrf/custom_extractor/main.go index fd658882..32b46003 100644 --- a/middleware/csrf/custom_extractor/main.go +++ b/middleware/csrf/custom_extractor/main.go @@ -42,10 +42,10 @@ 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) { + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, csrf.GetToken(ctx)) }) - h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, "CSRF token is valid") }) diff --git a/middleware/csrf/custom_ignoremethods/main.go b/middleware/csrf/custom_ignoremethods/main.go index 330635fe..1877b9e8 100644 --- a/middleware/csrf/custom_ignoremethods/main.go +++ b/middleware/csrf/custom_ignoremethods/main.go @@ -33,11 +33,11 @@ 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) { + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, csrf.GetToken(ctx)) }) - h.OPTIONS("/protected", func(c context.Context, ctx *app.RequestContext) { + h.OPTIONS("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, "success") }) h.Spin() diff --git a/middleware/csrf/custom_keylookup/main.go b/middleware/csrf/custom_keylookup/main.go index 3d5368cd..f0242dd2 100644 --- a/middleware/csrf/custom_keylookup/main.go +++ b/middleware/csrf/custom_keylookup/main.go @@ -32,10 +32,10 @@ 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) { + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, csrf.GetToken(ctx)) }) - h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, "CSRF token is valid") }) diff --git a/middleware/csrf/custom_next/main.go b/middleware/csrf/custom_next/main.go index 2da6f264..871975e1 100644 --- a/middleware/csrf/custom_next/main.go +++ b/middleware/csrf/custom_next/main.go @@ -42,7 +42,7 @@ 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) { + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.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..c75ae0b0 100644 --- a/middleware/csrf/custom_secret/main.go +++ b/middleware/csrf/custom_secret/main.go @@ -33,10 +33,10 @@ 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) { + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, csrf.GetToken(ctx)) }) - h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, "CSRF token is valid") }) diff --git a/middleware/csrf/default/main.go b/middleware/csrf/default/main.go index 3387260e..f2c8d5a2 100644 --- a/middleware/csrf/default/main.go +++ b/middleware/csrf/default/main.go @@ -33,11 +33,11 @@ func main() { h.Use(sessions.New("csrf-session", store)) h.Use(csrf.New()) - h.GET("/protected", func(c context.Context, ctx *app.RequestContext) { + h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, csrf.GetToken(ctx)) }) - h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { + h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { ctx.String(200, "CSRF token is valid") }) diff --git a/middleware/loadbalance/round_robin/server/main.go b/middleware/loadbalance/round_robin/server/main.go index 7998eb1e..bd769478 100644 --- a/middleware/loadbalance/round_robin/server/main.go +++ b/middleware/loadbalance/round_robin/server/main.go @@ -50,7 +50,7 @@ func main() { Tags: nil, }), ) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(consts.StatusOK, utils.H{"addr": addr}) }) h.Spin() diff --git a/middleware/pprof/custom_prefix/server.go b/middleware/pprof/custom_prefix/server.go index 75f0b3eb..a1d9f606 100644 --- a/middleware/pprof/custom_prefix/server.go +++ b/middleware/pprof/custom_prefix/server.go @@ -32,7 +32,7 @@ func main() { // default is "debug/pprof" pprof.Register(h, "dev/pprof") - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) diff --git a/middleware/pprof/custom_router_group/server.go b/middleware/pprof/custom_router_group/server.go index 794ed6eb..89ec1062 100644 --- a/middleware/pprof/custom_router_group/server.go +++ b/middleware/pprof/custom_router_group/server.go @@ -33,7 +33,7 @@ func main() { adminGroup := h.Group("/admin") - adminGroup.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + adminGroup.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) diff --git a/middleware/pprof/default/server.go b/middleware/pprof/default/server.go index ab25b965..4081137f 100644 --- a/middleware/pprof/default/server.go +++ b/middleware/pprof/default/server.go @@ -31,7 +31,7 @@ func main() { pprof.Register(h) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) diff --git a/monitoring/README.md b/monitoring/README.md index 1dc27331..2d8a3c2c 100644 --- a/monitoring/README.md +++ b/monitoring/README.md @@ -18,11 +18,11 @@ 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) { + h.GET("/metricGet", func(ctx context.Context, c *app.RequestContext) { ctx.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") }) @@ -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..139986ca 100644 --- a/monitoring/main.go +++ b/monitoring/main.go @@ -35,11 +35,11 @@ func main() { ), ) - h.GET("/metricGet", func(c context.Context, ctx *app.RequestContext) { + h.GET("/metricGet", func(ctx context.Context, c *app.RequestContext) { ctx.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") }) diff --git a/multiple_service/main.go b/multiple_service/main.go index 13e572cb..b66350ae 100644 --- a/multiple_service/main.go +++ b/multiple_service/main.go @@ -30,7 +30,7 @@ var wg sync.WaitGroup func hertz1() { h := server.Default(server.WithHostPorts(":8080")) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(consts.StatusOK, utils.H{"ping": "pong1"}) }) h.Spin() @@ -38,7 +38,7 @@ func hertz1() { func hertz2() { h := server.Default(server.WithHostPorts(":8081")) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) }) h.Spin() diff --git a/opentelemetry/README.md b/opentelemetry/README.md index 7f2bc496..49496fd0 100644 --- a/opentelemetry/README.md +++ b/opentelemetry/README.md @@ -108,7 +108,7 @@ 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) if err != nil { diff --git a/opentelemetry/hertz/server/main.go b/opentelemetry/hertz/server/main.go index 7afd781e..f05264e5 100644 --- a/opentelemetry/hertz/server/main.go +++ b/opentelemetry/hertz/server/main.go @@ -65,7 +65,7 @@ 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) if err != nil { diff --git a/protocol/http1/main.go b/protocol/http1/main.go index 0325c395..ff3dc58a 100644 --- a/protocol/http1/main.go +++ b/protocol/http1/main.go @@ -28,7 +28,7 @@ import ( func main() { h := server.Default(server.WithHostPorts("127.0.0.1:8080")) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) diff --git a/protocol/tls/main.go b/protocol/tls/main.go index 856dee1d..37dd98ae 100644 --- a/protocol/tls/main.go +++ b/protocol/tls/main.go @@ -50,13 +50,13 @@ func main() { h := server.Default(server.WithTLS(cfg), server.WithHostPorts(":8443")) - h.Use(func(c context.Context, ctx *app.RequestContext) { + h.Use(func(ctx context.Context, c *app.RequestContext) { fmt.Fprint(ctx, "Before real handle...\n") ctx.Next(c) fmt.Fprint(ctx, "After real handle...\n") }) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.String(consts.StatusOK, "TLS test\n") }) diff --git a/render/html/main.go b/render/html/main.go index ee2658e5..c7d1c97d 100644 --- a/render/html/main.go +++ b/render/html/main.go @@ -42,13 +42,13 @@ func main() { }) h.LoadHTMLGlob("render/html/*") - h.GET("/index", func(c context.Context, ctx *app.RequestContext) { + h.GET("/index", func(ctx context.Context, c *app.RequestContext) { ctx.HTML(http.StatusOK, "index.tmpl", utils.H{ "title": "Main website", }) }) - h.GET("/raw", func(c context.Context, ctx *app.RequestContext) { + h.GET("/raw", func(ctx context.Context, c *app.RequestContext) { ctx.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..1f2bba3c 100644 --- a/reverseproxy/use_middleware/main.go +++ b/reverseproxy/use_middleware/main.go @@ -35,7 +35,7 @@ func main() { panic(err) } - r.Use(func(c context.Context, ctx *app.RequestContext) { + r.Use(func(ctx context.Context, c *app.RequestContext) { if ctx.Query("country") == "cn" { proxy.ServeHTTP(c, ctx) ctx.Response.Header.Set("key", "value") @@ -45,13 +45,13 @@ func main() { } }) - r.GET("/backend", func(c context.Context, ctx *app.RequestContext) { + r.GET("/backend", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(200, utils.H{ "message": "pong1", }) }) - r2.GET("/backend", func(c context.Context, ctx *app.RequestContext) { + r2.GET("/backend", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(200, utils.H{ "message": "pong2", }) diff --git a/route/main.go b/route/main.go index df8b2d35..e3f31eba 100644 --- a/route/main.go +++ b/route/main.go @@ -118,7 +118,7 @@ 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) { + example1.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.String(consts.StatusOK, "ping") }) @@ -127,7 +127,7 @@ 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) { + example2.GET("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.String(consts.StatusOK, "ping") }) } @@ -157,14 +157,14 @@ 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) { + h.AnyEX("/ping", func(ctx context.Context, c *app.RequestContext) { ctx.String(consts.StatusOK, app.GetHandlerName(ctx.Handler())) }, "ping_handler") } // RegisterGetRoutesInfo Get route info func RegisterGetRoutesInfo(h *server.Hertz) { - h.GET("/getRoutesInfo", func(c context.Context, ctx *app.RequestContext) { + h.GET("/getRoutesInfo", func(ctx context.Context, c *app.RequestContext) { ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) routeInfo := h.Routes() diff --git a/sentinel/hertz/server/main.go b/sentinel/hertz/server/main.go index 38d87bb5..5f90aa8f 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) { + hertzSentinel.WithServerBlockFallback(func(ctx context.Context, c *app.RequestContext) { ctx.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..e7aa6c44 100644 --- a/tracer/hertz/server/main.go +++ b/tracer/hertz/server/main.go @@ -84,7 +84,7 @@ 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"` } From 4bacdde6e2b1c729ef613479b531e6eeb53be3d6 Mon Sep 17 00:00:00 2001 From: Skyenought Date: Mon, 29 Jul 2024 14:56:19 +0800 Subject: [PATCH 2/5] fix --- bizdemo/tiktok_demo/main.go | 6 +++--- file/html-fs/main.go | 2 +- file/html/main.go | 2 +- file/staticFile/main.go | 4 ++-- hex/hex_trans_handler.go | 2 +- hz/template/main.go | 2 +- hz/template/template/layout.yaml | 2 +- hz_kitex_demo/hertz-server/main.go | 2 +- middleware/csrf/custom_errorfunc/main.go | 16 ++++++++-------- middleware/csrf/custom_extractor/main.go | 8 ++++---- middleware/csrf/custom_ignoremethods/main.go | 4 ++-- middleware/csrf/custom_keylookup/main.go | 4 ++-- middleware/csrf/custom_next/main.go | 6 +++--- middleware/csrf/custom_secret/main.go | 4 ++-- middleware/csrf/default/main.go | 4 ++-- .../loadbalance/round_robin/server/main.go | 2 +- middleware/pprof/custom_prefix/server.go | 2 +- middleware/pprof/custom_router_group/server.go | 2 +- middleware/pprof/default/server.go | 2 +- monitoring/README.md | 4 ++-- monitoring/main.go | 4 ++-- multiple_service/main.go | 4 ++-- opentelemetry/README.md | 2 +- opentelemetry/hertz/server/main.go | 4 ++-- protocol/http1/main.go | 2 +- protocol/tls/main.go | 8 ++++---- render/html/main.go | 4 ++-- reverseproxy/use_middleware/main.go | 14 +++++++------- route/main.go | 8 ++++---- sentinel/hertz/server/main.go | 2 +- tracer/hertz/server/main.go | 4 ++-- 31 files changed, 68 insertions(+), 68 deletions(-) diff --git a/bizdemo/tiktok_demo/main.go b/bizdemo/tiktok_demo/main.go index d33725dd..29f98edc 100644 --- a/bizdemo/tiktok_demo/main.go +++ b/bizdemo/tiktok_demo/main.go @@ -34,9 +34,9 @@ import ( // Set up /src/*name route forwarding to access minio from external network 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 9c7d767c..cda40d8b 100644 --- a/file/html-fs/main.go +++ b/file/html-fs/main.go @@ -34,7 +34,7 @@ func main() { h.StaticFS(prefix, fs) h.GET("/", func(ctx context.Context, c *app.RequestContext) { - ctx.HTML(200, "index.html", nil) + c.HTML(200, "index.html", nil) }) h.Spin() } diff --git a/file/html/main.go b/file/html/main.go index e488b590..9157d166 100644 --- a/file/html/main.go +++ b/file/html/main.go @@ -30,7 +30,7 @@ func main() { h.Static("/", "./assets") h.GET("/", func(ctx context.Context, c *app.RequestContext) { - ctx.HTML(200, "index.html", nil) + 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 56f831b4..2b66e99d 100644 --- a/hex/hex_trans_handler.go +++ b/hex/hex_trans_handler.go @@ -91,7 +91,7 @@ func initHertz() *route.Engine { // add a ping route to test h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) router.GeneratedRegister(h) diff --git a/hz/template/main.go b/hz/template/main.go index b9efc8f8..14c32259 100755 --- a/hz/template/main.go +++ b/hz/template/main.go @@ -29,7 +29,7 @@ func main() { // add a ping route to test h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + 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 7cdaea98..6eb5130b 100644 --- a/hz/template/template/layout.yaml +++ b/hz/template/template/layout.yaml @@ -35,7 +35,7 @@ layouts: // add a ping route to test h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) router.GeneratedRegister(h) diff --git a/hz_kitex_demo/hertz-server/main.go b/hz_kitex_demo/hertz-server/main.go index 6238db2d..99e3ade5 100755 --- a/hz_kitex_demo/hertz-server/main.go +++ b/hz_kitex_demo/hertz-server/main.go @@ -27,7 +27,7 @@ func main() { // 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(ctx context.Context, c *app.RequestContext) { fmt.Println("pre-handler") - ctx.Next(c) + c.Next(c) fmt.Println("post-handler") }) diff --git a/middleware/csrf/custom_errorfunc/main.go b/middleware/csrf/custom_errorfunc/main.go index a443863a..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() { @@ -61,10 +61,10 @@ func main() { h.Use(csrf.New(csrf.WithErrorFunc(myErrFunc))) h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + c.String(200, csrf.GetToken(c)) }) h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, "CSRF token is valid") + 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 32b46003..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 } @@ -43,10 +43,10 @@ func main() { h.Use(csrf.New(csrf.WithExtractor(myExtractor))) h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + c.String(200, csrf.GetToken(c)) }) h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, "CSRF token is valid") + 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 1877b9e8..17504e50 100644 --- a/middleware/csrf/custom_ignoremethods/main.go +++ b/middleware/csrf/custom_ignoremethods/main.go @@ -34,11 +34,11 @@ func main() { h.Use(csrf.New(csrf.WithIgnoredMethods([]string{"GET", "HEAD", "TRACE"}))) h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + c.String(200, csrf.GetToken(c)) }) h.OPTIONS("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, "success") + c.String(200, "success") }) h.Spin() } diff --git a/middleware/csrf/custom_keylookup/main.go b/middleware/csrf/custom_keylookup/main.go index f0242dd2..6d5c2bbb 100644 --- a/middleware/csrf/custom_keylookup/main.go +++ b/middleware/csrf/custom_keylookup/main.go @@ -33,10 +33,10 @@ func main() { h.Use(csrf.New(csrf.WithKeyLookUp("form:csrf"))) h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + c.String(200, csrf.GetToken(c)) }) h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, "CSRF token is valid") + 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 871975e1..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 @@ -43,7 +43,7 @@ func main() { h.Use(csrf.New(csrf.WithNext(isPostMethod))) h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, "success even no csrf-token in header") + 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 c75ae0b0..39e488a0 100644 --- a/middleware/csrf/custom_secret/main.go +++ b/middleware/csrf/custom_secret/main.go @@ -34,10 +34,10 @@ func main() { h.Use(csrf.New(csrf.WithSecret("your_secret"))) h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + c.String(200, csrf.GetToken(c)) }) h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, "CSRF token is valid") + c.String(200, "CSRF token is valid") }) h.Spin() diff --git a/middleware/csrf/default/main.go b/middleware/csrf/default/main.go index f2c8d5a2..d7b36563 100644 --- a/middleware/csrf/default/main.go +++ b/middleware/csrf/default/main.go @@ -34,11 +34,11 @@ func main() { h.Use(csrf.New()) h.GET("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, csrf.GetToken(ctx)) + c.String(200, csrf.GetToken(c)) }) h.POST("/protected", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, "CSRF token is valid") + 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 bd769478..6d778751 100644 --- a/middleware/loadbalance/round_robin/server/main.go +++ b/middleware/loadbalance/round_robin/server/main.go @@ -51,7 +51,7 @@ func main() { }), ) h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"addr": addr}) + 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 a1d9f606..cbca2c05 100644 --- a/middleware/pprof/custom_prefix/server.go +++ b/middleware/pprof/custom_prefix/server.go @@ -33,7 +33,7 @@ func main() { pprof.Register(h, "dev/pprof") h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + 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 89ec1062..220f97a0 100644 --- a/middleware/pprof/custom_router_group/server.go +++ b/middleware/pprof/custom_router_group/server.go @@ -34,7 +34,7 @@ func main() { adminGroup := h.Group("/admin") adminGroup.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + 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 4081137f..e61ddc72 100644 --- a/middleware/pprof/default/server.go +++ b/middleware/pprof/default/server.go @@ -32,7 +32,7 @@ func main() { pprof.Register(h) h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) h.Spin() diff --git a/monitoring/README.md b/monitoring/README.md index 2d8a3c2c..4f457342 100644 --- a/monitoring/README.md +++ b/monitoring/README.md @@ -19,12 +19,12 @@ func main() { h := server.Default(server.WithHostPorts("127.0.0.1:8080"), server.WithTracer(prometheus.NewServerTracer(":9091", "/hertz"))) h.GET("/metricGet", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, "hello get") + c.String(200, "hello get") }) 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/monitoring/main.go b/monitoring/main.go index 139986ca..87ccde09 100644 --- a/monitoring/main.go +++ b/monitoring/main.go @@ -36,12 +36,12 @@ func main() { ) h.GET("/metricGet", func(ctx context.Context, c *app.RequestContext) { - ctx.String(200, "hello get") + c.String(200, "hello get") }) 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 b66350ae..0fa9a1ae 100644 --- a/multiple_service/main.go +++ b/multiple_service/main.go @@ -31,7 +31,7 @@ var wg sync.WaitGroup func hertz1() { h := server.Default(server.WithHostPorts(":8080")) h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong1"}) + c.JSON(consts.StatusOK, utils.H{"ping": "pong1"}) }) h.Spin() } @@ -39,7 +39,7 @@ func hertz1() { func hertz2() { h := server.Default(server.WithHostPorts(":8081")) h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) + c.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) }) h.Spin() } diff --git a/opentelemetry/README.md b/opentelemetry/README.md index 49496fd0..bb96c705 100644 --- a/opentelemetry/README.md +++ b/opentelemetry/README.md @@ -115,7 +115,7 @@ h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { 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 f05264e5..5e753b50 100644 --- a/opentelemetry/hertz/server/main.go +++ b/opentelemetry/hertz/server/main.go @@ -71,8 +71,8 @@ func main() { 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 ff3dc58a..3c190a15 100644 --- a/protocol/http1/main.go +++ b/protocol/http1/main.go @@ -29,7 +29,7 @@ func main() { h := server.Default(server.WithHostPorts("127.0.0.1:8080")) h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + c.JSON(consts.StatusOK, utils.H{"ping": "pong"}) }) h.Spin() diff --git a/protocol/tls/main.go b/protocol/tls/main.go index 37dd98ae..a5c97cb2 100644 --- a/protocol/tls/main.go +++ b/protocol/tls/main.go @@ -51,13 +51,13 @@ func main() { h := server.Default(server.WithTLS(cfg), server.WithHostPorts(":8443")) h.Use(func(ctx context.Context, c *app.RequestContext) { - fmt.Fprint(ctx, "Before real handle...\n") - ctx.Next(c) - fmt.Fprint(ctx, "After real handle...\n") + fmt.Fprint(c, "Before real handle...\n") + c.Next(ctx) + fmt.Fprint(c, "After real handle...\n") }) h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.String(consts.StatusOK, "TLS test\n") + c.String(consts.StatusOK, "TLS test\n") }) go func() { diff --git a/render/html/main.go b/render/html/main.go index c7d1c97d..d0186b5e 100644 --- a/render/html/main.go +++ b/render/html/main.go @@ -43,13 +43,13 @@ func main() { h.LoadHTMLGlob("render/html/*") h.GET("/index", func(ctx context.Context, c *app.RequestContext) { - ctx.HTML(http.StatusOK, "index.tmpl", utils.H{ + c.HTML(http.StatusOK, "index.tmpl", utils.H{ "title": "Main website", }) }) h.GET("/raw", func(ctx context.Context, c *app.RequestContext) { - ctx.HTML(http.StatusOK, "template1.html", map[string]interface{}{ + 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/use_middleware/main.go b/reverseproxy/use_middleware/main.go index 1f2bba3c..a71c6ed4 100644 --- a/reverseproxy/use_middleware/main.go +++ b/reverseproxy/use_middleware/main.go @@ -36,23 +36,23 @@ func main() { } r.Use(func(ctx context.Context, c *app.RequestContext) { - if ctx.Query("country") == "cn" { - proxy.ServeHTTP(c, ctx) - ctx.Response.Header.Set("key", "value") - ctx.Abort() + 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(ctx context.Context, c *app.RequestContext) { - ctx.JSON(200, utils.H{ + c.JSON(200, utils.H{ "message": "pong1", }) }) r2.GET("/backend", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(200, utils.H{ + c.JSON(200, utils.H{ "message": "pong2", }) }) diff --git a/route/main.go b/route/main.go index e3f31eba..79917cfe 100644 --- a/route/main.go +++ b/route/main.go @@ -119,7 +119,7 @@ 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(ctx context.Context, c *app.RequestContext) { - ctx.String(consts.StatusOK, "ping") + c.String(consts.StatusOK, "ping") }) // Sample Code 2: @@ -128,7 +128,7 @@ func RegisterGroupRouteWithMiddleware(h *server.Hertz) { example2 := h.Group("/example2") example2.Use(basic_auth.BasicAuth(map[string]string{"test": "test"})) example2.GET("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.String(consts.StatusOK, "ping") + c.String(consts.StatusOK, "ping") }) } @@ -158,14 +158,14 @@ func RegisterParaRoute(h *server.Hertz) { // RegisterAnonFunOrDecRoute Use anonymous function or decorator to register routes func RegisterAnonFunOrDecRoute(h *server.Hertz) { h.AnyEX("/ping", func(ctx context.Context, c *app.RequestContext) { - ctx.String(consts.StatusOK, app.GetHandlerName(ctx.Handler())) + c.String(consts.StatusOK, app.GetHandlerName(c.Handler())) }, "ping_handler") } // RegisterGetRoutesInfo Get route info func RegisterGetRoutesInfo(h *server.Hertz) { h.GET("/getRoutesInfo", func(ctx context.Context, c *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) + 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 5f90aa8f..6f499b97 100644 --- a/sentinel/hertz/server/main.go +++ b/sentinel/hertz/server/main.go @@ -60,7 +60,7 @@ func main() { // customize block fallback if required // abort with status 429 by default hertzSentinel.WithServerBlockFallback(func(ctx context.Context, c *app.RequestContext) { - ctx.AbortWithStatusJSON(400, utils.H{ + c.AbortWithStatusJSON(400, utils.H{ "err": "too many request; the quota used up", "code": 10222, }) diff --git a/tracer/hertz/server/main.go b/tracer/hertz/server/main.go index e7aa6c44..351fd770 100644 --- a/tracer/hertz/server/main.go +++ b/tracer/hertz/server/main.go @@ -89,7 +89,7 @@ func main() { Name string `query:"name"` } var hertzReq PingReq - err := ctx.BindAndValidate(&hertzReq) + err := c.BindAndValidate(&hertzReq) if err != nil { hlog.Errorf(err.Error()) return @@ -100,7 +100,7 @@ func main() { if err != nil { hlog.Errorf(err.Error()) } - ctx.JSON(consts.StatusOK, resp) + c.JSON(consts.StatusOK, resp) }) h.Spin() From 9425b2257be96a93295753b247ed12af18b76649 Mon Sep 17 00:00:00 2001 From: Skyenought Date: Mon, 29 Jul 2024 14:59:23 +0800 Subject: [PATCH 3/5] fix --- opentelemetry/hertz/server/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry/hertz/server/main.go b/opentelemetry/hertz/server/main.go index 5e753b50..c40e4311 100644 --- a/opentelemetry/hertz/server/main.go +++ b/opentelemetry/hertz/server/main.go @@ -67,7 +67,7 @@ func main() { 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()) } From c93eef4633a252cc38cc7270123e5821395f0b0b Mon Sep 17 00:00:00 2001 From: Skyenought Date: Mon, 29 Jul 2024 15:03:45 +0800 Subject: [PATCH 4/5] fix --- opentelemetry/README.md | 2 +- tracer/hertz/server/main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/opentelemetry/README.md b/opentelemetry/README.md index bb96c705..3d4b06b7 100644 --- a/opentelemetry/README.md +++ b/opentelemetry/README.md @@ -110,7 +110,7 @@ func init() { ```go 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()) } diff --git a/tracer/hertz/server/main.go b/tracer/hertz/server/main.go index 351fd770..0ed00dc2 100644 --- a/tracer/hertz/server/main.go +++ b/tracer/hertz/server/main.go @@ -96,7 +96,7 @@ func main() { } 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()) } From cd9b96d3533889419d10851290cf05631299b001 Mon Sep 17 00:00:00 2001 From: Skyenought Date: Mon, 29 Jul 2024 16:05:48 +0800 Subject: [PATCH 5/5] fix --- hz_kitex_demo/hertz-server/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hz_kitex_demo/hertz-server/main.go b/hz_kitex_demo/hertz-server/main.go index 99e3ade5..b95a03fb 100755 --- a/hz_kitex_demo/hertz-server/main.go +++ b/hz_kitex_demo/hertz-server/main.go @@ -27,7 +27,7 @@ func main() { // 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(ctx context.Context, c *app.RequestContext) { fmt.Println("pre-handler") - c.Next(c) + c.Next(ctx) fmt.Println("post-handler") })