From 26b73ebe9aad43876a7b5eb1d6211ff211389ccd Mon Sep 17 00:00:00 2001 From: Nevio Date: Mon, 20 May 2024 20:14:10 +0200 Subject: [PATCH] Updating methods to reflect unpack changes --- go.mod | 6 ++++-- go.sum | 3 +++ logger.go | 5 +++-- metrics.go | 5 +++-- middleware.go | 9 +++++---- nocancelctx.go | 5 +++-- sentry.go | 8 ++++---- sql_logger.go | 16 ++++++++-------- 8 files changed, 33 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index c9d5769..fdcf4b8 100644 --- a/go.mod +++ b/go.mod @@ -34,8 +34,10 @@ require ( github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // indirect - golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect - golang.org/x/text v0.3.7 // indirect + golang.org/x/sys v0.9.0 // indirect + golang.org/x/text v0.10.0 // indirect google.golang.org/protobuf v1.28.1 // indirect mellium.im/sasl v0.3.0 // indirect ) + +replace github.com/vmkteam/zenrpc/v2 => github.com/unpackdev/zenrpc/v2 v2.2.11-unpack diff --git a/go.sum b/go.sum index 7d28793..c11d30b 100644 --- a/go.sum +++ b/go.sum @@ -334,6 +334,7 @@ github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GH github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +github.com/unpackdev/zenrpc/v2 v2.2.11-unpack/go.mod h1:T/ZQlJbKThBNJtyN0313xEPcxjEyB19uNldTBr0o2KE= github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= @@ -537,6 +538,7 @@ golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY= golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -547,6 +549,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/logger.go b/logger.go index ac2ac36..04c8825 100644 --- a/logger.go +++ b/logger.go @@ -3,6 +3,7 @@ package middleware import ( "context" "encoding/json" + "net/http" "time" "github.com/vmkteam/zenrpc/v2" @@ -11,9 +12,9 @@ import ( // WithAPILogger logs via Printf function (e.g. log.Printf) all requests. func WithAPILogger(pf Printf, serverName string) zenrpc.MiddlewareFunc { return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc { - return func(ctx context.Context, method string, params json.RawMessage) zenrpc.Response { + return func(ctx context.Context, w http.ResponseWriter, method string, params json.RawMessage) zenrpc.Response { start := time.Now() - r := h(ctx, method, params) + r := h(ctx, w, method, params) methodName := fullMethodName(serverName, zenrpc.NamespaceFromContext(ctx), method) pf("ip=%s platform=%q version=%q method=%s duration=%v params=%q err=%q userAgent=%q xRequestId=%q", diff --git a/metrics.go b/metrics.go index 412a56f..6edb479 100644 --- a/metrics.go +++ b/metrics.go @@ -3,6 +3,7 @@ package middleware import ( "context" "encoding/json" + "net/http" "strconv" "time" @@ -35,9 +36,9 @@ func WithMetrics(appName string) zenrpc.MiddlewareFunc { prometheus.MustRegister(rpcErrors, rpcDurations) return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc { - return func(ctx context.Context, method string, params json.RawMessage) zenrpc.Response { + return func(ctx context.Context, w http.ResponseWriter, method string, params json.RawMessage) zenrpc.Response { start, code := time.Now(), "" - r := h(ctx, method, params) + r := h(ctx, w, method, params) // log metrics if n := zenrpc.NamespaceFromContext(ctx); n != "" { diff --git a/middleware.go b/middleware.go index f6f274e..8f7ef4f 100644 --- a/middleware.go +++ b/middleware.go @@ -3,6 +3,7 @@ package middleware import ( "context" "encoding/json" + "net/http" "github.com/labstack/echo/v4" "github.com/vmkteam/zenrpc/v2" @@ -29,9 +30,9 @@ type ( // WithDevel sets bool flag to context for detecting development environment. func WithDevel(isDevel bool) zenrpc.MiddlewareFunc { return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc { - return func(ctx context.Context, method string, params json.RawMessage) zenrpc.Response { + return func(ctx context.Context, w http.ResponseWriter, method string, params json.RawMessage) zenrpc.Response { ctx = NewIsDevelContext(ctx, isDevel) - return h(ctx, method, params) + return h(ctx, w, method, params) } } } @@ -39,7 +40,7 @@ func WithDevel(isDevel bool) zenrpc.MiddlewareFunc { // WithHeaders sets User-Agent, Platform, Version, X-Country headers to context. User-Agent strips to 2048 chars, Platform and Version – to 64, X-Country - to 16. func WithHeaders() zenrpc.MiddlewareFunc { return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc { - return func(ctx context.Context, method string, params json.RawMessage) zenrpc.Response { + return func(ctx context.Context, w http.ResponseWriter, method string, params json.RawMessage) zenrpc.Response { if req, ok := zenrpc.RequestFromContext(ctx); ok && req != nil { ctx = NewUserAgentContext(ctx, req.UserAgent()) ctx = NewPlatformContext(ctx, req.Header.Get("Platform")) @@ -47,7 +48,7 @@ func WithHeaders() zenrpc.MiddlewareFunc { ctx = NewXRequestIDContext(ctx, req.Header.Get(echo.HeaderXRequestID)) ctx = NewCountryContext(ctx, req.Header.Get("X-Country")) } - return h(ctx, method, params) + return h(ctx, w, method, params) } } } diff --git a/nocancelctx.go b/nocancelctx.go index a34cbb4..2a9f5d5 100644 --- a/nocancelctx.go +++ b/nocancelctx.go @@ -3,6 +3,7 @@ package middleware import ( "context" "encoding/json" + "net/http" "time" "github.com/vmkteam/zenrpc/v2" @@ -25,8 +26,8 @@ func withoutCancel(ctx context.Context) context.Context { // WithNoCancelContext ignores Cancel func from context. This is useful for passing context to `go-pg`. func WithNoCancelContext() zenrpc.MiddlewareFunc { return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc { - return func(ctx context.Context, method string, params json.RawMessage) zenrpc.Response { - return h(withoutCancel(ctx), method, params) + return func(ctx context.Context, w http.ResponseWriter, method string, params json.RawMessage) zenrpc.Response { + return h(withoutCancel(ctx), w, method, params) } } } diff --git a/sentry.go b/sentry.go index f56857a..80b6d01 100644 --- a/sentry.go +++ b/sentry.go @@ -30,7 +30,7 @@ func sentryHubFromContext(ctx context.Context) (*sentry.Hub, bool) { // version, method. func WithSentry(serverName string) zenrpc.MiddlewareFunc { return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc { - return func(ctx context.Context, method string, params json.RawMessage) zenrpc.Response { + return func(ctx context.Context, w http.ResponseWriter, method string, params json.RawMessage) zenrpc.Response { if hub, ok := sentryHubFromContext(ctx); ok { start, platform, version, ip, xRequestID := time.Now(), PlatformFromContext(ctx), VersionFromContext(ctx), IPFromContext(ctx), XRequestIDFromContext(ctx) @@ -49,7 +49,7 @@ func WithSentry(serverName string) zenrpc.MiddlewareFunc { }) } - return h(ctx, method, params) + return h(ctx, w, method, params) } } } @@ -58,11 +58,11 @@ func WithSentry(serverName string) zenrpc.MiddlewareFunc { // sensitive error data from response. It is good to use pkg/errors for stack trace support in sentry. func WithErrorLogger(pf Printf, serverName string) zenrpc.MiddlewareFunc { return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc { - return func(ctx context.Context, method string, params json.RawMessage) zenrpc.Response { + return func(ctx context.Context, w http.ResponseWriter, method string, params json.RawMessage) zenrpc.Response { start, platform, version, ip, xRequestID := time.Now(), PlatformFromContext(ctx), VersionFromContext(ctx), IPFromContext(ctx), XRequestIDFromContext(ctx) namespace := zenrpc.NamespaceFromContext(ctx) - r := h(ctx, method, params) + r := h(ctx, w, method, params) if r.Error != nil && (r.Error.Code == http.StatusInternalServerError || r.Error.Code < 0) { duration := time.Since(start) methodName := fullMethodName(serverName, namespace, method) diff --git a/sql_logger.go b/sql_logger.go index ee7d8ef..b5f0396 100644 --- a/sql_logger.go +++ b/sql_logger.go @@ -63,23 +63,23 @@ func SqlGroupFromContext(ctx context.Context) string { // If `DurationRemote` or `DurationDiff` are set then `DurationLocal` excludes these values. func WithTiming(isDevel bool, allowDebugFunc AllowDebugFunc) zenrpc.MiddlewareFunc { return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc { - return func(ctx context.Context, method string, params json.RawMessage) (resp zenrpc.Response) { + return func(ctx context.Context, w http.ResponseWriter, method string, params json.RawMessage) (resp zenrpc.Response) { // check for debug id if !isDevel { req, ok := zenrpc.RequestFromContext(ctx) if !ok || req == nil { - return h(ctx, method, params) + return h(ctx, w, method, params) } reqClone := req.Clone(ctx) if reqClone == nil || !allowDebugFunc(reqClone) { - return h(ctx, method, params) + return h(ctx, w, method, params) } } now := time.Now() - resp = h(ctx, method, params) + resp = h(ctx, w, method, params) if resp.Extensions == nil { resp.Extensions = make(map[string]interface{}) } @@ -111,19 +111,19 @@ func WithSQLLogger(db *pg.DB, isDevel bool, allowDebugFunc, allowSqlDebugFunc Al db.AddQueryHook(ql) return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc { - return func(ctx context.Context, method string, params json.RawMessage) (resp zenrpc.Response) { + return func(ctx context.Context, w http.ResponseWriter, method string, params json.RawMessage) (resp zenrpc.Response) { logQuery := true // check for debug id if !isDevel { req, ok := zenrpc.RequestFromContext(ctx) if !ok || req == nil { - return h(ctx, method, params) + return h(ctx, w, method, params) } reqClone := req.Clone(ctx) if reqClone == nil || !allowDebugFunc(reqClone) { - return h(ctx, method, params) + return h(ctx, w, method, params) } if reqClone == nil || !allowSqlDebugFunc(reqClone) { @@ -135,7 +135,7 @@ func WithSQLLogger(db *pg.DB, isDevel bool, allowDebugFunc, allowSqlDebugFunc Al ctx = NewDebugIDContext(ctx, debugID) ql.Push(debugID) - resp = h(ctx, method, params) + resp = h(ctx, w, method, params) if resp.Extensions == nil { resp.Extensions = make(map[string]interface{}) }