Skip to content

Commit

Permalink
Merge pull request #10 from owncloud/static-root-path
Browse files Browse the repository at this point in the history
Add root path to static middleware
  • Loading branch information
tboerger authored Dec 9, 2019
2 parents 535a6ef + 7b9cdad commit 29bdcd6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 7 additions & 0 deletions changelog/unreleased/static-root-path.md
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions middleware/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ 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,
),
)

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

0 comments on commit 29bdcd6

Please sign in to comment.