Skip to content

Commit

Permalink
Merge branch 'main' into feat/struct_reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou authored Dec 8, 2023
2 parents a0202b9 + 9ce73f4 commit 694b075
Show file tree
Hide file tree
Showing 43 changed files with 1,362 additions and 1,354 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: 1.21

- uses: actions/cache@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: 1.21

- uses: actions/cache@v3
with:
Expand All @@ -23,7 +23,7 @@ jobs:
- name: Lint
run: |
go vet -stdmethods=false $(go list ./...)
go install mvdan.cc/gofumpt@v0.2.0
go install mvdan.cc/gofumpt@v0.5.0
test -z "$(gofumpt -l -extra .)"
- name: Unit Test
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ You can enter the example for information about "How to run"
- [hello:](hello) Example of launching a hertz "hello world" application
- [config:](config) Example of configuring hertz server
- [protocol:](protocol) Example of using http1, tls and other protocols of hertz
- [HTTP1](https://github.com/cloudwego/hertz-examples/tree/main/protocol/http1) Example of hertz using HTTP1 protocol
- [TLS](https://github.com/cloudwego/hertz-examples/tree/main/protocol/tls) Example of hertz using TLS protocol
- [HTTP2](https://github.com/hertz-contrib/http2/tree/main/examples) Example of hertz using HTTP2 protocol
- [HTTP3](https://github.com/hertz-contrib/http3/tree/main/examples/quic-go) Example of hertz using HTTP3 protocol
- [Websocket](https://github.com/hertz-contrib/websocket/tree/main/examples) Example of hertz using Websocket protocol
- [SSE](https://github.com/hertz-contrib/sse/tree/main/examples) Example of hertz using SSE protocol
- [middleware:](middleware) Example of using middleware of hertz
- [basicauth:](middleware/basicauth) Example of using BasicAuth middleware
- [cors:](middleware/CORS) Example of using CORS middleware
Expand All @@ -30,6 +36,19 @@ You can enter the example for information about "How to run"
- [requestid:](middleware/requestid) Example of using RequestID middleware
- [gzip:](middleware/gzip) Example of using Gzip middleware
- [loadbalance:](middleware/loadbalance) Example of using Loadbalance middleware
- [Recovery](https://www.cloudwego.io/docs/hertz/tutorials/basic-feature/middleware/recovery/) Example of using Recovery middleware
- [jwt](https://github.com/hertz-contrib/jwt/tree/main/example/basic) Example of using jwt middleware
- [i18n](https://github.com/hertz-contrib/i18n/tree/main/example) Example of using i18n middleware
- [session](https://github.com/hertz-contrib/sessions/tree/main/_example) Example of using session middleware
- [KeyAuth](https://github.com/hertz-contrib/keyauth/tree/main/example) Example of using KeyAuth middleware
- [Swagger](https://github.com/swaggo/gin-swagger/tree/master/example) Example of using Swagger middleware
- [access log](https://github.com/hertz-contrib/logger/tree/main/accesslog/example) Example of using access log middleware
- [Secure](https://github.com/hertz-contrib/secure/tree/main/example/custom) Example of using Secure middleware
- [Sentry](https://github.com/hertz-contrib/hertzsentry) Example of using Sentry middleware
- [Casbin](https://github.com/hertz-contrib/casbin/tree/main/example) Example of using Casbin middleware
- [ETag](https://github.com/hertz-contrib/etag/tree/main/example) Example of using ETag middleware
- [Cache](https://github.com/hertz-contrib/cache/tree/main/example) Example of using Cache middleware
- [Paseto](https://github.com/hertz-contrib/paseto/tree/main/example) Example of using Paseto middleware
- [binding:](binding) Example of parameter binding and validation
- [parameters:](parameter) Example of getting query, form, cookie
- [file:](file) Examples of file upload, file download, and static file services
Expand All @@ -48,6 +67,8 @@ You can enter the example for information about "How to run"
- [gzip:](middleware/gzip) Example of using gzip middleware in hertz server
- [hlog:](hlog) Example of using hlog and its log extension
- [trailer:](trailer) Example of read/write trailers for hertz server
- [graphql-go:](graphql-go) Example of using graphql in hertz server


## Client

Expand Down
21 changes: 21 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
- [hello:](hello) 启动对于 hertz 来说相当于 "hello world" 的示例
- [config:](config) 配置 Hertz server 的示例
- [protocol:](protocol) 使用 http1, TLS 以及其他协议的示例
- [HTTP1](https://github.com/cloudwego/hertz-examples/tree/main/protocol/http1) hertz 使用 HTTP1 协议的示例
- [TLS](https://github.com/cloudwego/hertz-examples/tree/main/protocol/tls) hertz 使用 TLS 协议的示例
- [HTTP2](https://github.com/hertz-contrib/http2/tree/main/examples) hertz 使用 HTTP2 协议的示例
- [HTTP3](https://github.com/hertz-contrib/http3/tree/main/examples/quic-go) hertz 使用 HTTP3 协议的示例
- [Websocket](https://github.com/hertz-contrib/websocket/tree/main/examples) hertz 使用 Websocket 协议的示例
- [SSE](https://github.com/hertz-contrib/sse/tree/main/examples) hertz 使用 SSE 协议的示例
- [middleware:](middleware) 使用 hertz 中间件的示例
- [basicauth:](middleware/basicauth) 使用 BasicAuth 中间件的示例
- [cors:](middleware/CORS) 使用 CORS 中间件的示例
Expand All @@ -30,6 +36,19 @@
- [requestid:](middleware/requestid) 使用 RequestID 中间件的示例
- [gzip:](middleware/gzip) 使用 Gzip 中间件的示例
- [loadbalance:](middleware/loadbalance) 使用 Loadbalance 中间件的示例
- [Recovery](https://www.cloudwego.io/zh/docs/hertz/tutorials/basic-feature/middleware/recovery/) 使用 Recovery 中间件的示例
- [jwt](https://github.com/hertz-contrib/jwt/tree/main/example/basic) 使用 jwt 中间件的示例
- [i18n](https://github.com/hertz-contrib/i18n/tree/main/example) 使用 i18n 中间件的示例
- [session](https://github.com/hertz-contrib/sessions/tree/main/_example) 使用 session 中间件的示例
- [KeyAuth](https://github.com/hertz-contrib/keyauth/tree/main/example) 使用 KeyAuth 中间件的示例
- [Swagger](https://github.com/swaggo/gin-swagger/tree/master/example) 使用 Swagger 中间件的示例
- [access log](https://github.com/hertz-contrib/logger/tree/main/accesslog/example) 使用 access log 中间件的示例
- [Secure](https://github.com/hertz-contrib/secure/tree/main/example/custom) 使用 Secure 中间件的示例
- [Sentry](https://github.com/hertz-contrib/hertzsentry) 使用 Sentry 中间件的示例
- [Casbin](https://github.com/hertz-contrib/casbin/tree/main/example) 使用 Casbin 中间件的示例
- [ETag](https://github.com/hertz-contrib/etag/tree/main/example) 使用 ETag 中间件的示例
- [Cache](https://github.com/hertz-contrib/cache/tree/main/example) 使用 Cache 中间件的示例
- [Paseto](https://github.com/hertz-contrib/paseto/tree/main/example) 使用 Paseto 中间件的示例
- [binding:](binding) 绑定参数和验证参数的示例
- [parameters:](parameter) 获取 query, form, cookie 等类型参数的示例
- [file:](file) 关于如何上传,下载文件和搭建静态文件服务的示例
Expand All @@ -45,6 +64,8 @@
- [sentinel:](sentinel) sentinel-golang 结合 hertz 使用的示例
- [reverseproxy:](reverseproxy) 在 hertz server 中使用反向代理的示例
- [hlog:](hlog) 使用 hlog 以及其日志拓展的示例
- [graphql-go:](graphql-go) 在 hertz server 中使用 graphql 的示例


## Client

Expand Down
22 changes: 5 additions & 17 deletions binding/custom_error/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,7 @@ func (e *ValidateError) Error() string {
return e.ErrType + ": expr_path=" + e.FailField + ", cause=invalid"
}

func init() {
CustomBindErrFunc := func(failField, msg string) error {
err := BindError{
ErrType: "bindErr",
FailField: "[bindFailField]: " + failField,
Msg: "[bindErrMsg]: " + msg,
}

return &err
}

func main() {
CustomValidateErrFunc := func(failField, msg string) error {
err := ValidateError{
ErrType: "validateErr",
Expand All @@ -73,12 +63,10 @@ func init() {

return &err
}

binding.SetErrorFactory(CustomBindErrFunc, CustomValidateErrFunc)
}

func main() {
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
validateConfig := binding.NewValidateConfig()
validateConfig.SetValidatorErrorFactory(CustomValidateErrFunc)
h := server.Default(server.WithHostPorts("127.0.0.1:8080"),
server.WithValidateConfig(validateConfig))

h.GET("bindErr", func(ctx context.Context, c *app.RequestContext) {
type TestBind struct {
Expand Down
17 changes: 9 additions & 8 deletions binding/custom_type_resolve/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"reflect"
"time"

"github.com/cloudwego/hertz/pkg/route/param"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/app/server"
Expand All @@ -39,20 +41,19 @@ type TestBind struct {
A Nested `query:"a"`
}

func init() {
binding.MustRegTypeUnmarshal(reflect.TypeOf(Nested{}), func(v string, emptyAsZero bool) (reflect.Value, error) {
if v == "" && emptyAsZero {
func main() {
bindConfig := binding.NewBindConfig()
bindConfig.MustRegTypeUnmarshal(reflect.TypeOf(Nested{}), func(req *protocol.Request, params param.Params, text string) (reflect.Value, error) {
if text == "" {
return reflect.ValueOf(Nested{}), nil
}
val := Nested{
B: v[:5],
C: v[5:],
B: text[:5],
C: text[5:],
}
// 此外,也可以利用 req, params 来获取其他参数进行参数绑定
return reflect.ValueOf(val), nil
})
}

func main() {
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))

h.GET("customType", func(ctx context.Context, c *app.RequestContext) {
Expand Down
8 changes: 3 additions & 5 deletions binding/custom_validate_func/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ type ValidateStruct struct {
A string `query:"a" vd:"test($)"`
}

func init() {
binding.MustRegValidateFunc("test", func(args ...interface{}) error {
func main() {
validateConfig := binding.NewValidateConfig()
validateConfig.MustRegValidateFunc("test", func(args ...interface{}) error {
if len(args) != 1 {
return fmt.Errorf("the args must be one")
}
Expand All @@ -44,9 +45,6 @@ func init() {
}
return nil
})
}

func main() {
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))

h.GET("customValidate", func(ctx context.Context, c *app.RequestContext) {
Expand Down
8 changes: 3 additions & 5 deletions binding/loose_zero/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ import (
"github.com/cloudwego/hertz/pkg/protocol/consts"
)

func init() {
binding.SetLooseZeroMode(true)
}

func main() {
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
bindConfig := binding.NewBindConfig()
bindConfig.LooseZeroMode = true
h := server.Default(server.WithHostPorts("127.0.0.1:8080"), server.WithBindConfig(bindConfig))

h.GET("looseZero", func(ctx context.Context, c *app.RequestContext) {
type Loose struct {
Expand Down
10 changes: 5 additions & 5 deletions bizdemo/hertz_gorm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ go 1.16
replace github.com/apache/thrift => github.com/apache/thrift v0.13.0

require (
github.com/apache/thrift v0.13.0
github.com/cloudwego/hertz v0.6.6
github.com/hertz-contrib/gzip v0.0.1
gorm.io/driver/mysql v1.4.4
gorm.io/gorm v1.24.6
github.com/apache/thrift v0.16.0
github.com/cloudwego/hertz v0.7.2
github.com/hertz-contrib/gzip v0.0.3
gorm.io/driver/mysql v1.5.2
gorm.io/gorm v1.25.5
)
Loading

0 comments on commit 694b075

Please sign in to comment.