Skip to content

Commit

Permalink
Fix directory listing paths
Browse files Browse the repository at this point in the history
  • Loading branch information
rsierra committed Jan 27, 2020
1 parent 8fc8df6 commit 07fddd3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ package main
import (
"flag"
"html/template"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"os"
"path/filepath"
)

var port = flag.String("p", "8100", "port to serve on")
Expand Down Expand Up @@ -44,7 +45,7 @@ func serveTemplate(w http.ResponseWriter, r *http.Request) {
lp := filepath.Join("templates", "layout.html")
fp := filepath.Join("templates", template_name)

files, err := filePathWalkDir(*directory)
files, err := IOReadDir(*directory)
if err != nil {
log.Fatal(err)
}
Expand All @@ -54,13 +55,15 @@ func serveTemplate(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "layout", files)
}

func filePathWalkDir(root string) ([]string, error) {
func IOReadDir(root string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
fileInfo, err := ioutil.ReadDir(root)
if err != nil {
return files, err
}

for _, file := range fileInfo {
files = append(files, file.Name())
}
return nil
})
return files, err
return files, nil
}
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<h1>Files list</h1>
<ul>
{{range .}}
<li><a href="/{{.}}" class="qr-link">{{.}}</a></li>
<li><a href="/files/{{.}}" class="qr-link">{{.}}</a></li>
{{end}}
</ul>
{{end}}

0 comments on commit 07fddd3

Please sign in to comment.