From afdb367adafb58d28046a6987806c1e9e62bee27 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 28 Jan 2021 16:50:23 +0100 Subject: [PATCH] Remove unused middlewares and fix linter errors --- api/authentication.go | 16 ------------- api/client/const.go | 2 +- api/client/message.go | 2 +- api/config.go | 2 +- api/endpoints.go | 2 +- api/main.go | 2 +- api/middleware.go | 52 ------------------------------------------- 7 files changed, 5 insertions(+), 73 deletions(-) delete mode 100644 api/middleware.go diff --git a/api/authentication.go b/api/authentication.go index 2ef6352..792e6e2 100644 --- a/api/authentication.go +++ b/api/authentication.go @@ -133,22 +133,6 @@ func SetAuthenticator(fn AuthenticatorFunc) error { return nil } -func authMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - token := authenticateRequest(w, r, next) - if token == nil { - // Authenticator already replied. - return - } - - // Add token to request and serve next handler. - if _, apiRequest := getAPIContext(r); apiRequest != nil { - apiRequest.AuthToken = token - } - next.ServeHTTP(w, r) - }) -} - func authenticateRequest(w http.ResponseWriter, r *http.Request, targetHandler http.Handler) *AuthToken { tracer := log.Tracer(r.Context()) diff --git a/api/client/const.go b/api/client/const.go index c189c0e..080e938 100644 --- a/api/client/const.go +++ b/api/client/const.go @@ -1,6 +1,6 @@ package client -// message types +// Message Types. const ( msgRequestGet = "get" msgRequestQuery = "query" diff --git a/api/client/message.go b/api/client/message.go index 7280659..3e9b9dc 100644 --- a/api/client/message.go +++ b/api/client/message.go @@ -9,7 +9,7 @@ import ( "github.com/tevino/abool" ) -// Client errors +// Client errors. var ( ErrMalformedMessage = errors.New("malformed message") ) diff --git a/api/config.go b/api/config.go index ba40a44..706f5b4 100644 --- a/api/config.go +++ b/api/config.go @@ -7,7 +7,7 @@ import ( "github.com/safing/portbase/log" ) -// Config Keys +// Config Keys. const ( CfgDefaultListenAddressKey = "core/listenAddress" CfgAPIKeys = "core/apiKeys" diff --git a/api/endpoints.go b/api/endpoints.go index 2c92b97..c977fee 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -70,7 +70,7 @@ type ( RecordFunc func(ar *Request) (r record.Record, err error) ) -// MIME Types +// MIME Types. const ( MimeTypeJSON string = "application/json" MimeTypeText string = "text/plain" diff --git a/api/main.go b/api/main.go index 78846ef..e47b618 100644 --- a/api/main.go +++ b/api/main.go @@ -17,7 +17,7 @@ var ( exportEndpoints bool ) -// API Errors +// API Errors. var ( ErrAuthenticationAlreadySet = errors.New("the authentication function has already been set") ErrAuthenticationImmutable = errors.New("the authentication function can only be set before the api has started") diff --git a/api/middleware.go b/api/middleware.go deleted file mode 100644 index 7877037..0000000 --- a/api/middleware.go +++ /dev/null @@ -1,52 +0,0 @@ -package api - -import ( - "context" - "net/http" - - "github.com/safing/portbase/log" -) - -// Middleware is a function that can be added as a middleware to the API endpoint. -type Middleware func(next http.Handler) http.Handler - -type mwHandler struct { - handlers []Middleware - final http.Handler -} - -func (mwh *mwHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - handlerLock.RLock() - defer handlerLock.RUnlock() - - // final handler - handler := mwh.final - - // build middleware chain - // loop in reverse to build the handler chain in the correct order - for i := len(mwh.handlers) - 1; i >= 0; i-- { - handler = mwh.handlers[i](handler) - } - - // start - handler.ServeHTTP(w, r) -} - -// ModuleWorker is an http middleware that wraps the request in a module worker. -func ModuleWorker(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - _ = module.RunWorker("http request", func(_ context.Context) error { - next.ServeHTTP(w, r) - return nil - }) - }) -} - -// LogTracer is an http middleware that attaches a log tracer to the request context. -func LogTracer(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx, tracer := log.AddTracer(r.Context()) - next.ServeHTTP(w, r.WithContext(ctx)) - tracer.Submit() - }) -}