github.com/alexey-mercari/reviewdog@v0.10.1-0.20200514053941-928943b10766/doghouse/appengine/main.go (about) 1 package main 2 3 import ( 4 "io/ioutil" 5 "log" 6 "net/http" 7 "os" 8 "strconv" 9 10 "github.com/haya14busa/secretbox" 11 "github.com/justinas/nosurf" 12 13 "github.com/reviewdog/reviewdog/doghouse/server/cookieman" 14 "github.com/reviewdog/reviewdog/doghouse/server/storage" 15 ) 16 17 func mustCookieMan() *cookieman.CookieMan { 18 // Create secret key by following command. 19 // $ ruby -rsecurerandom -e 'puts SecureRandom.hex(32)' 20 cipher, err := secretbox.NewFromHexKey(mustGetenv("SECRETBOX_SECRET")) 21 if err != nil { 22 log.Fatalf("failed to create secretbox: %v", err) 23 } 24 c := cookieman.CookieOption{ 25 Cookie: http.Cookie{ 26 HttpOnly: true, 27 Secure: true, 28 Path: "/", 29 }, 30 } 31 return cookieman.New(cipher, c) 32 } 33 34 func mustGitHubAppsPrivateKey() []byte { 35 // Private keys https://github.com/settings/apps/reviewdog 36 githubAppsPrivateKey, err := ioutil.ReadFile(mustGetenv("GITHUB_PRIVATE_KEY_FILE")) 37 if err != nil { 38 log.Fatalf("could not read private key: %s", err) 39 } 40 return githubAppsPrivateKey 41 } 42 43 func mustGetenv(name string) string { 44 s := os.Getenv(name) 45 if s == "" { 46 log.Fatalf("%s is not set", name) 47 } 48 return s 49 } 50 51 func mustIntEnv(name string) int { 52 s := os.Getenv(name) 53 if s == "" { 54 log.Fatalf("%s is not set", name) 55 } 56 i, err := strconv.Atoi(s) 57 if err != nil { 58 log.Fatal(err) 59 } 60 return i 61 } 62 63 func main() { 64 initTemplates() 65 66 integrationID := mustIntEnv("GITHUB_INTEGRATION_ID") 67 ghPrivateKey := mustGitHubAppsPrivateKey() 68 69 ghInstStore := storage.GitHubInstallationDatastore{} 70 ghRepoTokenStore := storage.GitHubRepoTokenDatastore{} 71 72 ghHandler := NewGitHubHandler( 73 mustGetenv("GITHUB_CLIENT_ID"), 74 mustGetenv("GITHUB_CLIENT_SECRET"), 75 mustCookieMan(), 76 ghPrivateKey, 77 integrationID, 78 ) 79 80 ghChecker := githubChecker{ 81 privateKey: ghPrivateKey, 82 integrationID: integrationID, 83 ghInstStore: &ghInstStore, 84 ghRepoTokenStore: &ghRepoTokenStore, 85 } 86 87 ghWebhookHandler := githubWebhookHandler{ 88 secret: []byte(mustGetenv("GITHUB_WEBHOOK_SECRET")), 89 ghInstStore: &ghInstStore, 90 } 91 92 mu := http.NewServeMux() 93 94 // Register Admin handlers. 95 mu.HandleFunc("/_ah/warmup", warmupHandler) 96 97 mu.HandleFunc("/", handleTop) 98 mu.HandleFunc("/check", ghChecker.handleCheck) 99 mu.HandleFunc("/gh_/webhook", ghWebhookHandler.handleWebhook) 100 mu.HandleFunc("/gh_/auth/callback", ghHandler.HandleAuthCallback) 101 mu.HandleFunc("/gh_/logout", ghHandler.HandleLogout) 102 mu.Handle("/gh/", nosurf.New(ghHandler.LogInHandler(http.HandlerFunc(ghHandler.HandleGitHubTop)))) 103 104 http.Handle("/", mu) 105 log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), nil)) 106 } 107 108 func handleTop(w http.ResponseWriter, _ *http.Request) { 109 var data struct { 110 Title string 111 } 112 data.Title = "reviewdog" 113 topTmpl.ExecuteTemplate(w, "base", &data) 114 }