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