main.go (view raw)
1package main
2
3import (
4 "database/sql"
5 "flag"
6 "github.com/gorilla/sessions"
7 "io/ioutil"
8 "log"
9 "os"
10 "path"
11 "path/filepath"
12 "sort"
13 "sync"
14 "time"
15)
16
17var c Config // global var to hold static configuration
18
19type File struct {
20 Creator string
21 Name string
22 UpdatedTime time.Time
23 TimeAgo string
24}
25
26func getUsers() ([]string, error) {
27 rows, err := DB.Query(`SELECT username from user`)
28 if err != nil {
29 return nil, err
30 }
31 var users []string
32 for rows.Next() {
33 var user string
34 err = rows.Scan(&user)
35 if err != nil {
36 return nil, err
37 }
38 users = append(users, user)
39 }
40 return users, nil
41}
42
43func getIndexFiles() ([]*File, error) { // cache this function
44 result := []*File{}
45 err := filepath.Walk(c.FilesDirectory, func(thepath string, info os.FileInfo, err error) error {
46 if err != nil {
47 log.Printf("Failure accessing a path %q: %v\n", thepath, err)
48 return err // think about
49 }
50 // make this do what it should
51 if !info.IsDir() {
52 creatorFolder, _ := path.Split(thepath)
53 updatedTime := info.ModTime()
54 result = append(result, &File{
55 Name: info.Name(),
56 Creator: path.Base(creatorFolder),
57 UpdatedTime: updatedTime,
58 TimeAgo: timeago(&updatedTime),
59 })
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 SessionStore = sessions.NewCookieStore([]byte(c.CookieStoreKey))
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}