-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (92 loc) · 2.62 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
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/gregjones/httpcache"
"github.com/palantir/go-githubapp/githubapp"
"github.com/rcrowley/go-metrics"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/FrangipaneTeam/crown/handlers"
"github.com/FrangipaneTeam/crown/pkg/config"
"github.com/FrangipaneTeam/crown/pkg/db"
"github.com/FrangipaneTeam/crown/pkg/tracker"
)
// var Db *db.DB
func main() {
config, err := config.ReadConfig("config.yaml")
if err != nil {
panic(err)
}
// SRC https://github.com/rs/zerolog#add-file-and-line-number-to-log
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
return file + ":" + strconv.Itoa(line)
}
var logger zerolog.Logger
if config.Log.Human {
logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}).With().Caller().Logger()
} else {
logger = zerolog.New(os.Stdout).With().Timestamp().Caller().Logger()
}
zerolog.DefaultContextLogger = &logger
level, err := zerolog.ParseLevel(config.Log.Level)
if err != nil {
logger.Fatal().Err(err).Msg("Failed to parse log level")
}
zerolog.SetGlobalLevel(level)
err = db.NewDB(config.DB.Path)
if err != nil {
logger.Fatal().Err(err).Msg("Failed to open database")
}
tracker.Init(logger)
go tracker.Watch()
metricsRegistry := metrics.DefaultRegistry
cc, err := githubapp.NewDefaultCachingClientCreator(
config.Github,
githubapp.WithClientUserAgent("crown/1.0.0"),
githubapp.WithClientTimeout(3*time.Second),
githubapp.WithClientCaching(false, func() httpcache.Cache { return httpcache.NewMemoryCache() }),
githubapp.WithClientMiddleware(
githubapp.ClientMetrics(metricsRegistry),
),
)
if err != nil {
logger.Fatal().Err(err).Msg("Failed to create client githubApp")
}
webhookHandler := githubapp.NewEventDispatcher(
[]githubapp.EventHandler{
&handlers.PullRequestHandler{ClientCreator: cc},
&handlers.IssueCommentHandler{ClientCreator: cc},
// &handlers.IssuesHandler{ClientCreator: cc},
},
config.Github.App.WebhookSecret,
githubapp.WithScheduler(
githubapp.QueueAsyncScheduler(
100, 10,
),
),
)
http.Handle(githubapp.DefaultWebhookRoute, webhookHandler)
addr := fmt.Sprintf("%s:%d", config.Server.Address, config.Server.Port)
logger.Info().Msgf("Starting server on %s...", addr)
server := &http.Server{
Addr: addr,
ReadHeaderTimeout: 3 * time.Second,
}
err = server.ListenAndServe()
if err != nil {
panic(err)
}
db.DataBase.Close()
}