From f17f6747c44a6493a478c23768dce052429f9a0b Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 31 May 2023 16:34:04 +0300 Subject: [PATCH] [#54] Fix linter warnings Signed-off-by: Alex Vanin --- app.go | 42 ++++++++++++++++++------------------- settings.go | 6 +----- tokens/bearer-token.go | 12 ++++++----- tokens/bearer-token_test.go | 4 ++-- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/app.go b/app.go index 6a52a55..f43d187 100644 --- a/app.go +++ b/app.go @@ -494,41 +494,41 @@ func (a *app) configureRouter(uploadRoutes *uploader.Uploader, downloadRoutes *d a.webServer.Handler = r.Handler } -func (a *app) logger(req fasthttp.RequestHandler) fasthttp.RequestHandler { - return func(ctx *fasthttp.RequestCtx) { - a.log.Info("request", zap.String("remote", ctx.RemoteAddr().String()), - zap.ByteString("method", ctx.Method()), - zap.ByteString("path", ctx.Path()), - zap.ByteString("query", ctx.QueryArgs().QueryString()), - zap.Uint64("id", ctx.ID())) - req(ctx) +func (a *app) logger(h fasthttp.RequestHandler) fasthttp.RequestHandler { + return func(req *fasthttp.RequestCtx) { + a.log.Info("request", zap.String("remote", req.RemoteAddr().String()), + zap.ByteString("method", req.Method()), + zap.ByteString("path", req.Path()), + zap.ByteString("query", req.QueryArgs().QueryString()), + zap.Uint64("id", req.ID())) + h(req) } } -func (a *app) tokenizer(req fasthttp.RequestHandler) fasthttp.RequestHandler { - return func(ctx *fasthttp.RequestCtx) { - appCtx, err := tokens.StoreBearerTokenAppCtx(ctx, a.ctx) +func (a *app) tokenizer(h fasthttp.RequestHandler) fasthttp.RequestHandler { + return func(req *fasthttp.RequestCtx) { + appCtx, err := tokens.StoreBearerTokenAppCtx(a.ctx, req) if err != nil { a.log.Error("could not fetch and store bearer token", zap.Error(err)) - response.Error(ctx, "could not fetch and store bearer token: "+err.Error(), fasthttp.StatusBadRequest) + response.Error(req, "could not fetch and store bearer token: "+err.Error(), fasthttp.StatusBadRequest) } - utils.SetContextToRequest(appCtx, ctx) - req(ctx) + utils.SetContextToRequest(appCtx, req) + h(req) } } -func (a *app) tracer(req fasthttp.RequestHandler) fasthttp.RequestHandler { - return func(ctx *fasthttp.RequestCtx) { - appCtx := utils.GetContextFromRequest(ctx) +func (a *app) tracer(h fasthttp.RequestHandler) fasthttp.RequestHandler { + return func(req *fasthttp.RequestCtx) { + appCtx := utils.GetContextFromRequest(req) - appCtx, span := utils.StartHTTPServerSpan(appCtx, ctx, "REQUEST") + appCtx, span := utils.StartHTTPServerSpan(appCtx, req, "REQUEST") defer func() { - utils.SetHTTPTraceInfo(appCtx, span, ctx) + utils.SetHTTPTraceInfo(appCtx, span, req) span.End() }() - utils.SetContextToRequest(appCtx, ctx) - req(ctx) + utils.SetContextToRequest(appCtx, req) + h(req) } } diff --git a/settings.go b/settings.go index 87c56bc..7a78c53 100644 --- a/settings.go +++ b/settings.go @@ -320,11 +320,7 @@ func mergeConfig(v *viper.Viper, fileName string) error { } }() - if err = v.MergeConfig(cfgFile); err != nil { - return err - } - - return nil + return v.MergeConfig(cfgFile) } // newLogger constructs a zap.Logger instance for current application. diff --git a/tokens/bearer-token.go b/tokens/bearer-token.go index fd4404b..b01860d 100644 --- a/tokens/bearer-token.go +++ b/tokens/bearer-token.go @@ -13,9 +13,11 @@ import ( type fromHandler = func(h *fasthttp.RequestHeader) []byte +type ctxKey string + const ( - bearerTokenHdr = "Bearer" - bearerTokenKey = "__context_bearer_token_key" + bearerTokenHdr = "Bearer" + bearerTokenKey ctxKey = "__context_bearer_token_key" ) // BearerToken usage: @@ -50,12 +52,12 @@ func BearerTokenFromCookie(h *fasthttp.RequestHeader) []byte { // StoreBearerTokenAppCtx extracts a bearer token from the header or cookie and stores // it in the application context. -func StoreBearerTokenAppCtx(ctx *fasthttp.RequestCtx, appCtx context.Context) (context.Context, error) { - tkn, err := fetchBearerToken(ctx) +func StoreBearerTokenAppCtx(ctx context.Context, req *fasthttp.RequestCtx) (context.Context, error) { + tkn, err := fetchBearerToken(req) if err != nil { return nil, err } - newCtx := context.WithValue(appCtx, bearerTokenKey, tkn) + newCtx := context.WithValue(ctx, bearerTokenKey, tkn) return newCtx, nil } diff --git a/tokens/bearer-token_test.go b/tokens/bearer-token_test.go index 170246c..cc54e74 100644 --- a/tokens/bearer-token_test.go +++ b/tokens/bearer-token_test.go @@ -151,10 +151,10 @@ func Test_checkAndPropagateBearerToken(t *testing.T) { t64 := base64.StdEncoding.EncodeToString(tkn.Marshal()) require.NotEmpty(t, t64) - ctx := makeTestRequest(t64, "") + req := makeTestRequest(t64, "") // Expect to see the token within the context. - appCtx, err := StoreBearerTokenAppCtx(ctx, context.Background()) + appCtx, err := StoreBearerTokenAppCtx(context.Background(), req) require.NoError(t, err) // Expect to see the same token without errors.