main.go (view raw)
1package main
2
3import (
4 "flag"
5 "fmt"
6 "io/ioutil"
7 "log"
8 "os"
9 "path"
10 "path/filepath"
11 "strings"
12 "sync"
13)
14
15var c Config // global var to hold static configuration
16
17const ( // todo make configurable
18 userFilesPath = "./files"
19)
20
21type File struct {
22 Creator string
23 Name string
24 UpdatedTime string
25}
26
27func getUsers() ([]string, error) {
28 return []string{"me", "other guy"}, nil
29}
30
31/// Perform some checks to make sure the file is OK
32func checkIfValidFile(filename string, fileBytes []byte) error {
33 ext := strings.ToLower(path.Ext(filename))
34 found := false
35 for _, mimetype := range c.OkExtensions {
36 if ext == mimetype {
37 found = true
38 }
39 }
40 if !found {
41 return fmt.Errorf("Invalid file extension: %s", ext)
42 }
43 if len(fileBytes) > c.MaxFileSize {
44 return fmt.Errorf("File too large. File was %s bytes, Max file size is %s", len(fileBytes), c.MaxFileSize)
45 }
46 return nil
47}
48
49func getIndexFiles() ([]*File, error) { // cache this function
50 result := []*File{}
51 err := filepath.Walk(userFilesPath, func(path string, info os.FileInfo, err error) error {
52 if err != nil {
53 log.Printf("Failure accessing a path %q: %v\n", path, err)
54 return err // think about
55 }
56 // make this do what it should
57 result = append(result, &File{
58 Name: info.Name(),
59 Creator: "alex",
60 UpdatedTime: "123123",
61 })
62 return nil
63 })
64 if err != nil {
65 return nil, err
66 }
67 // sort
68 // truncate
69 return result, nil
70} // todo clean up paths
71
72func getUserFiles(user string) ([]*File, error) {
73 result := []*File{}
74 files, err := ioutil.ReadDir(path.Join(userFilesPath, user))
75 if err != nil {
76 return nil, err
77 }
78 for _, file := range files {
79 result = append(result, &File{
80 Name: file.Name(),
81 Creator: user,
82 UpdatedTime: "123123",
83 })
84 }
85 return result, nil
86}
87
88func main() {
89 configPath := flag.String("c", "flounder.toml", "path to config file")
90 var err error
91 c, err = getConfig(*configPath)
92 if err != nil {
93 log.Fatal(err)
94 }
95
96 wg := new(sync.WaitGroup)
97 wg.Add(2)
98 go func() {
99 runHTTPServer()
100 wg.Done()
101 }()
102 go func() {
103 runGeminiServer()
104 wg.Done()
105 }()
106 wg.Wait()
107}