mirror of
https://github.com/safing/portbase
synced 2025-09-17 02:29:50 +00:00
Update api with registering handlers, middleware and authentication
This commit is contained in:
parent
a7105dc6ba
commit
8d091f7f7a
10 changed files with 255 additions and 37 deletions
27
api/middleware.go
Normal file
27
api/middleware.go
Normal 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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue