From 5b1838a63bdb57050b0a72edd3242f05e549e556 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 19 Jan 2024 17:06:41 -0500 Subject: [PATCH] feat: create tus api builder --- api/middleware/s5.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/api/middleware/s5.go b/api/middleware/s5.go index 0402702..c8789ab 100644 --- a/api/middleware/s5.go +++ b/api/middleware/s5.go @@ -3,6 +3,7 @@ package middleware import ( "context" "fmt" + "git.lumeweb.com/LumeWeb/portal/api" "git.lumeweb.com/LumeWeb/portal/interfaces" "github.com/golang-jwt/jwt/v5" "go.sia.tech/jape" @@ -100,3 +101,31 @@ func AuthMiddleware(handler jape.Handler, portal interfaces.Portal) jape.Handler }) })(handler) } + +func BuildS5TusApi(portal interfaces.Portal) jape.Handler { + // Adapt the tus middleware to a function that transforms jape.Handler + adaptedTusMiddleware := jape.Adapt(portal.Storage().Tus().Middleware) + + // Wrapper function for AuthMiddleware to fit the MiddlewareFunc signature + authMiddlewareFunc := func(h jape.Handler) jape.Handler { + return AuthMiddleware(h, portal) + } + + // Create a jape.Handler for your tusHandler + tusJapeHandler := func(c jape.Context) { + tusHandler := portal.Storage().Tus() + tusHandler.ServeHTTP(c.ResponseWriter, c.Request) + } + + protocolMiddleware := jape.Adapt(func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := context.WithValue(r.Context(), "protocol", "s5") + h.ServeHTTP(w, r.WithContext(ctx)) + }) + }) + + // Apply the middlewares to the tusJapeHandler + tusHandler := api.ApplyMiddlewares(tusJapeHandler, adaptedTusMiddleware, authMiddlewareFunc, protocolMiddleware) + + return tusHandler +}