-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
98 lines (82 loc) · 2.77 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
94
95
96
97
98
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/bytebase/relay/hook"
"github.com/bytebase/relay/sink"
"github.com/flamego/flamego"
flag "github.com/spf13/pflag"
)
const (
// greetingBanner is the greeting banner.
// http://patorjk.com/software/taag/#p=display&f=ANSI%20Shadow&t=Relay
greetingBanner = `
██████╗ ███████╗██╗ █████╗ ██╗ ██╗
██╔══██╗██╔════╝██║ ██╔══██╗╚██╗ ██╔╝
██████╔╝█████╗ ██║ ███████║ ╚████╔╝
██╔══██╗██╔══╝ ██║ ██╔══██║ ╚██╔╝
██║ ██║███████╗███████╗██║ ██║ ██║
╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═╝
___________________________________________________________________________________________
`
// byeBanner is the bye banner.
// http://patorjk.com/software/taag/#p=display&f=ANSI%20Shadow&t=BYE
byeBanner = `
██████╗ ██╗ ██╗███████╗
██╔══██╗╚██╗ ██╔╝██╔════╝
██████╔╝ ╚████╔╝ █████╗
██╔══██╗ ╚██╔╝ ██╔══╝
██████╔╝ ██║ ███████╗
╚═════╝ ╚═╝ ╚══════╝
`
)
var (
address string
)
func init() {
flag.StringVar(&address, "address", os.Getenv("RELAY_ADDR"), "The host:port address where Relay runs, default to localhost:5678")
}
func main() {
flag.Parse()
h := "localhost"
p := 5678
if address != "" {
fields := strings.SplitN(address, ":", 2)
h = fields[0]
port, err := strconv.Atoi(fields[1])
if err != nil {
fmt.Printf("Port is not a number: %s\n", fields[1])
os.Exit(1)
}
p = port
}
f := flamego.Classic()
github := hook.NewGitHub()
lark := sink.NewLark()
hook.Mount(f, "/github", github, []sink.Sinker{lark})
gerrit := hook.NewGerrit()
bytebase := sink.NewBytebase()
hook.Mount(f, "/gerrit", gerrit, []sink.Sinker{bytebase})
// Setup signal handlers.
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
// Trigger graceful shutdown on SIGINT or SIGTERM.
// The default signal sent by the `kill` command is SIGTERM,
// which is taken as the graceful shutdown signal for many systems, eg., Kubernetes, Gunicorn.
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
f.Stop()
cancel()
}()
fmt.Print(greetingBanner)
f.Run(h, p)
fmt.Print(byeBanner)
// Wait for CTRL-C.
<-ctx.Done()
}