/
run.go
95 lines (84 loc) · 2.98 KB
/
run.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
// Copyright © 2017 Syndesis Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
gohttp "net/http"
"os"
"os/signal"
"sync"
"syscall"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.uber.org/zap"
"github.com/syndesisio/pure-bot/pkg/http"
"github.com/syndesisio/pure-bot/pkg/webhook"
)
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
Short: "Runs pure-bot",
Long: `Runs pure-bot.`,
Run: func(cmd *cobra.Command, args []string) {
githubHandler, err := webhook.NewGithubHTTPHandler(botConfig.Webhook, botConfig, logger.Named("github"))
if err != nil {
logger.Fatal("failed to create webhook handler", zap.Error(err))
}
zenhubHandler, err := webhook.NewZenhubHTTPHandler(botConfig.Webhook, botConfig, logger.Named("zenhub"))
if err != nil {
logger.Fatal("failed to create webhook handler", zap.Error(err))
}
// request dispatching
mux := gohttp.NewServeMux()
mux.HandleFunc("/", githubHandler)
mux.HandleFunc("/zenhub", zenhubHandler)
// server
srv := http.New(botConfig.HTTP, mux)
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := srv.Start(); err != nil {
if errors.Cause(err) != gohttp.ErrServerClosed {
logger.Fatal("web server failed", zap.Error(err))
}
}
}()
go func() {
<-c
if err := srv.Stop(); err != nil {
logger.Fatal("failed to stop web server", zap.Error(err))
}
}()
wg.Wait()
},
}
func init() {
RootCmd.AddCommand(runCmd)
runCmd.Flags().String("webhook-secret", "", "Secret to validate incoming webhooks")
v.BindPFlag("webhook.secret", runCmd.Flags().Lookup("webhook-secret"))
runCmd.Flags().String("bind-address", "", "Address to bind to")
v.BindPFlag("http.address", runCmd.Flags().Lookup("bind-address"))
runCmd.Flags().Int("bind-port", 8080, "Port to bind to")
v.BindPFlag("http.port", runCmd.Flags().Lookup("bind-port"))
runCmd.Flags().String("tls-cert", "", "TLS cert file")
v.BindPFlag("http.tlsCert", runCmd.Flags().Lookup("tls-cert"))
runCmd.Flags().String("tls-key", "", "TLS key file")
v.BindPFlag("http.tlsKey", runCmd.Flags().Lookup("tls-key"))
runCmd.Flags().Int("github-app-id", 0, "GitHub App ID")
v.BindPFlag("github.appId", runCmd.Flags().Lookup("github-app-id"))
runCmd.Flags().String("github-app-private-key", "", "GitHub app private key file")
v.BindPFlag("github.privateKey", runCmd.Flags().Lookup("github-app-private-key"))
}