Add root path to static middleware
This commit is contained in:
@@ -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
|
||||||
@@ -2,13 +2,14 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Static is a middleware that serves static assets.
|
// 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(
|
static := http.StripPrefix(
|
||||||
"/",
|
root+"/",
|
||||||
http.FileServer(
|
http.FileServer(
|
||||||
fs,
|
fs,
|
||||||
),
|
),
|
||||||
@@ -16,7 +17,7 @@ func Static(fs http.FileSystem) func(http.Handler) http.Handler {
|
|||||||
|
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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)
|
next.ServeHTTP(w, r)
|
||||||
} else {
|
} else {
|
||||||
if strings.HasSuffix(r.URL.Path, "/") {
|
if strings.HasSuffix(r.URL.Path, "/") {
|
||||||
|
|||||||
Reference in New Issue
Block a user