[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)
envVars, err := githook.GenerateEnvironmentVariables(&githook.Payload{
BaseURL: urlProvider.GetAPIBaseURL(),
BaseURL: urlProvider.GetAPIBaseURLInternal(),
RepoID: repo.ID,
PrincipalID: session.Principal.ID,
RequestID: requestID,

View File

@ -13,32 +13,41 @@ import (
// Provider provides the URLs of the gitness system.
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
// 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.
// NOTE: we store it as url.URL so we can derive clone URLS without errors.
gitURL *url.URL
}
func NewProvider(apiURLRaw string, rawGitURL string) (*Provider, error) {
gitURL, err := url.Parse(rawGitURL)
func NewProvider(apiURLRaw string, apiURLInternalRaw, gitURLRaw string) (*Provider, error) {
gitURL, err := url.Parse(gitURLRaw)
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{
apiURLRaw: apiURLRaw,
apiURLInternalRaw: apiURLInternalRaw,
gitURL: gitURL,
}, 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 {
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 {
repoPath = path.Clean(repoPath)
if !strings.HasSuffix(repoPath, ".git") {

View File

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

View File

@ -20,6 +20,7 @@ type Config struct {
URL struct {
Git string `envconfig:"GITNESS_URL_GIT" 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