Commit d9014671 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Initialize routes eagerly in NewUpstream

parent 3ea22ec1
......@@ -30,11 +30,6 @@ const ciAPIPattern = `^/ci/api/`
// We match against URI not containing the relativeUrlRoot:
// see upstream.ServeHTTP
func (u *Upstream) Routes() []route {
u.configureRoutesOnce.Do(u.configureRoutes)
return u.routes
}
func (u *Upstream) configureRoutes() {
api := &apipkg.API{
RoundTripper: u.RoundTripper(),
......@@ -48,7 +43,7 @@ func (u *Upstream) configureRoutes() {
RoundTripper: u.RoundTripper(),
}
u.routes = []route{
u.Routes = []route{
// Git Clone
route{"GET", regexp.MustCompile(gitProjectPattern + `info/refs\z`), git.GetInfoRefs(api)},
route{"POST", regexp.MustCompile(gitProjectPattern + `git-upload-pack\z`), contentEncodingHandler(git.PostRPC(api))},
......
......@@ -31,13 +31,25 @@ type Upstream struct {
urlPrefix urlprefix.Prefix
configureURLPrefixOnce sync.Once
routes []route
configureRoutesOnce sync.Once
Routes []route
roundtripper *badgateway.RoundTripper
configureRoundTripperOnce sync.Once
}
func NewUpstream(backend *url.URL, socket string, version string, documentRoot string, developmentMode bool, proxyHeadersTimeout time.Duration) *Upstream {
up := Upstream{
Backend: backend,
Socket: socket,
Version: version,
DocumentRoot: documentRoot,
DevelopmentMode: developmentMode,
ProxyHeadersTimeout: proxyHeadersTimeout,
}
up.configureRoutes()
return &up
}
func (u *Upstream) URLPrefix() urlprefix.Prefix {
u.configureURLPrefixOnce.Do(func() {
if u.Backend == nil {
......@@ -91,7 +103,7 @@ func (u *Upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
// Look for a matching Git service
var ro route
foundService := false
for _, ro = range u.Routes() {
for _, ro = range u.Routes {
if ro.method != "" && r.Method != ro.method {
continue
}
......
......@@ -81,14 +81,14 @@ func main() {
}()
}
up := &upstream.Upstream{
Backend: authBackend,
Socket: *authSocket,
Version: Version,
ProxyHeadersTimeout: *proxyHeadersTimeout,
DocumentRoot: *documentRoot,
DevelopmentMode: *developmentMode,
}
up := upstream.NewUpstream(
authBackend,
*authSocket,
Version,
*documentRoot,
*developmentMode,
*proxyHeadersTimeout,
)
log.Fatal(http.Serve(listener, up))
}
......@@ -505,11 +505,14 @@ func testAuthServer(url *regexp.Regexp, code int, body interface{}) *httptest.Se
}
func startWorkhorseServer(authBackend string) *httptest.Server {
u := &upstream.Upstream{
Backend: helper.URLMustParse(authBackend),
Version: "123",
DocumentRoot: testDocumentRoot,
}
u := upstream.NewUpstream(
helper.URLMustParse(authBackend),
"",
"123",
testDocumentRoot,
false,
time.Minute,
)
return httptest.NewServer(u)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment