Commit a202aadc authored by Jacob Vosmaer's avatar Jacob Vosmaer

Use api.NewAPI instead of lazy initialization

parent d9014671
...@@ -25,7 +25,7 @@ func runPreAuthorizeHandler(t *testing.T, suffix string, url *regexp.Regexp, api ...@@ -25,7 +25,7 @@ func runPreAuthorizeHandler(t *testing.T, suffix string, url *regexp.Regexp, api
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
a := &api.API{URL: helper.URLMustParse(ts.URL), Version: "123"} a := api.NewAPI(helper.URLMustParse(ts.URL), "123", nil)
response := httptest.NewRecorder() response := httptest.NewRecorder()
a.PreAuthorizeHandler(okHandler, suffix).ServeHTTP(response, httpRequest) a.PreAuthorizeHandler(okHandler, suffix).ServeHTTP(response, httpRequest)
......
...@@ -11,25 +11,24 @@ import ( ...@@ -11,25 +11,24 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"sync"
) )
type API struct { type API struct {
_client *http.Client Client *http.Client
configureClientOnce sync.Once URL *url.URL
URL *url.URL Version string
Version string
RoundTripper *badgateway.RoundTripper
} }
func (a *API) client() *http.Client { func NewAPI(myURL *url.URL, version string, roundtripper *badgateway.RoundTripper) *API {
a.configureClientOnce.Do(func() { a := API{
a._client = &http.Client{Transport: &badgateway.RoundTripper{}} Client: &http.Client{Transport: &badgateway.RoundTripper{}},
if a.RoundTripper != nil { URL: myURL,
a._client.Transport = a.RoundTripper Version: version,
} }
}) if roundtripper != nil {
return a._client a.Client.Transport = roundtripper
}
return &a
} }
type HandleFunc func(http.ResponseWriter, *http.Request, *Response) type HandleFunc func(http.ResponseWriter, *http.Request, *Response)
...@@ -113,7 +112,7 @@ func (api *API) PreAuthorizeHandler(h HandleFunc, suffix string) http.Handler { ...@@ -113,7 +112,7 @@ func (api *API) PreAuthorizeHandler(h HandleFunc, suffix string) http.Handler {
return return
} }
authResponse, err := api.client().Do(authReq) authResponse, err := api.Client.Do(authReq)
if err != nil { if err != nil {
helper.Fail500(w, fmt.Errorf("preAuthorizeHandler: do %v: %v", authReq.URL.Path, err)) helper.Fail500(w, fmt.Errorf("preAuthorizeHandler: do %v: %v", authReq.URL.Path, err))
return return
......
...@@ -31,11 +31,11 @@ const ciAPIPattern = `^/ci/api/` ...@@ -31,11 +31,11 @@ const ciAPIPattern = `^/ci/api/`
// see upstream.ServeHTTP // see upstream.ServeHTTP
func (u *Upstream) configureRoutes() { func (u *Upstream) configureRoutes() {
api := &apipkg.API{ api := apipkg.NewAPI(
RoundTripper: u.RoundTripper(), u.Backend,
URL: u.Backend, u.Version,
Version: u.Version, u.RoundTripper(),
} )
static := &staticpages.Static{u.DocumentRoot} static := &staticpages.Static{u.DocumentRoot}
proxy := &proxypkg.Proxy{ proxy := &proxypkg.Proxy{
URL: u.Backend, URL: u.Backend,
......
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