aboutsummaryrefslogtreecommitdiff
path: root/cmd/alps/main.go
blob: 4c358d50458b7d9aaa4278c3b619e75561ebe89d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main

import (
	"flag"
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"git.sr.ht/~emersion/alps"
	"github.com/fernet/fernet-go"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"github.com/labstack/gommon/log"

	_ "git.sr.ht/~emersion/alps/plugins/base"
	_ "git.sr.ht/~emersion/alps/plugins/caldav"
	_ "git.sr.ht/~emersion/alps/plugins/carddav"
	_ "git.sr.ht/~emersion/alps/plugins/lua"
	_ "git.sr.ht/~emersion/alps/plugins/viewhtml"
	_ "git.sr.ht/~emersion/alps/plugins/viewtext"
)

func main() {
	var (
		addr     string
		loginKey string
		options  alps.Options
	)
	flag.StringVar(&options.Theme, "theme", "", "default theme")
	flag.StringVar(&addr, "addr", ":1323", "listening address")
	flag.BoolVar(&options.Debug, "debug", false, "enable debug logs")
	flag.StringVar(&loginKey, "login-key", "", "Fernet key for login persistence")

	flag.Usage = func() {
		fmt.Fprintf(flag.CommandLine.Output(), "usage: alps [options...] <upstream servers...>\n")
		flag.PrintDefaults()
	}

	flag.Parse()

	options.Upstreams = flag.Args()
	if len(options.Upstreams) == 0 {
		flag.Usage()
		return
	}

	if loginKey != "" {
		fernetKey, err := fernet.DecodeKey(loginKey)
		if err != nil {
			flag.Usage()
			return
		}
		options.LoginKey = fernetKey
	}

	e := echo.New()
	e.HideBanner = true
	if l, ok := e.Logger.(*log.Logger); ok {
		l.SetHeader("${time_rfc3339} ${level}")
	}
	s, err := alps.New(e, &options)
	if err != nil {
		e.Logger.Fatal(err)
	}
	e.Use(middleware.Recover())
	if options.Debug {
		e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
			Format: "${time_rfc3339} method=${method}, uri=${uri}, status=${status}\n",
		}))
	}

	if options.Debug {
		e.Logger.SetLevel(log.DEBUG)
	}

	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGUSR1)
	go func() {
		for range sigs {
			if err := s.Reload(); err != nil {
				e.Logger.Errorf("Failed to reload server: %v", err)
			}
		}
	}()

	e.Logger.Fatal(e.Start(addr))
}