main.go (view raw)
1package main
2
3import (
4 "flag"
5 "fmt"
6 "github.com/go-co-op/gocron"
7 "github.com/gorilla/sessions"
8 "io"
9 "log"
10 "os"
11 "sync"
12 "time"
13)
14
15var c Config // global var to hold static configuration
16
17func main() {
18 configPath := flag.String("c", "flounder.toml", "path to config file") // doesnt work atm
19 flag.Parse()
20 args := flag.Args()
21 if len(args) < 1 {
22 fmt.Println("expected 'admin' or 'serve' subcommand")
23 os.Exit(1)
24 }
25
26 var err error
27 c, err = getConfig(*configPath)
28 if err != nil {
29 log.Fatal(err)
30 }
31 logFile, err := os.OpenFile(c.LogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
32 if err != nil {
33 panic(err)
34 }
35 mw := io.MultiWriter(os.Stdout, logFile)
36 log.SetOutput(mw)
37
38 initializeDB()
39
40 cookie := generateCookieKeyIfDNE()
41 SessionStore = sessions.NewCookieStore(cookie)
42
43 // handle background tasks
44 s1 := gocron.NewScheduler(time.UTC)
45 if c.AnalyticsDBFile != "" {
46 s1.Every(5).Minute().Do(dumpLogs)
47 }
48
49 // load domains in memory
50 refreshDomainMap()
51
52 switch args[0] {
53 case "serve":
54 s1.StartAsync()
55 wg := new(sync.WaitGroup)
56 wg.Add(2)
57 go func() {
58 runHTTPServer()
59 wg.Done()
60 }()
61 go func() {
62 runGeminiServer()
63 wg.Done()
64 }()
65 wg.Wait()
66 case "admin":
67 runAdminCommand()
68 case "dumplogs":
69 dumpLogs()
70 }
71}