This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
76 lines (63 loc) · 1.4 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
package main
import (
"context"
"errors"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/urcomputeringpal/kubevalidator/validator"
)
func runWithContext(ctx context.Context) error {
port, ok := os.LookupEnv("PORT")
if !ok {
port = "8080"
}
portInt, _ := strconv.Atoi(port)
webhookSecret, ok := os.LookupEnv("WEBHOOK_SECRET")
if !ok {
return errors.New("WEBHOOK_SECRET required")
}
appID, ok := os.LookupEnv("APP_ID")
if !ok {
return errors.New("APP_ID required")
}
appIDInt, _ := strconv.Atoi(appID)
privateKeyFile, ok := os.LookupEnv("PRIVATE_KEY_FILE")
if !ok {
return errors.New("PRIVATE_KEY_FILE required")
}
v := &validator.Server{
Port: portInt,
WebhookSecret: webhookSecret,
AppID: appIDInt,
PrivateKeyFile: privateKeyFile,
}
return v.Run(ctx)
}
func cancelOnInterrupt(ctx context.Context, f context.CancelFunc) {
term := make(chan os.Signal)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
for {
select {
case <-term:
log.Println("Received SIGTERM, exiting gracefully...")
f()
os.Exit(0)
case <-ctx.Done():
os.Exit(0)
}
}
}
func run() error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
go cancelOnInterrupt(ctx, cancelFunc)
return runWithContext(ctx)
}
func main() {
if err := run(); err != nil && err != context.Canceled && err != context.DeadlineExceeded {
panic(err)
}
}