Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: unified app.HandleFunc style #153

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bizdemo/tiktok_demo/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 file/html-fs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions file/html/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
4 changes: 2 additions & 2 deletions file/staticFile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions hex/hex_trans_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions hz/template/main.go

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

6 changes: 3 additions & 3 deletions hz/template/template/layout.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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}
4 changes: 2 additions & 2 deletions hz_kitex_demo/hertz-server/main.go

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

20 changes: 10 additions & 10 deletions middleware/csrf/custom_errorfunc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions middleware/csrf/custom_extractor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions middleware/csrf/custom_ignoremethods/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
8 changes: 4 additions & 4 deletions middleware/csrf/custom_keylookup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions middleware/csrf/custom_next/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
8 changes: 4 additions & 4 deletions middleware/csrf/custom_secret/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.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()
Expand Down
8 changes: 4 additions & 4 deletions middleware/csrf/default/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions middleware/loadbalance/round_robin/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions middleware/pprof/custom_prefix/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions middleware/pprof/custom_router_group/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions middleware/pprof/default/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions monitoring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
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/)
8 changes: 4 additions & 4 deletions monitoring/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions multiple_service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
```

Expand Down
Loading
Loading