Implement per-site index (#1906)

This commit is contained in:
Mohammed Al Sahaf
2017-10-30 00:13:10 +03:00
committed by Toby Allen
parent fc75527eb5
commit f7a70266ed
13 changed files with 110 additions and 24 deletions
+1 -2
View File
@@ -19,7 +19,6 @@ import (
"strings"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/mholt/caddy/caddyhttp/staticfiles"
)
func (h Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
@@ -44,7 +43,7 @@ outer:
matches := httpserver.Path(urlPath).Matches(rule.Path)
// Also check IndexPages when requesting a directory
if !matches {
indexFile, isIndexFile := httpserver.IndexFile(h.Root, urlPath, staticfiles.IndexPages)
indexFile, isIndexFile := httpserver.IndexFile(h.Root, urlPath, h.indexPages)
if isIndexFile {
matches = httpserver.Path(indexFile).Matches(rule.Path)
}
+2 -1
View File
@@ -393,7 +393,8 @@ func TestMiddlewareShouldPushIndexFile(t *testing.T) {
{Path: "/index.css", Method: http.MethodGet},
}},
},
Root: http.Dir(root),
Root: http.Dir(root),
indexPages: []string{indexFile},
}
indexFilePath := filepath.Join(root, indexFile)
+4 -3
View File
@@ -36,9 +36,10 @@ type (
// Middleware supports pushing resources to clients
Middleware struct {
Next httpserver.Handler
Rules []Rule
Root http.FileSystem
Next httpserver.Handler
Rules []Rule
Root http.FileSystem
indexPages []string // will be injected from SiteConfig on setup
}
ruleOp func([]Resource)
+1 -1
View File
@@ -50,7 +50,7 @@ func setup(c *caddy.Controller) error {
cfg := httpserver.GetConfig(c)
cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return Middleware{Next: next, Rules: rules, Root: http.Dir(cfg.Root)}
return Middleware{Next: next, Rules: rules, Root: http.Dir(cfg.Root), indexPages: cfg.IndexPages}
})
return nil