all repos — flounder @ 422f8d44a496cf7d701c731615c8c6c043f3a9da

A small site builder for the Gemini protocol

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		wg.Add(2)
53		go func() {
54			runHTTPServer()
55			wg.Done()
56		}()
57		go func() {
58			runGeminiServer()
59			wg.Done()
60		}()
61		wg.Wait()
62	case "admin":
63		runAdminCommand()
64	}
65}