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