forked from bangumi/github-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
98 lines (82 loc) · 2.12 KB
/
Copy pathmain.go
File metadata and controls
98 lines (82 loc) · 2.12 KB
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"
"os"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
_ "github.com/lib/pq"
"github.com/palantir/go-githubapp/githubapp"
"github.com/rs/zerolog"
"github.com/samber/lo"
"github-bot/config"
"github-bot/ent"
)
var logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
var version = "development"
func main() {
client, err := ent.Open("postgres", config.PgOption)
if err != nil {
logger.Fatal().Err(err).Msg("failed opening connection to pg")
}
defer client.Close()
// Run the auto migration tool.
if err := client.Schema.Create(context.Background()); err != nil {
logger.Fatal().Err(err).Msg("failed creating schema resources")
}
// Echo instance
e := echo.New()
e.HideBanner = true
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
err := next(c)
if err != nil {
if _, ok := err.(*echo.HTTPError); !ok {
logger.Err(err).Msg("internal error")
}
}
return err
}
})
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Set("x-version", version)
return next(c)
}
})
// Middleware
e.Use(middleware.Recover())
appClient := getGithubAppClient()
h := PRHandle{
ent: client,
app: appClient,
g: lo.Must(appClient.NewInstallationClient(config.InstallationID)),
}
// Routes
e.POST("/event-pr", h.Handle)
e.GET("/", h.Index)
h.setupGithubOAuth(e)
h.setupBangumiOAuth(e)
port := config.HTTPPost
if port == "" {
port = "8090"
}
host := config.HTTPHost
if host == "" {
host = "127.0.0.1"
}
// Start server
e.Logger.Fatal(e.Start(host + ":" + port))
}
func getGithubAppClient() githubapp.ClientCreator {
return lo.Must(githubapp.NewDefaultCachingClientCreator(githubapp.Config{
V3APIURL: "https://api.github.com/",
App: struct {
IntegrationID int64 `yaml:"integration_id" json:"integrationId"`
WebhookSecret string `yaml:"webhook_secret" json:"webhookSecret"`
PrivateKey string `yaml:"private_key" json:"privateKey"`
}{
IntegrationID: 289933,
PrivateKey: config.GitHubAppPrivateKey,
},
}))
}