Skip to content

Commit

Permalink
sorted directories and files for static file serving
Browse files Browse the repository at this point in the history
  • Loading branch information
dkllrjr committed Dec 5, 2024
1 parent 3b01785 commit a369ddf
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions middleware/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package middleware
import (
"errors"
"fmt"
"sort"
"html/template"
"net/http"
"net/url"
Expand Down Expand Up @@ -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 = `
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a369ddf

Please sign in to comment.