hook2xmpp/main.go

79 lines
1.7 KiB
Go
Raw Normal View History

2017-06-09 10:55:47 +02:00
package main
import (
"flag"
"net/http"
"os"
"os/signal"
"syscall"
2019-02-13 03:24:38 +01:00
"dev.sum7.eu/genofire/golang-lib/file"
2019-02-13 03:55:07 +01:00
"github.com/bdlm/log"
2019-07-15 23:57:26 +02:00
"gosrc.io/xmpp"
2019-02-13 03:24:38 +01:00
2019-08-08 15:49:25 +02:00
_ "dev.sum7.eu/sum7/hook2xmpp/circleci"
_ "dev.sum7.eu/sum7/hook2xmpp/git"
_ "dev.sum7.eu/sum7/hook2xmpp/gitlab"
_ "dev.sum7.eu/sum7/hook2xmpp/grafana"
_ "dev.sum7.eu/sum7/hook2xmpp/prometheus"
"dev.sum7.eu/sum7/hook2xmpp/runtime"
2017-06-09 10:55:47 +02:00
)
2019-07-15 23:57:26 +02:00
var config = runtime.Config{}
2017-06-09 10:55:47 +02:00
func main() {
configFile := "config.conf"
flag.StringVar(&configFile, "config", configFile, "path of configuration file")
flag.Parse()
2019-07-15 23:57:26 +02:00
if err := file.ReadTOML(configFile, &config); err != nil {
2019-02-14 12:55:09 +01:00
log.WithField("tip", "maybe call me with: hook2xmpp--config /etc/hook2xmpp.conf").Panicf("error on read config: %s", err)
2017-06-16 14:55:07 +02:00
}
2019-02-14 01:32:27 +01:00
log.SetLevel(config.LogLevel)
2019-07-15 23:57:26 +02:00
router := xmpp.NewRouter()
var err error
client, err := xmpp.NewClient(xmpp.Config{
Address: config.XMPP.Address,
Jid: config.XMPP.JID,
Password: config.XMPP.Password,
}, router)
2017-06-09 10:55:47 +02:00
if err != nil {
2019-02-13 04:52:00 +01:00
log.Panicf("error on startup xmpp client: %s", err)
2017-06-09 10:55:47 +02:00
}
2019-07-15 23:57:26 +02:00
cm := xmpp.NewStreamManager(client, postStartup)
go func() {
err := cm.Run()
log.Panic("closed connection:", err)
}()
2019-02-13 03:24:38 +01:00
for hookType, getHandler := range runtime.HookRegister {
hooks, ok := config.Hooks[hookType]
if ok {
2019-02-13 04:52:00 +01:00
http.HandleFunc("/"+hookType, getHandler(client, hooks))
2019-02-13 03:24:38 +01:00
}
}
2017-06-09 15:51:04 +02:00
2017-06-09 10:55:47 +02:00
srv := &http.Server{
Addr: config.WebserverBind,
}
go func() {
2019-03-09 00:09:45 +01:00
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
2017-06-09 10:55:47 +02:00
panic(err)
}
}()
// Wait for system signal
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
2019-07-15 23:57:26 +02:00
closeXMPP(client)
2017-06-09 14:27:06 +02:00
2017-06-09 10:55:47 +02:00
srv.Close()
2019-02-13 03:55:07 +01:00
log.Infof("closed by receiving: %s", sig)
2017-06-09 10:55:47 +02:00
}