mirror of
https://github.com/caddyserver/caddy.git
synced 2025-05-29 23:25:50 +08:00
Added the Context to the browse directive
Moved the Context type to middleware and exported it. Users can use .Include and others in browse directive templating Created test for the templates directive.
This commit is contained in:
@ -5,13 +5,13 @@ package browse
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
@ -51,6 +51,8 @@ type Listing struct {
|
||||
|
||||
// And which order
|
||||
Order string
|
||||
|
||||
middleware.Context
|
||||
}
|
||||
|
||||
// FileInfo is the info about a particular file or directory
|
||||
@ -135,8 +137,9 @@ var IndexPages = []string{
|
||||
"default.htm",
|
||||
}
|
||||
|
||||
func directoryListing(files []os.FileInfo, urlPath string, canGoUp bool) (Listing, error) {
|
||||
func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root string) (Listing, error) {
|
||||
var fileinfos []FileInfo
|
||||
var urlPath = r.URL.Path
|
||||
for _, f := range files {
|
||||
name := f.Name()
|
||||
|
||||
@ -168,6 +171,11 @@ func directoryListing(files []os.FileInfo, urlPath string, canGoUp bool) (Listin
|
||||
Path: urlPath,
|
||||
CanGoUp: canGoUp,
|
||||
Items: fileinfos,
|
||||
Context: middleware.Context{
|
||||
Root: http.Dir(root),
|
||||
Req: r,
|
||||
URL: r.URL,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -222,7 +230,7 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
}
|
||||
}
|
||||
// Assemble listing of directory contents
|
||||
listing, err := directoryListing(files, r.URL.Path, canGoUp)
|
||||
listing, err := directoryListing(files, r, canGoUp, b.Root)
|
||||
if err != nil { // directory isn't browsable
|
||||
continue
|
||||
}
|
||||
|
Reference in New Issue
Block a user