diff --git a/middleware/static.go b/middleware/static.go index 1016f1b09..08c69391b 100644 --- a/middleware/static.go +++ b/middleware/static.go @@ -6,6 +6,7 @@ package middleware import ( "errors" "fmt" + "sort" "html/template" "net/http" "net/url" @@ -50,6 +51,12 @@ type StaticConfig struct { Filesystem http.FileSystem `yaml:"-"` } +// Used for sorting directories in listDir +type byName []os.FileInfo +func (s byName) Len() int { return len(s) } +func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() } +func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } + const html = ` @@ -251,6 +258,9 @@ func listDir(t *template.Template, name string, dir http.File, res *echo.Respons return } + // Sort directories first + sort.Sort(byName(files)) + // Create directory index res.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8) data := struct {