mirror of
https://github.com/ncarlier/webhookd.git
synced 2025-04-06 11:50:14 +00:00
87 lines
2.0 KiB
Go
87 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/ncarlier/webhookd/pkg/api"
|
|
"github.com/ncarlier/webhookd/pkg/config"
|
|
configflag "github.com/ncarlier/webhookd/pkg/config/flag"
|
|
"github.com/ncarlier/webhookd/pkg/logger"
|
|
"github.com/ncarlier/webhookd/pkg/notification"
|
|
"github.com/ncarlier/webhookd/pkg/server"
|
|
"github.com/ncarlier/webhookd/pkg/version"
|
|
"github.com/ncarlier/webhookd/pkg/worker"
|
|
)
|
|
|
|
func main() {
|
|
conf := &config.Config{}
|
|
configflag.Bind(conf, "WHD")
|
|
|
|
flag.Parse()
|
|
|
|
if *version.ShowVersion {
|
|
version.Print()
|
|
os.Exit(0)
|
|
}
|
|
|
|
if conf.HookLogOutput {
|
|
logger.Init(conf.LogLevel, "out")
|
|
} else {
|
|
logger.Init(conf.LogLevel)
|
|
}
|
|
|
|
if conf.HookLogDir == "" {
|
|
conf.HookLogDir = os.TempDir()
|
|
}
|
|
|
|
if err := conf.Validate(); err != nil {
|
|
logger.Error.Fatal("invalid configuration:", err)
|
|
}
|
|
|
|
logger.Debug.Println("starting webhookd server...")
|
|
|
|
srv := server.NewServer(conf)
|
|
|
|
// Configure notification
|
|
if err := notification.Init(conf.NotificationURI); err != nil {
|
|
logger.Error.Fatalf("unable to create notification channel: %v\n", err)
|
|
}
|
|
|
|
// Start the dispatcher.
|
|
logger.Debug.Printf("starting the dispatcher with %d workers...\n", conf.NbWorkers)
|
|
worker.StartDispatcher(conf.NbWorkers)
|
|
|
|
done := make(chan bool)
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
<-quit
|
|
logger.Debug.Println("server is shutting down...")
|
|
api.Shutdown()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
if err := srv.Shutdown(ctx); err != nil {
|
|
logger.Error.Fatalf("could not gracefully shutdown the server: %v\n", err)
|
|
}
|
|
close(done)
|
|
}()
|
|
|
|
logger.Info.Println("server is ready to handle requests at", conf.ListenAddr)
|
|
api.Start()
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
logger.Error.Fatalf("could not listen on %s : %v\n", conf.ListenAddr, err)
|
|
}
|
|
|
|
<-done
|
|
logger.Debug.Println("server stopped")
|
|
}
|