From ae7088a84b47f37bbbbe4b824276d0f37c747fee Mon Sep 17 00:00:00 2001 From: Ruben Sierra <‘ruben.sierra@bizneo.com’> Date: Mon, 13 Apr 2020 00:38:32 +0200 Subject: [PATCH] Add support for folders --- server.go | 65 ++++++++++++++++++++++++++++++------------- templates/index.html | 21 ++++++++++++-- templates/layout.html | 4 +-- 3 files changed, 65 insertions(+), 25 deletions(-) diff --git a/server.go b/server.go index 26575b6..ad74027 100644 --- a/server.go +++ b/server.go @@ -10,6 +10,7 @@ package main import ( "flag" + "fmt" auth "github.com/abbot/go-http-auth" "html/template" "io/ioutil" @@ -20,16 +21,16 @@ import ( ) var port = flag.String("p", "8100", "port to serve on") -var directory = flag.String("d", ".", "the directory of files to host") +var root_folder = flag.String("d", ".", "the root folder of files to host") func main() { flag.Parse() - fs := http.FileServer(http.Dir(*directory)) - http.Handle("/files/", http.StripPrefix("/files/", fs)) + fs := http.FileServer(http.Dir(*root_folder)) + http.Handle("/_app/files/", http.StripPrefix("/_app/files/", fs)) ss := http.FileServer(http.Dir("statics")) - http.Handle("/statics/", http.StripPrefix("/statics/", ss)) + http.Handle("/_app/statics/", http.StripPrefix("/_app/statics/", ss)) htppasswd := os.Getenv("HTPASSWD_FILE") if htppasswd == "" { @@ -41,7 +42,7 @@ func main() { http.Handle("/", a.Wrap(serveAuthTemplate)) } - log.Printf("Serving %s on HTTP port: %s\n", *directory, *port) + log.Printf("Serving %s on HTTP port: %s\n", *root_folder, *port) log.Fatal(http.ListenAndServe(":"+*port, nil)) } @@ -50,33 +51,57 @@ func serveAuthTemplate(w http.ResponseWriter, ar *auth.AuthenticatedRequest) { } func serveTemplate(w http.ResponseWriter, r *http.Request) { - var template_name string - if template_name = filepath.Clean(r.URL.Path); template_name == "/" { - template_name = "/index.html" - } - - lp := filepath.Join("templates", "layout.html") - fp := filepath.Join("templates", template_name) + request_path := filepath.Clean(r.URL.Path) - files, err := IOReadDir(*directory) + files, folders, err := ListFilesAndFolders(filepath.Clean(filepath.Join(*root_folder, request_path))) if err != nil { - log.Fatal(err) + errorHandler(w, r, http.StatusNotFound) + return } - log.Printf("Template %s\n", template_name) + data := struct { + CurrentFolder string + ParentFolder string + Files []string + Folders []string + }{FolderToWeb(request_path), FolderToWeb(filepath.Dir(request_path)), files, folders} + + lp := filepath.Join("templates", "layout.html") + fp := filepath.Join("templates", "index.html") tmpl, _ := template.ParseFiles(lp, fp) - tmpl.ExecuteTemplate(w, "layout", files) + tmpl.ExecuteTemplate(w, "layout", data) } -func IOReadDir(root string) ([]string, error) { +func FolderToWeb(folder string) string { + if folder == "/" { + return folder + } else { + return folder + "/" + } +} + +func ListFilesAndFolders(root string) ([]string, []string, error) { var files []string + var folders []string fileInfo, err := ioutil.ReadDir(root) if err != nil { - return files, err + return files, folders, err } for _, file := range fileInfo { - files = append(files, file.Name()) + if file.IsDir() { + folders = append(folders, file.Name()) + } else { + files = append(files, file.Name()) + } + } + + return files, folders, nil +} + +func errorHandler(w http.ResponseWriter, r *http.Request, status int) { + w.WriteHeader(status) + if status == http.StatusNotFound { + fmt.Fprint(w, "Resource not found") } - return files, nil } diff --git a/templates/index.html b/templates/index.html index 7ffbc95..55a4d67 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,13 +1,28 @@ {{define "title"}}Files list{{end}} {{define "body"}} +{{with $data := .}} +{{if eq $data.CurrentFolder "/"}}