Skip to content

Commit

Permalink
style: unified app.HandleFunc style
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Jul 29, 2024
1 parent 231bdf8 commit 3ffc942
Show file tree
Hide file tree
Showing 34 changed files with 54 additions and 54 deletions.
2 changes: 1 addition & 1 deletion bizdemo/tiktok_demo/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion file/html-fs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion file/html/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion hex/hex_trans_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
})

Expand Down
2 changes: 1 addition & 1 deletion hz/template/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions hz/template/template/layout.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
})
Expand Down Expand Up @@ -561,4 +561,4 @@ layouts:
CURDIR=$(cd $(dirname $0); pwd)
BinaryName={{.ServiceName}}
echo "$CURDIR/bin/${BinaryName}"
exec $CURDIR/bin/${BinaryName}
exec $CURDIR/bin/${BinaryName}
2 changes: 1 addition & 1 deletion hz_kitex_demo/hertz-server/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions middleware/csrf/custom_errorfunc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

Expand Down
4 changes: 2 additions & 2 deletions middleware/csrf/custom_extractor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

Expand Down
4 changes: 2 additions & 2 deletions middleware/csrf/custom_ignoremethods/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions middleware/csrf/custom_keylookup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

Expand Down
2 changes: 1 addition & 1 deletion middleware/csrf/custom_next/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions middleware/csrf/custom_secret/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

Expand Down
4 changes: 2 additions & 2 deletions middleware/csrf/default/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

Expand Down
2 changes: 1 addition & 1 deletion middleware/loadbalance/round_robin/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion middleware/pprof/custom_prefix/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
})

Expand Down
2 changes: 1 addition & 1 deletion middleware/pprof/custom_router_group/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
})

Expand Down
2 changes: 1 addition & 1 deletion middleware/pprof/default/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
})

Expand Down
6 changes: 3 additions & 3 deletions monitoring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand Down Expand Up @@ -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/)
For more information about hertz monitoring, please click [monitoring](https://www.cloudwego.io/zh/docs/hertz/tutorials/framework-exten/monitor/)
4 changes: 2 additions & 2 deletions monitoring/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand Down
4 changes: 2 additions & 2 deletions multiple_service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ 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()
}

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()
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry/hertz/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion protocol/http1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
})

Expand Down
4 changes: 2 additions & 2 deletions protocol/tls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

Expand Down
4 changes: 2 additions & 2 deletions render/html/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down
2 changes: 1 addition & 1 deletion reverseproxy/customize_error/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!!",
})
Expand Down
2 changes: 1 addition & 1 deletion reverseproxy/discovery/registry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion reverseproxy/standard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion reverseproxy/tls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions reverseproxy/use_middleware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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",
})
Expand Down
Loading

0 comments on commit 3ffc942

Please sign in to comment.