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