main.go (view raw)
1package main
2
3import (
4 "fmt"
5 "io/ioutil"
6 "log"
7 "net/http"
8 "os"
9 "path"
10 "path/filepath"
11)
12
13const ( // todo make configurable
14 userFilesPath = "./files"
15)
16
17type File struct {
18 Creator string
19 Name string
20 UpdatedTime string
21}
22
23func getIndexFiles() ([]*File, error) { // cache this function
24 result := []*File{}
25 err := filepath.Walk(userFilesPath, func(path string, info os.FileInfo, err error) error {
26 if err != nil {
27 log.Printf("Failure accessing a path %q: %v\n", path, err)
28 return err // think about
29 }
30 // make this do what it should
31 result = append(result, &File{
32 Name: info.Name(),
33 Creator: "alex",
34 UpdatedTime: "123123",
35 })
36 return nil
37 })
38 if err != nil {
39 return nil, err
40 }
41 // sort
42 // truncate
43 return result, nil
44}
45
46func indexHandler(w http.ResponseWriter, r *http.Request) {
47 indexFiles, _ := getIndexFiles()
48 for _, file := range indexFiles {
49 fmt.Fprintf(w, "%s\n", file.Name)
50 }
51}
52
53func mySiteHandler(w http.ResponseWriter, r *http.Request) {
54 authUser := "alex"
55 files, _ := ioutil.ReadDir(path.Join(userFilesPath, authUser))
56 for _, file := range files {
57 fmt.Fprintf(w, "%s\n", file.Name())
58 }
59}
60
61func main() {
62 http.HandleFunc("/", indexHandler)
63 http.HandleFunc("/my_site", mySiteHandler)
64 // go serve gemini
65 // go serve http
66 log.Fatal(http.ListenAndServe(":8080", nil))
67}