Update api with registering handlers, middleware and authentication

This commit is contained in:
Daniel 2019-07-30 13:08:45 +02:00
parent a7105dc6ba
commit 8d091f7f7a
10 changed files with 255 additions and 37 deletions

27
api/middleware.go Normal file
View file

@ -0,0 +1,27 @@
package api
import "net/http"
// 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
for _, mw := range mwh.handlers {
handler = mw(handler)
}
// start
handler.ServeHTTP(w, r)
}