mirror of
https://github.com/caddyserver/caddy.git
synced 2026-10-20 19:04:36 +00:00
Expanded index file support to other middlewares (fixes #27)
This commit is contained in:
parent
a9064a7871
commit
47717fee88
7 changed files with 117 additions and 52 deletions
|
|
@ -20,11 +20,17 @@ type Markdown struct {
|
|||
// Server root
|
||||
Root string
|
||||
|
||||
// Jail the requests to site root with a mock file system
|
||||
FileSys http.FileSystem
|
||||
|
||||
// Next HTTP handler in the chain
|
||||
Next middleware.Handler
|
||||
|
||||
// The list of markdown configurations
|
||||
Configs []Config
|
||||
|
||||
// The list of index files to try
|
||||
IndexFiles []string
|
||||
}
|
||||
|
||||
// Config stores markdown middleware configurations.
|
||||
|
|
@ -52,11 +58,14 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
|
|||
continue
|
||||
}
|
||||
|
||||
for _, ext := range m.Extensions {
|
||||
if strings.HasSuffix(r.URL.Path, ext) {
|
||||
fpath := md.Root + r.URL.Path
|
||||
fpath := r.URL.Path
|
||||
if idx, ok := middleware.IndexFile(md.FileSys, fpath, md.IndexFiles); ok {
|
||||
fpath = idx
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadFile(fpath)
|
||||
for _, ext := range m.Extensions {
|
||||
if strings.HasSuffix(fpath, ext) {
|
||||
f, err := md.FileSys.Open(fpath)
|
||||
if err != nil {
|
||||
if os.IsPermission(err) {
|
||||
return http.StatusForbidden, err
|
||||
|
|
@ -64,6 +73,11 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
|
|||
return http.StatusNotFound, nil
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
content := blackfriday.Markdown(body, m.Renderer, 0)
|
||||
|
||||
var scripts, styles string
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue