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