[MISC] Add internal API URL to url.Provider (#168)

This commit is contained in:
Johannes Batzill 2023-01-06 14:17:41 -08:00 committed by GitHub
parent 5cb824debd
commit 1a84e19ce4
4 changed files with 22 additions and 11 deletions

View File

@ -66,7 +66,7 @@ func CreateRPCWriteParams(ctx context.Context, urlProvider *url.Provider,
// generate envars (add everything githook CLI needs for execution) // generate envars (add everything githook CLI needs for execution)
envVars, err := githook.GenerateEnvironmentVariables(&githook.Payload{ envVars, err := githook.GenerateEnvironmentVariables(&githook.Payload{
BaseURL: urlProvider.GetAPIBaseURL(), BaseURL: urlProvider.GetAPIBaseURLInternal(),
RepoID: repo.ID, RepoID: repo.ID,
PrincipalID: session.Principal.ID, PrincipalID: session.Principal.ID,
RequestID: requestID, RequestID: requestID,

View File

@ -13,32 +13,41 @@ import (
// Provider provides the URLs of the gitness system. // Provider provides the URLs of the gitness system.
type Provider struct { type Provider struct {
// apiURLRaw stores the raw URL the api endpoints are available at. // apiURLRaw stores the raw URL the api endpoints are reachable at publicly.
apiURLRaw string apiURLRaw string
// apiURLInternalRaw stores the raw URL the api endpoints are reachable at internally.
// NOTE: no need for internal services to go via public route.
apiURLInternalRaw string
// gitURL stores the URL the git endpoints are available at. // gitURL stores the URL the git endpoints are available at.
// NOTE: we store it as url.URL so we can derive clone URLS without errors. // NOTE: we store it as url.URL so we can derive clone URLS without errors.
gitURL *url.URL gitURL *url.URL
} }
func NewProvider(apiURLRaw string, rawGitURL string) (*Provider, error) { func NewProvider(apiURLRaw string, apiURLInternalRaw, gitURLRaw string) (*Provider, error) {
gitURL, err := url.Parse(rawGitURL) gitURL, err := url.Parse(gitURLRaw)
if err != nil { if err != nil {
return nil, fmt.Errorf("provided rawGitURL '%s' is invalid: %w", rawGitURL, err) return nil, fmt.Errorf("provided gitURLRaw '%s' is invalid: %w", gitURLRaw, err)
} }
return &Provider{ return &Provider{
apiURLRaw: apiURLRaw, apiURLRaw: apiURLRaw,
gitURL: gitURL, apiURLInternalRaw: apiURLInternalRaw,
gitURL: gitURL,
}, nil }, nil
} }
// GetAPIBaseURL returns the base url of the api server. // GetAPIBaseURL returns the publicly reachable base url of the api server.
func (p *Provider) GetAPIBaseURL() string { func (p *Provider) GetAPIBaseURL() string {
return p.apiURLRaw return p.apiURLRaw
} }
// GenerateRepoCloneURL generates the git clone URL for the provided repo path. // GetAPIBaseURLInternal returns the internally reachable base url of the api server.
func (p *Provider) GetAPIBaseURLInternal() string {
return p.apiURLInternalRaw
}
// GenerateRepoCloneURL generates the public git clone URL for the provided repo path.
func (p *Provider) GenerateRepoCloneURL(repoPath string) string { func (p *Provider) GenerateRepoCloneURL(repoPath string) string {
repoPath = path.Clean(repoPath) repoPath = path.Clean(repoPath)
if !strings.HasSuffix(repoPath, ".git") { if !strings.HasSuffix(repoPath, ".git") {

View File

@ -16,6 +16,7 @@ var WireSet = wire.NewSet(ProvideURLProvider)
func ProvideURLProvider(config *types.Config) (*Provider, error) { func ProvideURLProvider(config *types.Config) (*Provider, error) {
return NewProvider( return NewProvider(
config.URL.API, config.URL.API,
config.URL.APIInternal,
config.URL.Git, config.URL.Git,
) )
} }

View File

@ -18,8 +18,9 @@ type Config struct {
// URL defines the URLs via which the different parts of the service are reachable by. // URL defines the URLs via which the different parts of the service are reachable by.
URL struct { URL struct {
Git string `envconfig:"GITNESS_URL_GIT" default:"http://localhost:3000"` Git string `envconfig:"GITNESS_URL_GIT" default:"http://localhost:3000"`
API string `envconfig:"GITNESS_URL_API" default:"http://localhost:3000"` API string `envconfig:"GITNESS_URL_API" default:"http://localhost:3000"`
APIInternal string `envconfig:"GITNESS_URL_API_INTERNAL" default:"http://localhost:3000"`
} }
// Git defines the git configuration parameters // Git defines the git configuration parameters