forked from ncarlier/webhookd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (68 loc) · 1.98 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
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")
}