Skip to content

Commit

Permalink
Updating methods to reflect unpack changes
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed May 20, 2024
1 parent 35ad3f8 commit 26b73eb
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 24 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand Down
5 changes: 3 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"context"
"encoding/json"
"net/http"
"time"

"github.com/vmkteam/zenrpc/v2"
Expand All @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"context"
"encoding/json"
"net/http"
"strconv"
"time"

Expand Down Expand Up @@ -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 != "" {
Expand Down
9 changes: 5 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"context"
"encoding/json"
"net/http"

"github.com/labstack/echo/v4"
"github.com/vmkteam/zenrpc/v2"
Expand All @@ -29,25 +30,25 @@ 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)
}
}
}

// 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"))
ctx = NewVersionContext(ctx, req.Header.Get("Version"))
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)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions nocancelctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"context"
"encoding/json"
"net/http"
"time"

"github.com/vmkteam/zenrpc/v2"
Expand All @@ -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)
}
}
}
8 changes: 4 additions & 4 deletions sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -49,7 +49,7 @@ func WithSentry(serverName string) zenrpc.MiddlewareFunc {
})
}

return h(ctx, method, params)
return h(ctx, w, method, params)
}
}
}
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions sql_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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{})
}
Expand Down

0 comments on commit 26b73eb

Please sign in to comment.