-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (70 loc) · 2.41 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
package main
import (
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"net/http"
"os"
"issues2stories/internal/config"
"issues2stories/internal/githubapi"
"issues2stories/internal/trackeractivity"
"issues2stories/internal/trackerapi"
"issues2stories/internal/trackerimport"
)
func main() {
log.Println("Starting server at port 8080")
const configFilePath = "/etc/config/config.yaml"
configYAML, err := ioutil.ReadFile(configFilePath)
if err != nil {
log.Fatalf("could not read config file: %s", configFilePath)
}
configuration := config.Config{}
err = yaml.Unmarshal(configYAML, &configuration)
if err != nil {
log.Fatalf("could not parse config file (%s) as YAML: %v", configFilePath, err)
}
log.Printf("Read user ID mapping config: %v", configuration.UserIDMapping)
gitHubOrg := requireEnv("GITHUB_ORG")
gitHubRepo := requireEnv("GITHUB_REPO")
gitAPIToken := requireEnv("GITHUB_API_TOKEN")
trackerAPIToken := requireEnv("TRACKER_API_TOKEN")
basicAuthCredentials := &config.BasicAuthCredentials{
Username: requireEnv("BASIC_AUTH_USERNAME"),
Password: requireEnv("BASIC_AUTH_PASSWORD"),
}
trackerClient := trackerapi.New(trackerAPIToken, &http.Client{})
gitHubClient := githubapi.New(gitAPIToken, gitHubOrg, gitHubRepo)
mux := http.NewServeMux()
mux.Handle("/tracker_activity",
trackeractivity.NewHandler(trackerClient, gitHubClient, &configuration, basicAuthCredentials))
mux.Handle("/tracker_import",
trackerimport.NewHandler(gitHubClient, basicAuthCredentials))
mux.Handle("/",
http.HandlerFunc(defaultHandler))
if err := http.ListenAndServe(":8080", mux); err != nil {
log.Fatal(err)
}
}
func requireEnv(envVarName string) string {
value, ok := os.LookupEnv(envVarName)
if !ok || value == "" {
log.Fatalf("environment variable not found or empty: %s", envVarName)
}
return value
}
// Respond to heath checks at the root path. GKE Ingress default health check is GET "/".
func defaultHandler(responseWriter http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/" {
log.Printf("defaultHandler: path not found: %s", request.URL.Path)
http.Error(responseWriter, "404. Not found.", http.StatusNotFound)
return
}
if request.Method != "GET" {
msg := "Method is not supported"
log.Printf("defaultHandler: %v", msg)
http.Error(responseWriter, msg, http.StatusMethodNotAllowed)
return
}
log.Print("defaultHandler: responding ok to GET /")
responseWriter.Write([]byte("ok"))
}