diff --git a/auth/casbin/README.md b/auth/casbin/README.md index bf98eba..dbc0402 100644 --- a/auth/casbin/README.md +++ b/auth/casbin/README.md @@ -7,7 +7,7 @@ Prerequisites: Download the module: ```shell -go get -u github.com/zeromicro/zero-contrib/auth/casbin +go get -u github.com/wqyjh/zero-contrib/auth/casbin ``` For example: @@ -18,7 +18,7 @@ package main import ( stdcasbin "github.com/casbin/casbin/v2" "github.com/zeromicro/go-zero/rest" - "github.com/zeromicro/zero-contrib/auth/casbin" + "github.com/wqyjh/zero-contrib/auth/casbin" ) func main() { diff --git a/auth/casbin/go.mod b/auth/casbin/go.mod index 48bca16..6a07353 100644 --- a/auth/casbin/go.mod +++ b/auth/casbin/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/auth/casbin +module github.com/wqyjh/zero-contrib/auth/casbin go 1.16 diff --git a/handler/README.md b/handler/README.md index 5984eb0..ac106ce 100644 --- a/handler/README.md +++ b/handler/README.md @@ -11,7 +11,7 @@ ETag: ETag Handler to support [ETag](https://en.wikipedia.org/wiki/HTTP_ETag) bo ### Download the module: ```shell -go get -u github.com/zeromicro/zero-contrib/handler +go get -u github.com/wqyjh/zero-contrib/handler ``` ### Example: @@ -23,7 +23,7 @@ import ( ... "github.com/zeromicro/go-zero/rest" - "github.com/zeromicro/zero-contrib/handler" + "github.com/wqyjh/zero-contrib/handler" ) func main() { diff --git a/handler/go.mod b/handler/go.mod index 3112cad..e18de2e 100644 --- a/handler/go.mod +++ b/handler/go.mod @@ -1,5 +1,34 @@ -module github.com/zeromicro/zero-contrib/handler +module github.com/wqyjh/zero-contrib/handler -go 1.16 +go 1.21 -require github.com/stretchr/testify v1.8.4 +toolchain go1.22.1 + +require ( + github.com/stretchr/testify v1.9.0 + github.com/zeromicro/go-zero v1.6.5 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fatih/color v1.16.0 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/prometheus/client_golang v1.18.0 // indirect + github.com/prometheus/client_model v0.5.0 // indirect + github.com/prometheus/common v0.45.0 // indirect + github.com/prometheus/procfs v0.12.0 // indirect + github.com/spaolacci/murmur3 v1.1.0 // indirect + go.opentelemetry.io/otel v1.22.0 // indirect + go.opentelemetry.io/otel/sdk v1.21.0 // indirect + go.opentelemetry.io/otel/trace v1.22.0 // indirect + go.uber.org/automaxprocs v1.5.3 // indirect + golang.org/x/sys v0.20.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/handler/go.sum b/handler/go.sum index 479781e..0be8543 100644 --- a/handler/go.sum +++ b/handler/go.sum @@ -1,17 +1,67 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= +github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g= +github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U= +github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= +github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= +github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= +github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= +github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM= +github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= +github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= +github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= +github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/zeromicro/go-zero v1.6.5 h1:JgsBa25/knnEL7+KQksbwktudIkNQvaAin0nisVgnSA= +github.com/zeromicro/go-zero v1.6.5/go.mod h1:XjbssEVEzFKueAh0Fie5kNf+cRqFlQQk46fY9WgEGaM= +go.opentelemetry.io/otel v1.22.0 h1:xS7Ku+7yTFvDfDraDIJVpw7XPyuHlB9MCiqqX5mcJ6Y= +go.opentelemetry.io/otel v1.22.0/go.mod h1:eoV4iAi3Ea8LkAEI9+GFT44O6T/D0GWAVFyZVCC6pMI= +go.opentelemetry.io/otel/metric v1.22.0 h1:lypMQnGyJYeuYPhOM/bgjbFM6WE44W1/T45er4d8Hhg= +go.opentelemetry.io/otel/metric v1.22.0/go.mod h1:evJGjVpZv0mQ5QBRJoBF64yMuOf4xCWdXjK8pzFvliY= +go.opentelemetry.io/otel/sdk v1.21.0 h1:FTt8qirL1EysG6sTQRZ5TokkU8d0ugCj8htOgThZXQ8= +go.opentelemetry.io/otel/sdk v1.21.0/go.mod h1:Nna6Yv7PWTdgJHVRD9hIYywQBRx7pbox6nwBnZIxl/E= +go.opentelemetry.io/otel/trace v1.22.0 h1:Hg6pPujv0XG9QaVbGOBVHunyuLcCC3jN7WEhPx83XD0= +go.opentelemetry.io/otel/trace v1.22.0/go.mod h1:RbbHXVqKES9QhzZq/fE5UnOSILqRt40a21sPw2He1xo= +go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8= +go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/handler/prometheus.go b/handler/prometheus.go new file mode 100644 index 0000000..08b952e --- /dev/null +++ b/handler/prometheus.go @@ -0,0 +1,85 @@ +package handler + +import ( + "net/http" + "strconv" + + "github.com/wqyjh/zero-contrib/handler/response" + "github.com/zeromicro/go-zero/core/metric" + "github.com/zeromicro/go-zero/core/timex" +) + +// PrometheusHandler returns a middleware that reports stats to prometheus. +func PrometheusHandler(opts ...PrometheusOption) func(http.Handler) http.Handler { + options := prometheusOptions{ + namespace: "http_server", + subsystem: "requests", + buckets: []float64{5, 10, 25, 50, 100, 250, 500, 750, 1000}, + } + for _, o := range opts { + o(&options) + } + + metricServerReqDur := metric.NewHistogramVec(&metric.HistogramVecOpts{ + Namespace: options.namespace, + Subsystem: options.subsystem, + Name: "duration_ms", + Help: "http server requests duration(ms).", + Labels: []string{"path", "method", "code"}, + Buckets: options.buckets, + }) + + metricServerReqCodeTotal := metric.NewCounterVec(&metric.CounterVecOpts{ + Namespace: options.namespace, + Subsystem: options.subsystem, + Name: "code_total", + Help: "http server requests error count.", + Labels: []string{"path", "method", "code"}, + }) + + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + startTime := timex.Now() + cw := response.NewWithCodeResponseWriter(w) + defer func() { + code := strconv.Itoa(cw.Code) + path := r.URL.Path + method := r.Method + metricServerReqDur.Observe(timex.Since(startTime).Milliseconds(), path, method, code) + metricServerReqCodeTotal.Inc(path, method, code) + }() + + next.ServeHTTP(cw, r) + }) + } +} + +type prometheusOptions struct { + namespace string + subsystem string + buckets []float64 +} + +// PrometheusOption allows for managing prometheus options. +type PrometheusOption func(*prometheusOptions) + +// WithBuckets sets the buckets for the prometheus metrics. +func WithBuckets(buckets []float64) PrometheusOption { + return func(o *prometheusOptions) { + o.buckets = buckets + } +} + +// WithNamespace sets the namespace for the prometheus metrics. +func WithNamespace(namespace string) PrometheusOption { + return func(o *prometheusOptions) { + o.namespace = namespace + } +} + +// WithSubsystem sets the subsystem for the prometheus metrics. +func WithSubsystem(subsystem string) PrometheusOption { + return func(o *prometheusOptions) { + o.subsystem = subsystem + } +} diff --git a/handler/response/withcoderesponsewriter.go b/handler/response/withcoderesponsewriter.go new file mode 100644 index 0000000..2a49c79 --- /dev/null +++ b/handler/response/withcoderesponsewriter.go @@ -0,0 +1,61 @@ +package response + +import ( + "bufio" + "errors" + "net" + "net/http" +) + +// A WithCodeResponseWriter is a helper to delay sealing a http.ResponseWriter on writing code. +type WithCodeResponseWriter struct { + Writer http.ResponseWriter + Code int +} + +// NewWithCodeResponseWriter returns a WithCodeResponseWriter. +// If writer is already a WithCodeResponseWriter, it returns writer directly. +func NewWithCodeResponseWriter(writer http.ResponseWriter) *WithCodeResponseWriter { + switch w := writer.(type) { + case *WithCodeResponseWriter: + return w + default: + return &WithCodeResponseWriter{ + Writer: writer, + Code: http.StatusOK, + } + } +} + +// Flush flushes the response writer. +func (w *WithCodeResponseWriter) Flush() { + if flusher, ok := w.Writer.(http.Flusher); ok { + flusher.Flush() + } +} + +// Header returns the http header. +func (w *WithCodeResponseWriter) Header() http.Header { + return w.Writer.Header() +} + +// Hijack implements the http.Hijacker interface. +// This expands the Response to fulfill http.Hijacker if the underlying http.ResponseWriter supports it. +func (w *WithCodeResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { + if hijacked, ok := w.Writer.(http.Hijacker); ok { + return hijacked.Hijack() + } + + return nil, nil, errors.New("server doesn't support hijacking") +} + +// Write writes bytes into w. +func (w *WithCodeResponseWriter) Write(bytes []byte) (int, error) { + return w.Writer.Write(bytes) +} + +// WriteHeader writes code into w, and not sealing the writer. +func (w *WithCodeResponseWriter) WriteHeader(code int) { + w.Writer.WriteHeader(code) + w.Code = code +} diff --git a/logx/logrusx/go.mod b/logx/logrusx/go.mod index 9cefb4d..6d79340 100644 --- a/logx/logrusx/go.mod +++ b/logx/logrusx/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/logx/logrusx +module github.com/wqyjh/zero-contrib/logx/logrusx go 1.16 diff --git a/logx/logrusx/readme.md b/logx/logrusx/readme.md index 515862a..9ec0a0b 100644 --- a/logx/logrusx/readme.md +++ b/logx/logrusx/readme.md @@ -11,7 +11,7 @@ go get -u github.com/zeromicro/go-zero Download the module: ```console -go get -u github.com/zeromicro/zero-contrib/logx/logrusx +go get -u github.com/wqyjh/zero-contrib/logx/logrusx ``` For example: @@ -24,7 +24,7 @@ import ( "time" "github.com/zeromicro/go-zero/core/logx" - "github.com/zeromicro/zero-contrib/logx/logrusx" + "github.com/wqyjh/zero-contrib/logx/logrusx" ) func main() { diff --git a/logx/zapx/go.mod b/logx/zapx/go.mod index 8108a47..1191f39 100644 --- a/logx/zapx/go.mod +++ b/logx/zapx/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/logx/zapx +module github.com/wqyjh/zero-contrib/logx/zapx go 1.16 diff --git a/logx/zapx/readme.md b/logx/zapx/readme.md index 53720f3..04d0d7e 100644 --- a/logx/zapx/readme.md +++ b/logx/zapx/readme.md @@ -11,7 +11,7 @@ go get -u github.com/zeromicro/go-zero Download the module: ```console -go get -u github.com/zeromicro/zero-contrib/logx/zapx +go get -u github.com/wqyjh/zero-contrib/logx/zapx ``` For example: @@ -24,7 +24,7 @@ import ( "time" "github.com/zeromicro/go-zero/core/logx" - "github.com/zeromicro/zero-contrib/logx/zapx" + "github.com/wqyjh/zero-contrib/logx/zapx" ) func main() { diff --git a/logx/zapx/zap.go b/logx/zapx/zap.go index e65e5b3..94c7be1 100644 --- a/logx/zapx/zap.go +++ b/logx/zapx/zap.go @@ -25,6 +25,12 @@ func NewZapWriter(opts ...zap.Option) (logx.Writer, error) { }, nil } +func NewFromZapLogger(logger *zap.Logger) logx.Writer { + return &ZapWriter{ + logger: logger, + } +} + func (w *ZapWriter) Alert(v interface{}) { w.logger.Error(fmt.Sprint(v)) } diff --git a/logx/zerologx/go.mod b/logx/zerologx/go.mod index f71085a..222fb77 100644 --- a/logx/zerologx/go.mod +++ b/logx/zerologx/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/logx/zerologx +module github.com/wqyjh/zero-contrib/logx/zerologx go 1.16 diff --git a/logx/zerologx/readme.md b/logx/zerologx/readme.md index 4fd732e..c1a32a2 100644 --- a/logx/zerologx/readme.md +++ b/logx/zerologx/readme.md @@ -11,7 +11,7 @@ go get -u github.com/zeromicro/go-zero Download the module: ```console -go get -u github.com/zeromicro/zero-contrib/logx/zerologx +go get -u github.com/wqyjh/zero-contrib/logx/zerologx ``` For example: @@ -23,7 +23,7 @@ import ( "context" "github.com/rs/zerolog" "github.com/zeromicro/go-zero/core/logx" - "github.com/zeromicro/zero-contrib/logx/zerologx" + "github.com/wqyjh/zero-contrib/logx/zerologx" "os" "time" ) diff --git a/rest/registry/etcd/README.md b/rest/registry/etcd/README.md index 9ef39ba..55078c4 100644 --- a/rest/registry/etcd/README.md +++ b/rest/registry/etcd/README.md @@ -5,7 +5,7 @@ Prerequisites: Download the module: ```console -go get -u github.com/zeromicro/zero-contrib/rest/registry/etcd +go get -u github.com/wqyjh/zero-contrib/rest/registry/etcd ``` For example: @@ -44,7 +44,7 @@ import ( "github.com/zeromicro/go-zero/conf" "github.com/zeromicro/go-zero/rest" - "github.com/zeromicro/zero-contrib/rest/registry/etcd" + "github.com/wqyjh/zero-contrib/rest/registry/etcd" ) var configFile = flag.String("f", "etc/user-api.yaml", "the config file") diff --git a/rest/registry/etcd/go.mod b/rest/registry/etcd/go.mod index 40bf29e..e97e6fe 100644 --- a/rest/registry/etcd/go.mod +++ b/rest/registry/etcd/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/rest/registry/etcd +module github.com/wqyjh/zero-contrib/rest/registry/etcd go 1.16 diff --git a/router/chi/README.md b/router/chi/README.md index bac6e08..5299ac1 100644 --- a/router/chi/README.md +++ b/router/chi/README.md @@ -5,7 +5,7 @@ Prerequesites: Download the module: ```console -go get -u github.com/zeromicro/zero-contrib/router/chi +go get -u github.com/wqyjh/zero-contrib/router/chi ``` For example: @@ -18,7 +18,7 @@ import ( "github.com/zeromicro/go-zero/core/service" "github.com/zeromicro/go-zero/rest" "github.com/zeromicro/go-zero/rest/httpx" - "github.com/zeromicro/zero-contrib/router/chi" + "github.com/wqyjh/zero-contrib/router/chi" "net/http" "strings" ) diff --git a/router/chi/go.mod b/router/chi/go.mod index efd52bf..448f67c 100644 --- a/router/chi/go.mod +++ b/router/chi/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/router/chi +module github.com/wqyjh/zero-contrib/router/chi go 1.15 diff --git a/router/gin/README.md b/router/gin/README.md index 68fdd93..947710d 100644 --- a/router/gin/README.md +++ b/router/gin/README.md @@ -11,7 +11,7 @@ go get -u github.com/zeromicro/go-zero Download the module: ```console -go get -u github.com/zeromicro/zero-contrib/router/gin +go get -u github.com/wqyjh/zero-contrib/router/gin ``` For example: @@ -27,8 +27,8 @@ import ( "github.com/zeromicro/go-zero/core/service" "github.com/zeromicro/go-zero/rest" "github.com/zeromicro/go-zero/rest/httpx" - "github.com/zeromicro/zero-contrib/router/gin" - stdgin "github.com/gin-gonic/gin" + zerogin "github.com/wqyjh/zero-contrib/router/gin" + "github.com/gin-gonic/gin" ) type CommonPathID struct { @@ -46,11 +46,12 @@ func (c *CommonPathID) String() string { } func init() { - stdgin.SetMode(stdgin.ReleaseMode) + gin.SetMode(gin.ReleaseMode) } func main() { - r := gin.NewRouter() + server := gin.New() + r := zerogin.NewRouter(r) engine := rest.MustNewServer(rest.RestConf{ ServiceConf: service.ServiceConf{ Log: logx.LogConf{ diff --git a/router/gin/go.mod b/router/gin/go.mod index 70e2e75..76cb857 100644 --- a/router/gin/go.mod +++ b/router/gin/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/router/gin +module github.com/wqyjh/zero-contrib/router/gin go 1.15 diff --git a/router/gin/middleware.go b/router/gin/middleware.go new file mode 100644 index 0000000..9e203ce --- /dev/null +++ b/router/gin/middleware.go @@ -0,0 +1,25 @@ +package gin + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +type ( + Middleware func(next http.Handler) http.Handler +) + +func HandlerFunc(ctx *gin.Context) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + ctx.Request = r + ctx.Next() + } +} + +func ZeroMiddleware(middleware Middleware) func(ctx *gin.Context) { + return func(ctx *gin.Context) { + next := HandlerFunc(ctx) + middleware(next).ServeHTTP(ctx.Writer, ctx.Request) + } +} diff --git a/router/gin/router.go b/router/gin/router.go index 92f05b1..7ab5d27 100644 --- a/router/gin/router.go +++ b/router/gin/router.go @@ -14,8 +14,7 @@ type ginRouter struct { } // NewRouter returns a gin.Router. -func NewRouter(opts ...Option) httpx.Router { - g := gin.New() +func NewRouter(g *gin.Engine, opts ...Option) httpx.Router { cfg := config{ redirectTrailingSlash: true, redirectFixedPath: false, @@ -58,6 +57,7 @@ func (pr *ginRouter) SetNotFoundHandler(handler http.Handler) { } func (pr *ginRouter) SetNotAllowedHandler(handler http.Handler) { + pr.g.HandleMethodNotAllowed = true pr.g.NoMethod(gin.WrapH(handler)) } diff --git a/router/gin/router_test.go b/router/gin/router_test.go index e6d42f7..e6593f9 100644 --- a/router/gin/router_test.go +++ b/router/gin/router_test.go @@ -8,6 +8,7 @@ import ( "net/http/httptest" "testing" + "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" "github.com/zeromicro/go-zero/rest/httpx" "github.com/zeromicro/go-zero/rest/pathvar" @@ -29,9 +30,9 @@ func (m *mockedResponseWriter) WriteHeader(code int) { m.code = code } -func TestChiRouterNotFound(t *testing.T) { +func TestGinRouterNotFound(t *testing.T) { var notFound bool - router := NewRouter() + router := NewRouter(gin.New()) router.SetNotFoundHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { notFound = true })) @@ -44,9 +45,9 @@ func TestChiRouterNotFound(t *testing.T) { assert.True(t, notFound) } -func TestChiRouterNotAllowed(t *testing.T) { +func TestGinRouterNotAllowed(t *testing.T) { var notAllowed bool - router := NewRouter() + router := NewRouter(gin.New()) router.SetNotAllowedHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { notAllowed = true })) @@ -76,7 +77,7 @@ func TestGinRouter(t *testing.T) { for _, test := range tests { t.Run(test.method+":"+test.path, func(t *testing.T) { routed := false - router := NewRouter() + router := NewRouter(gin.New()) err := router.Handle(test.method, "/test/:name/:last_name/*wild", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { routed = true @@ -115,7 +116,7 @@ func TestParseJsonPost(t *testing.T) { assert.Nil(t, err) r.Header.Set(httpx.ContentType, httpx.JsonContentType) - router := NewRouter() + router := NewRouter(gin.New()) err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func( w http.ResponseWriter, r *http.Request) { v := struct { @@ -147,7 +148,7 @@ func TestParseJsonPostWithIntSlice(t *testing.T) { assert.Nil(t, err) r.Header.Set(httpx.ContentType, httpx.JsonContentType) - router := NewRouter() + router := NewRouter(gin.New()) err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func( w http.ResponseWriter, r *http.Request) { v := struct { @@ -175,7 +176,7 @@ func TestParseJsonPostError(t *testing.T) { assert.Nil(t, err) r.Header.Set(httpx.ContentType, httpx.JsonContentType) - router := NewRouter() + router := NewRouter(gin.New()) err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -200,7 +201,7 @@ func TestParsePath(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017", nil) assert.Nil(t, err) - router := NewRouter() + router := NewRouter(gin.New()) err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -225,7 +226,7 @@ func TestParsePathRequired(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin", nil) assert.Nil(t, err) - router := NewRouter() + router := NewRouter(gin.New()) err = router.Handle(http.MethodGet, "/:name/", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -246,7 +247,7 @@ func TestParseQuery(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil) assert.Nil(t, err) - router := NewRouter() + router := NewRouter(gin.New()) err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -271,7 +272,7 @@ func TestParseOptional(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=", nil) assert.Nil(t, err) - router := NewRouter() + router := NewRouter(gin.New()) err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -295,7 +296,7 @@ func TestParseOptional(t *testing.T) { func BenchmarkGinRouter(b *testing.B) { b.ReportAllocs() - router := NewRouter() + router := NewRouter(gin.New()) router.Handle(http.MethodGet, "/api/param/:param1/:params2/:param3/:param4/:param5", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { })) w := &mockedResponseWriter{} diff --git a/router/mux/README.md b/router/mux/README.md index 3717d87..316687d 100644 --- a/router/mux/README.md +++ b/router/mux/README.md @@ -11,7 +11,7 @@ go get -u github.com/zeromicro/go-zero Download the module: ```console -go get -u github.com/zeromicro/zero-contrib/router/mux +go get -u github.com/wqyjh/zero-contrib/router/mux ``` For example: @@ -26,7 +26,7 @@ import ( "github.com/zeromicro/go-zero/core/service" "github.com/zeromicro/go-zero/rest" "github.com/zeromicro/go-zero/rest/httpx" - "github.com/zeromicro/zero-contrib/router/mux" + "github.com/wqyjh/zero-contrib/router/mux" "net/http" ) diff --git a/router/mux/go.mod b/router/mux/go.mod index 8ea1e94..aad9bb3 100644 --- a/router/mux/go.mod +++ b/router/mux/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/router/mux +module github.com/wqyjh/zero-contrib/router/mux go 1.15 diff --git a/stores/clickhouse/go.mod b/stores/clickhouse/go.mod index 6ccf42e..15fef05 100644 --- a/stores/clickhouse/go.mod +++ b/stores/clickhouse/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/stores/clickhouse +module github.com/wqyjh/zero-contrib/stores/clickhouse go 1.16 diff --git a/zrpc/registry/consul/README.md b/zrpc/registry/consul/README.md index c120e10..1d92e0f 100644 --- a/zrpc/registry/consul/README.md +++ b/zrpc/registry/consul/README.md @@ -5,7 +5,7 @@ Prerequisites: Download the module: ```console -go get -u github.com/zeromicro/zero-contrib/zrpc/registry/consul +go get -u github.com/wqyjh/zero-contrib/zrpc/registry/consul ``` For example: @@ -38,7 +38,7 @@ type Config struct { - main.go ```go -import _ "github.com/zeromicro/zero-contrib/zrpc/registry/consul" +import _ "github.com/wqyjh/zero-contrib/zrpc/registry/consul" func main() { flag.Parse() @@ -74,7 +74,7 @@ service "check.rpc" { - main.go ```go -import _ "github.com/zeromicro/zero-contrib/zrpc/registry/consul" +import _ "github.com/wqyjh/zero-contrib/zrpc/registry/consul" ``` - etc/\*.yaml diff --git a/zrpc/registry/consul/go.mod b/zrpc/registry/consul/go.mod index d5fbeef..4dbe045 100644 --- a/zrpc/registry/consul/go.mod +++ b/zrpc/registry/consul/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/zrpc/registry/consul +module github.com/wqyjh/zero-contrib/zrpc/registry/consul go 1.15 diff --git a/zrpc/registry/consul/tests/client_test.go b/zrpc/registry/consul/tests/client_test.go index 1a5329b..8eade32 100644 --- a/zrpc/registry/consul/tests/client_test.go +++ b/zrpc/registry/consul/tests/client_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - _ "github.com/zeromicro/zero-contrib/zrpc/registry/consul" + _ "github.com/wqyjh/zero-contrib/zrpc/registry/consul" "google.golang.org/grpc" ) diff --git a/zrpc/registry/nacos/README.md b/zrpc/registry/nacos/README.md index 7df3df4..89508ba 100644 --- a/zrpc/registry/nacos/README.md +++ b/zrpc/registry/nacos/README.md @@ -5,7 +5,7 @@ Prerequisites: Download the module: ```console -go get -u github.com/zeromicro/zero-contrib/zrpc/registry/nacos +go get -u github.com/wqyjh/zero-contrib/zrpc/registry/nacos ``` For example: @@ -15,7 +15,7 @@ For example: - main.go ```go -import _ "github.com/zeromicro/zero-contrib/zrpc/registry/nacos" +import _ "github.com/wqyjh/zero-contrib/zrpc/registry/nacos" func main() { flag.Parse() @@ -53,7 +53,7 @@ func main() { - main.go ```go -import _ "github.com/zeromicro/zero-contrib/zrpc/registry/nacos" +import _ "github.com/wqyjh/zero-contrib/zrpc/registry/nacos" ``` - etc/\*.yaml diff --git a/zrpc/registry/nacos/go.mod b/zrpc/registry/nacos/go.mod index f0daccd..afda69f 100644 --- a/zrpc/registry/nacos/go.mod +++ b/zrpc/registry/nacos/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/zrpc/registry/nacos +module github.com/wqyjh/zero-contrib/zrpc/registry/nacos go 1.15 diff --git a/zrpc/registry/polaris/README.md b/zrpc/registry/polaris/README.md index bb86643..e486746 100644 --- a/zrpc/registry/polaris/README.md +++ b/zrpc/registry/polaris/README.md @@ -5,7 +5,7 @@ Prerequisites: Download the module: ```console -go get -u github.com/zeromicro/zero-contrib/zrpc/registry/polaris +go get -u github.com/wqyjh/zero-contrib/zrpc/registry/polaris ``` For example: @@ -24,7 +24,7 @@ global: - main.go ```go -import _ "github.com/zeromicro/zero-contrib/zrpc/registry/polaris" +import _ "github.com/wqyjh/zero-contrib/zrpc/registry/polaris" func main() { flag.Parse() @@ -51,7 +51,7 @@ func main() { - main.go ```go -import _ "github.com/zeromicro/zero-contrib/zrpc/registry/polaris" +import _ "github.com/wqyjh/zero-contrib/zrpc/registry/polaris" ``` - etc/\*.yaml diff --git a/zrpc/registry/polaris/go.mod b/zrpc/registry/polaris/go.mod index 237c771..ad75f93 100644 --- a/zrpc/registry/polaris/go.mod +++ b/zrpc/registry/polaris/go.mod @@ -1,4 +1,4 @@ -module github.com/zeromicro/zero-contrib/zrpc/registry/polaris +module github.com/wqyjh/zero-contrib/zrpc/registry/polaris go 1.15