diff --git a/handler/handler.go b/handler/handler.go index c1f9be5..0c4c14c 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -27,23 +27,24 @@ const ( execution = 1e6 ) -var ( - errAccounting = errors.New("accounting error") +var errAccounting = errors.New("accounting error") - helper handlerHelper -) +type Handler struct { +} -type handlerHelper struct{} +func New() *Handler { + return &Handler{} +} -func CompleteRequest(ctx *fasthttp.RequestCtx) { +func (h Handler) CompleteRequest(ctx *fasthttp.RequestCtx) { currentTime := time.Now() - method, resp := helper.AuthenticateAccountingProxy(ctx) + method, resp := h.authenticateAccountingProxy(ctx) executionTime := time.Since(currentTime) / execution statusCode := ctx.Response.StatusCode() - service.Metrics.UpdateStatusCounter(helper.SetMetricStatus(statusCode)) + service.Metrics.UpdateStatusCounter(h.setMetricStatus(statusCode)) if statusCode == http.StatusOK { service.Metrics.UpdateMethodResponseTime(method, executionTime) } @@ -64,7 +65,7 @@ func CompleteRequest(ctx *fasthttp.RequestCtx) { } } -func (handlerHelper) AuthenticateAccountingProxy(ctx *fasthttp.RequestCtx) (string, domain.ProxyResponse) { +func (h Handler) authenticateAccountingProxy(ctx *fasthttp.RequestCtx) (string, domain.ProxyResponse) { initialPath := string(ctx.Path()) p, path := proxy.Find(initialPath) @@ -107,7 +108,7 @@ func (handlerHelper) AuthenticateAccountingProxy(ctx *fasthttp.RequestCtx) (stri return path, p.ProxyRequest(ctx, path) } -func (handlerHelper) SetMetricStatus(statusCode int) string { +func (Handler) setMetricStatus(statusCode int) string { metricStatus := "5xx" switch { case statusCode >= 100 && statusCode < 200: diff --git a/server/http.go b/server/http.go index 1105336..504e004 100644 --- a/server/http.go +++ b/server/http.go @@ -43,7 +43,7 @@ func (s *httpSrv) run(maxRequestBodySize int64) { localConfig := config.Get().(*conf.Configuration) restAddress := localConfig.HttpInnerAddress.GetAddress() s.srv = &fasthttp.Server{ - Handler: handler.CompleteRequest, + Handler: handler.New().CompleteRequest, WriteTimeout: defaultTimeout, ReadTimeout: defaultTimeout, MaxRequestBodySize: int(maxRequestBodySize),