2019-12-02 14:31:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
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"
|
2019-12-02 16:24:19 +00:00
|
|
|
|
2019-12-02 14:31:00 +00:00
|
|
|
"git.sr.ht/~emersion/koushin"
|
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
|
|
|
|
|
|
|
_ "git.sr.ht/~emersion/koushin/plugins/base"
|
2019-12-02 14:31:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-12-04 17:30:01 +00:00
|
|
|
var options koushin.Options
|
2020-01-08 11:01:50 +00:00
|
|
|
var addr string
|
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")
|
2019-12-04 17:30:01 +00:00
|
|
|
|
|
|
|
flag.Usage = func() {
|
2020-01-20 11:00:04 +00:00
|
|
|
fmt.Fprintf(flag.CommandLine.Output(), "usage: koushin [options...] <upstream server...>\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
|
|
|
}
|
|
|
|
|
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-01-08 10:02:13 +00:00
|
|
|
s, err := koushin.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-01-08 10:02:13 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-01-08 11:01:50 +00:00
|
|
|
e.Logger.Fatal(e.Start(addr))
|
2019-12-02 14:31:00 +00:00
|
|
|
}
|