-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (60 loc) · 1.76 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 (
"net/http"
"os"
"time"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/palantir/go-githubapp/githubapp"
"github.com/rs/zerolog"
)
type Config struct {
Server struct {
Address string `default:"0.0.0.0:8080"`
Timeout time.Duration `default:"5s"`
}
Github struct {
V3APIURL string `default:"https://api.github.com"`
App struct {
IntegrationID int64 `split_words:"true"`
PrivateKey string `split_words:"true"`
}
}
}
func main() {
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
zerolog.DefaultContextLogger = &logger
_ = godotenv.Load()
var c Config
if err := envconfig.Process("app", &c); err != nil {
logger.Fatal().Err(err).Msg("failed to read envconfig")
}
ghConfig := newAppConfig(c)
cc, err := githubapp.NewDefaultCachingClientCreator(
ghConfig,
githubapp.WithClientUserAgent("check-pls/0.0.0"),
githubapp.WithClientTimeout(c.Server.Timeout),
)
if err != nil {
logger.Fatal().Err(err).Msg("failed to create client creator")
}
ph, crh := initHandlers(cc)
webhookHandler := githubapp.NewDefaultEventDispatcher(ghConfig, ph, crh)
http.Handle(githubapp.DefaultWebhookRoute, webhookHandler)
logger.Info().Msgf("Starting server on %s...", c.Server.Address)
err = http.ListenAndServe(c.Server.Address, nil)
if err != nil {
logger.Fatal().Err(err).Msg("error starting server")
}
}
func newAppConfig(c Config) githubapp.Config {
gh := c.Github
var ghc githubapp.Config
ghc.V3APIURL = gh.V3APIURL
ghc.App.IntegrationID = gh.App.IntegrationID
ghc.App.PrivateKey = gh.App.PrivateKey
return ghc
}
func initHandlers(cc githubapp.ClientCreator) (*PushHandler, *CheckRunHandler) {
return &PushHandler{ClientCreator: cc}, &CheckRunHandler{ClientCreator: cc}
}