-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
114 lines (98 loc) · 3.27 KB
/
server.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 (
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var client http.Client
func healthHandler(cache *CrucibleRepositoriesCache) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if cache.isEmpty() {
http.Error(w, "not enough projects", http.StatusPreconditionFailed)
}
})
}
func handler(crucibleSettings CrucibleSettings, gitLabSettings GitLabSettings, crucibleCache *CrucibleRepositoriesCache) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
return
}
gitUrl, err := GetNormalizedGitUrlFromRequest(r, gitLabSettings)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if gitUrl == "" {
http.Error(w, "git url is empty. Is the hook in the proper format?", http.StatusBadRequest)
return
}
projectId := crucibleCache.getRepositoryName(gitUrl)
if projectId == "" {
http.Error(w, "project not found", http.StatusNotFound)
return
}
err = TriggerCrucibleSync(projectId, client, &crucibleSettings)
if err != nil {
log.Printf("error when sending request to Crucible: %s\n", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}
func cron(f func(), d time.Duration) {
ch := time.After(d)
for {
<-ch
f()
ch = time.After(d)
}
}
func main() {
projectRefreshInterval, err := strconv.ParseInt(os.Getenv("CRUCIBLE_PROJECT_REFRESH_INTERVAL"), 10, 32)
if err != nil {
panic(err)
}
projectLimit, err := strconv.ParseInt(os.Getenv("CRUCIBLE_PROJECT_LIMIT"), 10, 32)
if err != nil {
panic(err)
}
crucibleSettings := CrucibleSettings{
ApiBaseUrl: os.Getenv("CRUCIBLE_API_BASE_URL"),
ApiKey: os.Getenv("CRUCIBLE_API_KEY"),
Username: os.Getenv("CRUCIBLE_USERNAME"),
Password: os.Getenv("CRUCIBLE_PASSWORD"),
ProjectRefreshInterval: int(projectRefreshInterval),
ProjectLimit: int(projectLimit),
}
gitLabSettings := GitLabSettings{
Token: strings.TrimSpace(os.Getenv("GITLAB_TOKEN")),
HostNames: strings.Fields(os.Getenv("GITLAB_HOSTNAMES")),
}
crucibleCache := &CrucibleRepositoriesCache{}
updateCache := crucibleCache.updateFactory(crucibleSettings, func(url string) string {
return NormalizeGitUrl(url, gitLabSettings.HostNames)
})
updateCache()
go cron(updateCache, time.Minute*time.Duration(crucibleSettings.ProjectRefreshInterval))
log.Println(fmt.Sprintf("downloading Crucible project list every %d minute(s)", crucibleSettings.ProjectRefreshInterval))
client = http.Client{
Timeout: 5 * time.Second,
}
histogram := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "gitlab_crucible_bridge_request_duration_seconds",
Help: "Duration of HTTP requests in seconds",
Buckets: prometheus.ExponentialBuckets(0.1, 3, 4),
}, []string{"code"})
prometheus.Register(histogram)
http.Handle("/", promhttp.InstrumentHandlerDuration(
histogram, handler(crucibleSettings, gitLabSettings, crucibleCache)))
http.Handle("/metrics", promhttp.Handler())
http.Handle("/health", healthHandler(crucibleCache))
log.Fatal(http.ListenAndServe(":8888", nil))
}