2019-12-02 14:31:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-23 17:30:29 +00:00
|
|
|
"context"
|
2019-12-04 17:30:01 +00:00
|
|
|
"flag"
|
2019-12-02 16:24:19 +00:00
|
|
|
"fmt"
|
2020-01-08 10:02:13 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2020-10-23 17:30:29 +00:00
|
|
|
"time"
|
2019-12-02 16:24:19 +00:00
|
|
|
|
2020-11-18 18:31:43 +00:00
|
|
|
"git.sr.ht/~migadu/alps"
|
2020-05-20 17:05:05 +00:00
|
|
|
"github.com/fernet/fernet-go"
|
2019-12-04 17:30:01 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2019-12-02 14:31:00 +00:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2019-12-04 17:30:01 +00:00
|
|
|
"github.com/labstack/gommon/log"
|
2019-12-16 11:51:42 +00:00
|
|
|
|
2020-11-18 18:31:43 +00:00
|
|
|
_ "git.sr.ht/~migadu/alps/plugins/base"
|
|
|
|
_ "git.sr.ht/~migadu/alps/plugins/caldav"
|
|
|
|
_ "git.sr.ht/~migadu/alps/plugins/carddav"
|
|
|
|
_ "git.sr.ht/~migadu/alps/plugins/lua"
|
|
|
|
_ "git.sr.ht/~migadu/alps/plugins/viewhtml"
|
|
|
|
_ "git.sr.ht/~migadu/alps/plugins/viewtext"
|
2019-12-02 14:31:00 +00:00
|
|
|
)
|
|
|
|
|
2021-09-10 08:12:38 +00:00
|
|
|
var themesPath = "./themes"
|
|
|
|
|
2019-12-02 14:31:00 +00:00
|
|
|
func main() {
|
2020-05-20 17:05:05 +00:00
|
|
|
var (
|
|
|
|
addr string
|
|
|
|
loginKey string
|
|
|
|
options alps.Options
|
|
|
|
)
|
2019-12-04 17:30:01 +00:00
|
|
|
flag.StringVar(&options.Theme, "theme", "", "default theme")
|
2020-01-08 11:01:50 +00:00
|
|
|
flag.StringVar(&addr, "addr", ":1323", "listening address")
|
2020-02-11 17:39:52 +00:00
|
|
|
flag.BoolVar(&options.Debug, "debug", false, "enable debug logs")
|
2020-05-20 17:05:05 +00:00
|
|
|
flag.StringVar(&loginKey, "login-key", "", "Fernet key for login persistence")
|
2019-12-04 17:30:01 +00:00
|
|
|
|
|
|
|
flag.Usage = func() {
|
2020-05-13 12:07:44 +00:00
|
|
|
fmt.Fprintf(flag.CommandLine.Output(), "usage: alps [options...] <upstream servers...>\n")
|
2019-12-04 17:30:01 +00:00
|
|
|
flag.PrintDefaults()
|
2019-12-02 16:24:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 17:30:01 +00:00
|
|
|
flag.Parse()
|
2019-12-02 16:24:19 +00:00
|
|
|
|
2020-01-20 11:00:04 +00:00
|
|
|
options.Upstreams = flag.Args()
|
|
|
|
if len(options.Upstreams) == 0 {
|
2019-12-04 17:30:01 +00:00
|
|
|
flag.Usage()
|
|
|
|
return
|
2019-12-03 14:21:59 +00:00
|
|
|
}
|
2021-09-10 08:12:38 +00:00
|
|
|
options.ThemesPath = themesPath
|
2019-12-03 14:21:59 +00:00
|
|
|
|
2020-05-20 17:05:05 +00:00
|
|
|
if loginKey != "" {
|
|
|
|
fernetKey, err := fernet.DecodeKey(loginKey)
|
|
|
|
if err != nil {
|
|
|
|
flag.Usage()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
options.LoginKey = fernetKey
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:30:01 +00:00
|
|
|
e := echo.New()
|
2020-01-16 23:22:11 +00:00
|
|
|
e.HideBanner = true
|
2019-12-04 17:30:01 +00:00
|
|
|
if l, ok := e.Logger.(*log.Logger); ok {
|
|
|
|
l.SetHeader("${time_rfc3339} ${level}")
|
|
|
|
}
|
2020-05-13 12:07:44 +00:00
|
|
|
s, err := alps.New(e, &options)
|
2020-01-08 09:38:33 +00:00
|
|
|
if err != nil {
|
2019-12-04 17:30:01 +00:00
|
|
|
e.Logger.Fatal(err)
|
|
|
|
}
|
2019-12-02 14:31:00 +00:00
|
|
|
e.Use(middleware.Recover())
|
2020-02-25 14:14:55 +00:00
|
|
|
if options.Debug {
|
|
|
|
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
|
|
|
Format: "${time_rfc3339} method=${method}, uri=${uri}, status=${status}\n",
|
|
|
|
}))
|
2020-02-11 17:39:52 +00:00
|
|
|
e.Logger.SetLevel(log.DEBUG)
|
|
|
|
}
|
|
|
|
|
2020-10-23 17:30:29 +00:00
|
|
|
go e.Start(addr)
|
|
|
|
|
2020-01-08 10:02:13 +00:00
|
|
|
sigs := make(chan os.Signal, 1)
|
2020-10-23 17:30:29 +00:00
|
|
|
signal.Notify(sigs, syscall.SIGUSR1, syscall.SIGINT)
|
|
|
|
|
|
|
|
for sig := range sigs {
|
|
|
|
if sig == syscall.SIGUSR1 {
|
2020-01-08 10:02:13 +00:00
|
|
|
if err := s.Reload(); err != nil {
|
|
|
|
e.Logger.Errorf("Failed to reload server: %v", err)
|
|
|
|
}
|
2020-10-23 17:30:29 +00:00
|
|
|
} else if sig == syscall.SIGINT {
|
|
|
|
break
|
2020-01-08 10:02:13 +00:00
|
|
|
}
|
2020-10-23 17:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithDeadline(context.Background(),
|
|
|
|
time.Now().Add(30*time.Second))
|
|
|
|
e.Shutdown(ctx)
|
|
|
|
cancel()
|
2020-01-08 10:02:13 +00:00
|
|
|
|
2020-10-29 19:18:36 +00:00
|
|
|
s.Close()
|
2019-12-02 14:31:00 +00:00
|
|
|
}
|