flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit 3b6a527b8800e325039859b533049eeff5b061c2
parent 95f5edc1b5d2f7286e51338727592a159aaa2ae0
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Fri, 13 Nov 2020 00:31:02 -0800

WIP adding folders

Diffstat:
Mhttp.go | 3++-
Mmain.go | 19++++++++++++-------
Mtemplates/my_site.html | 13+++++++++++++
3 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/http.go b/http.go @@ -206,7 +206,8 @@ func mySiteHandler(w http.ResponseWriter, r *http.Request) { return } // check auth - files, _ := getUserFiles(authUser) + userFolder := path.Join(c.FilesDirectory, authUser) + files, _ := getFiles(userFolder) data := struct { Host string PageTitle string diff --git a/main.go b/main.go @@ -22,12 +22,13 @@ import ( var c Config // global var to hold static configuration -type File struct { +type File struct { // also folders Creator string Name string UpdatedTime time.Time TimeAgo string IsText bool + Children []*File } type User struct { @@ -111,20 +112,24 @@ func getIndexFiles() ([]*File, error) { // cache this function return result, nil } // todo clean up paths -func getUserFiles(user string) ([]*File, error) { +func getFiles(p string) ([]*File, error) { result := []*File{} - files, err := ioutil.ReadDir(path.Join(c.FilesDirectory, user)) + files, err := ioutil.ReadDir(p) if err != nil { return nil, err } for _, file := range files { isText := strings.HasPrefix(mime.TypeByExtension(path.Ext(file.Name())), "text") - result = append(result, &File{ - Name: file.Name(), - Creator: user, + f := &File{ + Name: file.Name(), + // Creator: strings.Split(p, "/")[0], UpdatedTime: file.ModTime(), IsText: isText, - }) + } + if file.IsDir() { + f.Children, err = getFiles(path.Join(p, f.Name)) + } + result = append(result, f) } return result, nil } diff --git a/templates/my_site.html b/templates/my_site.html @@ -12,6 +12,18 @@ {{ range .Files }} <tr> <div> + {{ if .Children }} + <td> + <details> + <summary> + {{.Name}}/ + </summary> + {{ range .Children }} + {{.Name}} + {{ end }} + </details> + </td> + {{ else }} <td> <a href="//{{$authUser}}.{{$domain}}/{{.Name}}"> {{ .Name }}</a> @@ -30,6 +42,7 @@ value="delete" /> </td> + {{ end }} </form> </div> </tr>