-
Notifications
You must be signed in to change notification settings - Fork 82
/
main.go
93 lines (73 loc) · 2.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"context"
"flag"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"slices"
"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/notification/all"
"github.com/ncarlier/webhookd/pkg/server"
"github.com/ncarlier/webhookd/pkg/version"
"github.com/ncarlier/webhookd/pkg/worker"
)
const envPrefix = "WHD"
func main() {
conf := &config.Config{}
configflag.Bind(conf, envPrefix)
flag.Parse()
if *version.ShowVersion {
version.Print()
os.Exit(0)
}
if conf.Hook.LogDir == "" {
conf.Hook.LogDir = os.TempDir()
}
if err := conf.Validate(); err != nil {
log.Fatal("invalid configuration:", err)
}
logger.Configure(conf.Log.Format, conf.Log.Level)
logger.HookOutputEnabled = slices.Contains(conf.Log.Modules, "hook")
logger.RequestOutputEnabled = slices.Contains(conf.Log.Modules, "http")
conf.ManageDeprecatedFlags(envPrefix)
slog.Debug("starting webhookd server...")
srv := server.NewServer(conf)
// Configure notification
if err := notification.Init(conf.Notification.URI); err != nil {
slog.Error("unable to create notification channel", "err", err)
}
// Start the dispatcher.
slog.Debug("starting the dispatcher...", "workers", conf.Hook.Workers)
worker.StartDispatcher(conf.Hook.Workers)
done := make(chan bool)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
go func() {
<-quit
slog.Debug("server is shutting down...")
api.Shutdown()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
slog.Error("could not gracefully shutdown the server", "err", err)
}
close(done)
}()
api.Start()
slog.Info("server started", "addr", conf.ListenAddr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error("unable to start the server", "addr", conf.ListenAddr, "err", err)
os.Exit(1)
}
<-done
slog.Debug("server stopped")
}