improve files listing for ghttp.Server

This commit is contained in:
John
2020-05-18 20:09:00 +08:00
parent bd8d3fca08
commit 38111a64e3
3 changed files with 22 additions and 7 deletions

View File

@ -15,4 +15,11 @@
[viewer]
delimiters = ["${", "}"]
autoencode = true
autoencode = true
[server]
Address = ":8800"
ServerRoot = "/Users/john/Downloads"
ServerAgent = "gf-app"
# LogPath = "./log/gf-app/server"

View File

@ -1,12 +1,11 @@
package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
)
func main() {
b1 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
b2 := b1[:2]
b1[0] = 9
fmt.Println(b2)
s := g.Server()
s.SetIndexFolder(true)
s.Run()
}

View File

@ -279,7 +279,16 @@ func (s *Server) listDir(r *Request, f http.File) {
r.Response.WriteStatus(http.StatusInternalServerError, "Error reading directory")
return
}
sort.Slice(files, func(i, j int) bool { return files[i].Name() < files[j].Name() })
// The folder type has the most priority than file.
sort.Slice(files, func(i, j int) bool {
if files[i].IsDir() && !files[j].IsDir() {
return true
}
if !files[i].IsDir() && files[j].IsDir() {
return false
}
return files[i].Name() < files[j].Name()
})
if r.Response.Header().Get("Content-Type") == "" {
r.Response.Header().Set("Content-Type", "text/html; charset=utf-8")
}