-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.go
49 lines (44 loc) · 1 KB
/
dir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package server
import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
)
func dir(w http.ResponseWriter, r *http.Request) {
files, err := listArrai(".")
if err != nil {
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(toHtml(files)))
}
// listArrai returns a list of paths to arr.ai files that can be evaluated.
func listArrai(root string) ([]string, error) {
files := []string{}
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ".arrai") {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
func toHtml(files []string) string {
wd, _ := os.Getwd()
b := strings.Builder{}
b.WriteString(fmt.Sprintf("<h1>Arr.ai files under %s</h1><ul>", wd))
for _, path := range files {
b.WriteString(fmt.Sprintf(`<li><a href="%s">%s</a></li>`, path, path))
}
b.WriteString("</ul>")
return b.String()
}