-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (62 loc) · 1.39 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"time"
"github.com/go-pa/fenv"
"github.com/some-programs/call-webhook-service/internal/webhook"
)
func main() {
rand.Seed(time.Now().UnixNano())
log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
var flags webhook.Flags
flags.Register()
fenv.CommandLinePrefix("CALL_WEBHOOK_")
fenv.MustParse()
flag.Parse()
if err := flags.Process(); err != nil {
fmt.Println(err)
os.Exit(1)
}
if flags.PrintConfig {
data, err := json.MarshalIndent(flags, "", " ")
if err != nil {
log.Fatal(err)
}
log.Printf("configuration: %s", string(data))
}
ctx := context.Background()
if flags.MetricsAddr != "" {
webhook.AddMetricsHandlers(ctx, nil)
webhook.AddDebugHandlers(ctx, nil, flags.PostTimeout)
srv := http.Server{Addr: flags.MetricsAddr}
go func() {
for {
select {
case <-ctx.Done():
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("HTTP server Shutdown: %v", err)
}
return
}
}
}()
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("HTTP server ListenAndServe: %v", err)
}
}()
}
c := webhook.CallWebhookWorker{}
if err := c.Start(ctx, flags); err != nil {
log.Fatal(err)
}
}