feat: create ApplyMiddlewares helper

This commit is contained in:
Derrick Hammer 2024-01-19 17:05:18 -05:00
parent e9db71f3b8
commit 26c28db1f2
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 10 additions and 0 deletions

View File

@ -3,8 +3,11 @@ package api
import ( import (
"git.lumeweb.com/LumeWeb/portal/interfaces" "git.lumeweb.com/LumeWeb/portal/interfaces"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"go.sia.tech/jape"
) )
type MiddlewareFunc func(jape.Handler) jape.Handler
func Init(router interfaces.APIRegistry) error { func Init(router interfaces.APIRegistry) error {
router.Register("s5", NewS5()) router.Register("s5", NewS5())
return nil return nil
@ -17,3 +20,10 @@ func registerProtocolSubdomain(portal interfaces.Portal, mux *httprouter.Router,
(*router)[name+"."+domain] = mux (*router)[name+"."+domain] = mux
} }
func ApplyMiddlewares(handler jape.Handler, middlewares ...MiddlewareFunc) jape.Handler {
// Apply each middleware in reverse order
for i := len(middlewares) - 1; i >= 0; i-- {
handler = middlewares[i](handler)
}
return handler
}