Commit d55aa579 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Push some initialization out of internal/upstream

parent 8e9a6da2
......@@ -3,7 +3,6 @@ package main
import (
"./internal/api"
"./internal/helper"
"./internal/upstream"
"fmt"
"net/http"
"net/http/httptest"
......@@ -26,10 +25,10 @@ func runPreAuthorizeHandler(t *testing.T, suffix string, url *regexp.Regexp, api
if err != nil {
t.Fatal(err)
}
api := (&upstream.Upstream{Backend: helper.URLMustParse(ts.URL), Version: "123"}).API()
a := &api.API{URL: helper.URLMustParse(ts.URL), Version: "123"}
response := httptest.NewRecorder()
api.PreAuthorizeHandler(okHandler, suffix)(response, httpRequest)
a.PreAuthorizeHandler(okHandler, suffix)(response, httpRequest)
helper.AssertResponseCode(t, response, expectedCode)
return response
}
......
package api
import (
"../badgateway"
"../helper"
"../proxy"
"encoding/json"
......@@ -10,12 +11,25 @@ import (
"net/http"
"net/url"
"strings"
"sync"
)
type API struct {
*http.Client
URL *url.URL
Version string
_client *http.Client
configureClientOnce sync.Once
URL *url.URL
Version string
RoundTripper *badgateway.RoundTripper
}
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
}
type HandleFunc func(http.ResponseWriter, *http.Request, *Response)
......@@ -99,7 +113,7 @@ func (api *API) PreAuthorizeHandler(h HandleFunc, suffix string) http.HandlerFun
return
}
authResponse, err := api.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
......
package upstream
import (
apipkg "../api"
"../git"
"../lfs"
pr "../proxy"
proxypkg "../proxy"
"../staticpages"
"../upload"
"net/http"
......@@ -34,31 +35,41 @@ func (u *Upstream) Routes() []route {
}
func (u *Upstream) configureRoutes() {
api := &apipkg.API{
RoundTripper: u.RoundTripper(),
URL: u.Backend,
Version: u.Version,
}
static := &staticpages.Static{u.DocumentRoot}
proxy := &pr.Proxy{URL: u.Backend, Version: u.Version, RoundTripper: u.RoundTripper()}
proxy := &proxypkg.Proxy{
URL: u.Backend,
Version: u.Version,
RoundTripper: u.RoundTripper(),
}
u.routes = []route{
// Git Clone
route{"GET", regexp.MustCompile(gitProjectPattern + `info/refs\z`), git.GetInfoRefs(u.API())},
route{"POST", regexp.MustCompile(gitProjectPattern + `git-upload-pack\z`), contentEncodingHandler(git.PostRPC(u.API()))},
route{"POST", regexp.MustCompile(gitProjectPattern + `git-receive-pack\z`), contentEncodingHandler(git.PostRPC(u.API()))},
route{"PUT", regexp.MustCompile(gitProjectPattern + `gitlab-lfs/objects/([0-9a-f]{64})/([0-9]+)\z`), lfs.PutStore(u.API(), proxy)},
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-receive-pack\z`), contentEncodingHandler(git.PostRPC(api))},
route{"PUT", regexp.MustCompile(gitProjectPattern + `gitlab-lfs/objects/([0-9a-f]{64})/([0-9]+)\z`), lfs.PutStore(api, proxy)},
// Repository Archive
route{"GET", regexp.MustCompile(projectPattern + `repository/archive\z`), git.GetArchive(u.API())},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.zip\z`), git.GetArchive(u.API())},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar\z`), git.GetArchive(u.API())},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.gz\z`), git.GetArchive(u.API())},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.bz2\z`), git.GetArchive(u.API())},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive\z`), git.GetArchive(api)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.zip\z`), git.GetArchive(api)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar\z`), git.GetArchive(api)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.gz\z`), git.GetArchive(api)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.bz2\z`), git.GetArchive(api)},
// Repository Archive API
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive\z`), git.GetArchive(u.API())},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.zip\z`), git.GetArchive(u.API())},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar\z`), git.GetArchive(u.API())},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.gz\z`), git.GetArchive(u.API())},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.bz2\z`), git.GetArchive(u.API())},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive\z`), git.GetArchive(api)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.zip\z`), git.GetArchive(api)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar\z`), git.GetArchive(api)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.gz\z`), git.GetArchive(api)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.bz2\z`), git.GetArchive(api)},
// CI Artifacts API
route{"POST", regexp.MustCompile(ciAPIPattern + `v1/builds/[0-9]+/artifacts\z`), contentEncodingHandler(upload.Artifacts(u.API(), proxy))},
route{"POST", regexp.MustCompile(ciAPIPattern + `v1/builds/[0-9]+/artifacts\z`), contentEncodingHandler(upload.Artifacts(api, proxy))},
// Explicitly proxy API requests
route{"", regexp.MustCompile(apiPattern), proxy},
......
......@@ -7,11 +7,8 @@ In this file we handle request routing and interaction with the authBackend.
package upstream
import (
"../api"
"../badgateway"
"../helper"
"../proxy"
"../staticpages"
"../urlprefix"
"fmt"
"net/http"
......@@ -31,12 +28,6 @@ type Upstream struct {
DevelopmentMode bool
ResponseHeaderTimeout time.Duration
_api *api.API
configureAPIOnce sync.Once
_proxy *proxy.Proxy
configureProxyOnce sync.Once
urlPrefix urlprefix.Prefix
configureURLPrefixOnce sync.Once
......@@ -45,22 +36,6 @@ type Upstream struct {
roundtripper *badgateway.RoundTripper
configureRoundTripperOnce sync.Once
_static *staticpages.Static
configureStaticOnce sync.Once
}
func (u *Upstream) API() *api.API {
u.configureAPIOnce.Do(u.configureAPI)
return u._api
}
func (u *Upstream) configureAPI() {
u._api = &api.API{
Client: &http.Client{Transport: u.RoundTripper()},
URL: u.Backend,
Version: u.Version,
}
}
func (u *Upstream) URLPrefix() urlprefix.Prefix {
......
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