mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Remove unused middlewares and fix linter errors
This commit is contained in:
parent
7dd62276af
commit
afdb367ada
7 changed files with 5 additions and 73 deletions
|
@ -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())
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package client
|
||||
|
||||
// message types
|
||||
// Message Types.
|
||||
const (
|
||||
msgRequestGet = "get"
|
||||
msgRequestQuery = "query"
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/tevino/abool"
|
||||
)
|
||||
|
||||
// Client errors
|
||||
// Client errors.
|
||||
var (
|
||||
ErrMalformedMessage = errors.New("malformed message")
|
||||
)
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"github.com/safing/portbase/log"
|
||||
)
|
||||
|
||||
// Config Keys
|
||||
// Config Keys.
|
||||
const (
|
||||
CfgDefaultListenAddressKey = "core/listenAddress"
|
||||
CfgAPIKeys = "core/apiKeys"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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()
|
||||
})
|
||||
}
|
Loading…
Add table
Reference in a new issue