Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pact-proxy more configurable using ENV variables #27

Merged
merged 4 commits into from
Nov 9, 2022

Conversation

miketonks-form3
Copy link
Contributor

Work as part of investment time: https://github.com/form3tech/investment-time-project/issues/62

Collect configuration into one struct.

Add configuration for the WaitDelay and WaitDuration

Add envconfig library to tidy up parsing of env vars


func ConfigureProxy(config ProxyConfig) error {
targetURL, err := url.Parse(config.Target)
var c pactproxy.Config

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use config or cfg over c?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


func (c *Config) SetDefaults() {
defaultConfig := Config{}
// Can be changed (on next release) to: envconfig.ExtractDefaults(context.Background(), &defaultConfig)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we carry this information in a issue ticket instead of a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I've decided to remove SetDefaults in favor of a simpler approach

retryFor(func(timeLeft time.Duration) bool {
log.Infof("retry: %s", timeLeft)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@@ -179,7 +200,9 @@ func (a *api) interactionsWaitHandler(res http.ResponseWriter, req *http.Request
}

log.Infof("waiting for %s", waitFor)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here, while we're at it.

err = json.Unmarshal(configBytes, &proxyConfig)
if err != nil {
httpresponse.Errorf(w, http.StatusBadRequest, "unable to parse interactionConstraint from data. %s", err.Error())
return
}
proxyConfig.SetDefaults()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little bit confused about this. We first unmarshal proxyConfig from the payload, we then invoke SetDefaults which creates a new Struct, and runs envconfig.ProcessWith(context.Background(), &defaultConfig, envconfig.MapLookuper(nil)) . does this mean we ignore the unmarshalled result?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit horrible. I think this admin handler is only used by the tests. Because the config is posted as json, the env loading can't be used. Any config values not passed will be set to zero values.

proxyConfig.SetDefaults() is updating the struct and overwriting zero values with the default values, for fields where this makes sense (AdminPort, WaitDelay, WaitDuration). Tests fail if WaitDelay is zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the SetDefaults as it is indeed confusing. Moved back to a simpler handling of defaults in the proxy: define constants and a simple if to set them if passed values are zero.

serverAddrURL, err := url.Parse(serverAddr)
if err != nil {
return err
log.Fatal(err.Error())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if the function returned pactproxy.Config, error and we did the fatal in main.go.

@miketonks-form3 miketonks-form3 marked this pull request as ready for review November 2, 2022 15:46
@miketonks-form3 miketonks-form3 requested a review from a team as a code owner November 2, 2022 15:46
@miketonks-form3 miketonks-form3 merged commit b115d57 into master Nov 9, 2022
@miketonks-form3 miketonks-form3 deleted the mike-kintri-config branch November 9, 2022 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants