Merge pull request #10 from owncloud/static-root-path

Add root path to static middleware
This commit is contained in:
Thomas Boerger
2019-12-09 14:35:57 +01:00
committed by GitHub
2 changed files with 11 additions and 3 deletions

View File

@@ -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

View File

@@ -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, "/") {