-
-
Notifications
You must be signed in to change notification settings - Fork 397
/
main.go
114 lines (96 loc) 路 2.67 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
112
113
114
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"github.com/haya14busa/secretbox"
"github.com/justinas/nosurf"
"github.com/reviewdog/reviewdog/doghouse/server/cookieman"
"github.com/reviewdog/reviewdog/doghouse/server/storage"
)
func mustCookieMan() *cookieman.CookieMan {
// Create secret key by following command.
// $ ruby -rsecurerandom -e 'puts SecureRandom.hex(32)'
cipher, err := secretbox.NewFromHexKey(mustGetenv("SECRETBOX_SECRET"))
if err != nil {
log.Fatalf("failed to create secretbox: %v", err)
}
c := cookieman.CookieOption{
Cookie: http.Cookie{
HttpOnly: true,
Secure: true,
Path: "/",
},
}
return cookieman.New(cipher, c)
}
func mustGitHubAppsPrivateKey() []byte {
// Private keys https://github.com/settings/apps/reviewdog
githubAppsPrivateKey, err := ioutil.ReadFile(mustGetenv("GITHUB_PRIVATE_KEY_FILE"))
if err != nil {
log.Fatalf("could not read private key: %s", err)
}
return githubAppsPrivateKey
}
func mustGetenv(name string) string {
s := os.Getenv(name)
if s == "" {
log.Fatalf("%s is not set", name)
}
return s
}
func mustIntEnv(name string) int {
s := os.Getenv(name)
if s == "" {
log.Fatalf("%s is not set", name)
}
i, err := strconv.Atoi(s)
if err != nil {
log.Fatal(err)
}
return i
}
func main() {
initTemplates()
integrationID := mustIntEnv("GITHUB_INTEGRATION_ID")
ghPrivateKey := mustGitHubAppsPrivateKey()
ghInstStore := storage.GitHubInstallationDatastore{}
ghRepoTokenStore := storage.GitHubRepoTokenDatastore{}
ghHandler := NewGitHubHandler(
mustGetenv("GITHUB_CLIENT_ID"),
mustGetenv("GITHUB_CLIENT_SECRET"),
mustCookieMan(),
ghPrivateKey,
integrationID,
)
ghChecker := githubChecker{
privateKey: ghPrivateKey,
integrationID: integrationID,
ghInstStore: &ghInstStore,
ghRepoTokenStore: &ghRepoTokenStore,
}
ghWebhookHandler := githubWebhookHandler{
secret: []byte(mustGetenv("GITHUB_WEBHOOK_SECRET")),
ghInstStore: &ghInstStore,
}
mu := http.NewServeMux()
// Register Admin handlers.
mu.HandleFunc("/_ah/warmup", warmupHandler)
mu.HandleFunc("/", handleTop)
mu.HandleFunc("/check", ghChecker.handleCheck)
mu.HandleFunc("/gh_/webhook", ghWebhookHandler.handleWebhook)
mu.HandleFunc("/gh_/auth/callback", ghHandler.HandleAuthCallback)
mu.HandleFunc("/gh_/logout", ghHandler.HandleLogout)
mu.Handle("/gh/", nosurf.New(ghHandler.LogInHandler(http.HandlerFunc(ghHandler.HandleGitHubTop))))
http.Handle("/", mu)
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}
func handleTop(w http.ResponseWriter, r *http.Request) {
var data struct {
Title string
}
data.Title = "reviewdog"
topTmpl.ExecuteTemplate(w, "base", &data)
}