all repos — flounder @ 3ec267fae76b5f2ad46f32d84fa27ee67dc2673b

A small site builder for the Gemini protocol

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	if c.HttpsEnabled {
39		_, err1 := os.Stat(c.TLSCertFile)
40		_, err2 := os.Stat(c.TLSKeyFile)
41		if os.IsNotExist(err1) || os.IsNotExist(err2) {
42			log.Fatal("Keyfile or certfile does not exist.")
43		}
44	}
45
46	initializeDB()
47
48	cookie := generateCookieKeyIfDNE()
49	SessionStore = sessions.NewCookieStore(cookie)
50
51	// handle background tasks
52	s1 := gocron.NewScheduler(time.UTC)
53	if c.AnalyticsDBFile != "" {
54		s1.Every(1).Hour().Do(dumpLogs) // TODO Dont do on start?
55	}
56
57	switch args[0] {
58	case "serve":
59		s1.StartAsync()
60		wg := new(sync.WaitGroup)
61		wg.Add(2)
62		go func() {
63			runHTTPServer()
64			wg.Done()
65		}()
66		go func() {
67			runGeminiServer()
68			wg.Done()
69		}()
70		wg.Wait()
71	case "admin":
72		runAdminCommand()
73	case "dumplogs":
74		dumpLogs()
75	}
76}