Commit 9f0b3f71 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Use eager initialization for Upstream.URLPrefix

parent a202aadc
...@@ -73,7 +73,7 @@ func (u *Upstream) configureRoutes() { ...@@ -73,7 +73,7 @@ func (u *Upstream) configureRoutes() {
// Serve assets // Serve assets
route{"", regexp.MustCompile(`^/assets/`), route{"", regexp.MustCompile(`^/assets/`),
static.ServeExisting(u.URLPrefix(), staticpages.CacheExpireMax, static.ServeExisting(u.URLPrefix, staticpages.CacheExpireMax,
NotFoundUnless(u.DevelopmentMode, NotFoundUnless(u.DevelopmentMode,
proxy, proxy,
), ),
...@@ -88,7 +88,7 @@ func (u *Upstream) configureRoutes() { ...@@ -88,7 +88,7 @@ func (u *Upstream) configureRoutes() {
// Serve static files or forward the requests // Serve static files or forward the requests
route{"", nil, route{"", nil,
static.ServeExisting(u.URLPrefix(), staticpages.CacheDisabled, static.ServeExisting(u.URLPrefix, staticpages.CacheDisabled,
static.DeployPage( static.DeployPage(
static.ErrorPages(u.DevelopmentMode, static.ErrorPages(u.DevelopmentMode,
proxy, proxy,
......
...@@ -28,9 +28,7 @@ type Upstream struct { ...@@ -28,9 +28,7 @@ type Upstream struct {
DevelopmentMode bool DevelopmentMode bool
ProxyHeadersTimeout time.Duration ProxyHeadersTimeout time.Duration
urlPrefix urlprefix.Prefix URLPrefix urlprefix.Prefix
configureURLPrefixOnce sync.Once
Routes []route Routes []route
roundtripper *badgateway.RoundTripper roundtripper *badgateway.RoundTripper
...@@ -46,23 +44,20 @@ func NewUpstream(backend *url.URL, socket string, version string, documentRoot s ...@@ -46,23 +44,20 @@ func NewUpstream(backend *url.URL, socket string, version string, documentRoot s
DevelopmentMode: developmentMode, DevelopmentMode: developmentMode,
ProxyHeadersTimeout: proxyHeadersTimeout, ProxyHeadersTimeout: proxyHeadersTimeout,
} }
if backend == nil {
up.Backend = DefaultBackend
}
up.configureRoutes() up.configureRoutes()
up.configureURLPrefix()
return &up return &up
} }
func (u *Upstream) URLPrefix() urlprefix.Prefix { func (u *Upstream) configureURLPrefix() {
u.configureURLPrefixOnce.Do(func() {
if u.Backend == nil {
u.Backend = DefaultBackend
}
relativeURLRoot := u.Backend.Path relativeURLRoot := u.Backend.Path
if !strings.HasSuffix(relativeURLRoot, "/") { if !strings.HasSuffix(relativeURLRoot, "/") {
relativeURLRoot += "/" relativeURLRoot += "/"
} }
u.urlPrefix = urlprefix.Prefix(relativeURLRoot) u.URLPrefix = urlprefix.Prefix(relativeURLRoot)
})
return u.urlPrefix
} }
func (u *Upstream) RoundTripper() *badgateway.RoundTripper { func (u *Upstream) RoundTripper() *badgateway.RoundTripper {
...@@ -94,7 +89,7 @@ func (u *Upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) { ...@@ -94,7 +89,7 @@ func (u *Upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
// Check URL Root // Check URL Root
URIPath := urlprefix.CleanURIPath(r.URL.Path) URIPath := urlprefix.CleanURIPath(r.URL.Path)
prefix := u.URLPrefix() prefix := u.URLPrefix
if !prefix.Match(URIPath) { if !prefix.Match(URIPath) {
httpError(&w, r, fmt.Sprintf("Not found %q", URIPath), http.StatusNotFound) httpError(&w, r, fmt.Sprintf("Not found %q", URIPath), http.StatusNotFound)
return return
......
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