diff --git a/changelog/unreleased/static-root-path.md b/changelog/unreleased/static-root-path.md new file mode 100644 index 0000000000..d7d2778a86 --- /dev/null +++ b/changelog/unreleased/static-root-path.md @@ -0,0 +1,7 @@ +Change: Add root path to static middleware + +Currently the `Static` middleware always serves from the root path, but all our +HTTP handlers accept a custom root path which also got to be applied to the +static file handling. + +https://github.com/owncloud/ocis-pkg/issues/9 diff --git a/middleware/static.go b/middleware/static.go index 4705254e23..ae06aacbc9 100644 --- a/middleware/static.go +++ b/middleware/static.go @@ -2,13 +2,14 @@ package middleware import ( "net/http" + "path" "strings" ) // Static is a middleware that serves static assets. -func Static(fs http.FileSystem) func(http.Handler) http.Handler { +func Static(root string, fs http.FileSystem) func(http.Handler) http.Handler { static := http.StripPrefix( - "/", + root+"/", http.FileServer( fs, ), @@ -16,7 +17,7 @@ func Static(fs http.FileSystem) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if strings.HasPrefix(r.URL.Path, "/api") { + if strings.HasPrefix(r.URL.Path, path.Join(root, "api")) { next.ServeHTTP(w, r) } else { if strings.HasSuffix(r.URL.Path, "/") {