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/` ...@@ -30,11 +30,6 @@ const ciAPIPattern = `^/ci/api/`
// We match against URI not containing the relativeUrlRoot: // We match against URI not containing the relativeUrlRoot:
// see upstream.ServeHTTP // see upstream.ServeHTTP
func (u *Upstream) Routes() []route {
u.configureRoutesOnce.Do(u.configureRoutes)
return u.routes
}
func (u *Upstream) configureRoutes() { func (u *Upstream) configureRoutes() {
api := &apipkg.API{ api := &apipkg.API{
RoundTripper: u.RoundTripper(), RoundTripper: u.RoundTripper(),
...@@ -48,7 +43,7 @@ func (u *Upstream) configureRoutes() { ...@@ -48,7 +43,7 @@ func (u *Upstream) configureRoutes() {
RoundTripper: u.RoundTripper(), RoundTripper: u.RoundTripper(),
} }
u.routes = []route{ u.Routes = []route{
// Git Clone // Git Clone
route{"GET", regexp.MustCompile(gitProjectPattern + `info/refs\z`), git.GetInfoRefs(api)}, route{"GET", regexp.MustCompile(gitProjectPattern + `info/refs\z`), git.GetInfoRefs(api)},
route{"POST", regexp.MustCompile(gitProjectPattern + `git-upload-pack\z`), contentEncodingHandler(git.PostRPC(api))}, route{"POST", regexp.MustCompile(gitProjectPattern + `git-upload-pack\z`), contentEncodingHandler(git.PostRPC(api))},
......
...@@ -31,13 +31,25 @@ type Upstream struct { ...@@ -31,13 +31,25 @@ type Upstream struct {
urlPrefix urlprefix.Prefix urlPrefix urlprefix.Prefix
configureURLPrefixOnce sync.Once configureURLPrefixOnce sync.Once
routes []route Routes []route
configureRoutesOnce sync.Once
roundtripper *badgateway.RoundTripper roundtripper *badgateway.RoundTripper
configureRoundTripperOnce sync.Once 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 { func (u *Upstream) URLPrefix() urlprefix.Prefix {
u.configureURLPrefixOnce.Do(func() { u.configureURLPrefixOnce.Do(func() {
if u.Backend == nil { if u.Backend == nil {
...@@ -91,7 +103,7 @@ func (u *Upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) { ...@@ -91,7 +103,7 @@ func (u *Upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
// Look for a matching Git service // Look for a matching Git service
var ro route var ro route
foundService := false foundService := false
for _, ro = range u.Routes() { for _, ro = range u.Routes {
if ro.method != "" && r.Method != ro.method { if ro.method != "" && r.Method != ro.method {
continue continue
} }
......
...@@ -81,14 +81,14 @@ func main() { ...@@ -81,14 +81,14 @@ func main() {
}() }()
} }
up := &upstream.Upstream{ up := upstream.NewUpstream(
Backend: authBackend, authBackend,
Socket: *authSocket, *authSocket,
Version: Version, Version,
ProxyHeadersTimeout: *proxyHeadersTimeout, *documentRoot,
DocumentRoot: *documentRoot, *developmentMode,
DevelopmentMode: *developmentMode, *proxyHeadersTimeout,
} )
log.Fatal(http.Serve(listener, up)) log.Fatal(http.Serve(listener, up))
} }
...@@ -505,11 +505,14 @@ func testAuthServer(url *regexp.Regexp, code int, body interface{}) *httptest.Se ...@@ -505,11 +505,14 @@ func testAuthServer(url *regexp.Regexp, code int, body interface{}) *httptest.Se
} }
func startWorkhorseServer(authBackend string) *httptest.Server { func startWorkhorseServer(authBackend string) *httptest.Server {
u := &upstream.Upstream{ u := upstream.NewUpstream(
Backend: helper.URLMustParse(authBackend), helper.URLMustParse(authBackend),
Version: "123", "",
DocumentRoot: testDocumentRoot, "123",
} testDocumentRoot,
false,
time.Minute,
)
return httptest.NewServer(u) 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