Skip to content

Commit a369ddf

Browse files
committed
sorted directories and files for static file serving
1 parent 3b01785 commit a369ddf

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

middleware/static.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package middleware
66
import (
77
"errors"
88
"fmt"
9+
"sort"
910
"html/template"
1011
"net/http"
1112
"net/url"
@@ -50,6 +51,12 @@ type StaticConfig struct {
5051
Filesystem http.FileSystem `yaml:"-"`
5152
}
5253

54+
// Used for sorting directories in listDir
55+
type byName []os.FileInfo
56+
func (s byName) Len() int { return len(s) }
57+
func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
58+
func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
59+
5360
const html = `
5461
<!DOCTYPE html>
5562
<html lang="en">
@@ -251,6 +258,9 @@ func listDir(t *template.Template, name string, dir http.File, res *echo.Respons
251258
return
252259
}
253260

261+
// Sort directories first
262+
sort.Sort(byName(files))
263+
254264
// Create directory index
255265
res.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
256266
data := struct {

0 commit comments

Comments
 (0)