alps/cmd/koushin/main.go
Simon Ser ad1d2ee7f4
Implement plugin/template reload on SIGUSR1
There's no way around having a global mutex, because we need to update
the HTTP routes when reloading plugins. During reload we need to lock
the whole server.

Closes: https://todo.sr.ht/~sircmpwn/koushin/43
2020-01-08 11:50:29 +01:00

59 lines
1.1 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"git.sr.ht/~emersion/koushin"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
_ "git.sr.ht/~emersion/koushin/plugins/base"
)
func main() {
var options koushin.Options
flag.StringVar(&options.Theme, "theme", "", "default theme")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "usage: koushin [options...] <IMAP URL> [SMTP URL]\n")
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() < 1 || flag.NArg() > 2 {
flag.Usage()
return
}
options.IMAPURL = flag.Arg(0)
options.SMTPURL = flag.Arg(1)
e := echo.New()
if l, ok := e.Logger.(*log.Logger); ok {
l.SetHeader("${time_rfc3339} ${level}")
}
s, err := koushin.New(e, &options)
if err != nil {
e.Logger.Fatal(err)
}
e.Use(middleware.Recover())
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(":1323"))
}