Skip to content

Commit

Permalink
Merge pull request #27 from form3tech-oss/mike-kintri-config
Browse files Browse the repository at this point in the history
Make pact-proxy more configurable using ENV variables
  • Loading branch information
miketonks-form3 authored Nov 9, 2022
2 parents 80ce19d + ab4da27 commit b115d57
Show file tree
Hide file tree
Showing 14 changed files with 1,493 additions and 40 deletions.
17 changes: 9 additions & 8 deletions cmd/pact-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ import (
"os"
"os/signal"
"strconv"
"strings"
"syscall"

"github.com/form3tech-oss/pact-proxy/internal/app/configuration"
"github.com/form3tech-oss/pact-proxy/internal/app/pactproxy"
log "github.com/sirupsen/logrus"
)

func main() {
proxies := os.Getenv("PROXIES")
for _, proxy := range strings.Split(strings.TrimSpace(proxies), ";") {
if proxy != "" {
log.Infof("setting up proxy for %s", proxy)
if err := configuration.ConfigureProxy(configuration.ProxyConfig{Target: proxy}); err != nil {
panic(err)
}
config, err := configuration.NewFromEnv()
if err != nil {
log.WithError(err).Fatal("unable to load configuration")
}
for _, proxy := range config.Proxies {
log.Infof("setting up proxy for %s", proxy)
if err := configuration.ConfigureProxy(pactproxy.Config{Target: proxy}); err != nil {
panic(err)
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/labstack/echo/v4 v4.9.0
github.com/pact-foundation/pact-go v1.7.0
github.com/pkg/errors v0.9.1
github.com/sethvargo/go-envconfig v0.8.2
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.0
github.com/tidwall/sjson v1.2.4
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -208,6 +209,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sethvargo/go-envconfig v0.8.2 h1:DDUVuG21RMgeB/bn4leclUI/837y6cQCD4w8hb5797k=
github.com/sethvargo/go-envconfig v0.8.2/go.mod h1:Iz1Gy1Sf3T64TQlJSvee81qDhf7YIlt8GMUX6yyNFs0=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
Expand Down
3 changes: 2 additions & 1 deletion internal/app/configuration/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/form3tech-oss/pact-proxy/internal/app/httpresponse"
"github.com/form3tech-oss/pact-proxy/internal/app/pactproxy"
"github.com/labstack/echo/v4"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -34,7 +35,7 @@ func deleteProxiesHandler(c echo.Context) error {

func postProxiesHandler(c echo.Context) error {

proxyConfig := ProxyConfig{}
proxyConfig := pactproxy.Config{}
err := c.Bind(&proxyConfig)
if err != nil {
return c.JSON(
Expand Down
34 changes: 13 additions & 21 deletions internal/app/configuration/proxyconfig.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
package configuration

import (
"fmt"
"net/url"
"context"

"github.com/form3tech-oss/pact-proxy/internal/app/pactproxy"
"github.com/pkg/errors"
"github.com/sethvargo/go-envconfig"
)

type ProxyConfig struct {
ServerAddress string
Target string
}
func NewFromEnv() (pactproxy.Config, error) {
ctx := context.Background()

func ConfigureProxy(config ProxyConfig) error {
targetURL, err := url.Parse(config.Target)
var config pactproxy.Config
err := envconfig.Process(ctx, &config)
if err != nil {
return err
}

serverAddr := config.ServerAddress
if serverAddr == "" {
serverAddr = fmt.Sprintf("http://:%s", targetURL.Port())
}

serverAddrURL, err := url.Parse(serverAddr)
if err != nil {
return err
return config, errors.Wrap(err, "process env config")
}
return config, nil
}

server, err := GetServer(serverAddrURL)
func ConfigureProxy(config pactproxy.Config) error {
server, err := GetServer(&config.ServerAddress)
if err != nil {
return err
}

pactproxy.StartProxy(server, targetURL)
pactproxy.StartProxy(server, &config)
return err
}
31 changes: 25 additions & 6 deletions internal/app/pactproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ const (
defaultDuration = 15 * time.Second
)

type Config struct {
ServerAddress url.URL `env:"SERVER_ADDRESS"` // Address to listen on
Proxies []url.URL `env:"PROXIES,delimiter=;"` // List of URL to serve pact-proxy on, e.g. http://localhost:8080;http://localhost:8081
WaitDelay time.Duration `env:"WAIT_DELAY"` // Default Delay for WaitForInteractions endpoint
WaitDuration time.Duration `env:"WAIT_DURATION"` // Default Duration for WaitForInteractions endpoint
Target url.URL // Do not load Target from env, we set this for each value from Proxies
}

var supportedMediaTypes = map[string]func([]byte, *url.URL) (requestDocument, error){
mediaTypeJSON: ParseJSONRequest,
mediaTypeText: ParsePlainTextRequest,
Expand All @@ -41,16 +49,22 @@ func (a *api) ProxyRequest(c echo.Context) error {
return nil
}

func StartProxy(e *echo.Echo, target *url.URL) {
func StartProxy(e *echo.Echo, config *Config) {

// Create these once at startup, thay are shared by all server threads
a := api{
target: target,
proxy: httputil.NewSingleHostReverseProxy(target),
target: &config.Target,
proxy: httputil.NewSingleHostReverseProxy(&config.Target),
interactions: &Interactions{},
notify: NewNotify(),
delay: defaultDelay,
duration: defaultDuration,
delay: config.WaitDelay,
duration: config.WaitDuration,
}
if a.delay == 0 {
a.delay = defaultDelay
}
if a.duration == 0 {
a.duration = defaultDuration
}

e.GET("/ready", a.readinessHandler)
Expand Down Expand Up @@ -168,8 +182,13 @@ func (a *api) interactionsWaitHandler(c echo.Context) error {
return c.JSON(http.StatusBadRequest, httpresponse.Errorf("cannot wait for interaction '%s', interaction not found.", waitFor))
}

log.Infof("waiting for %s", waitFor)
log.WithField("wait_for", waitFor).Infof("waiting")
retryFor(func(timeLeft time.Duration) bool {
log.WithFields(log.Fields{
"wait_for": waitFor,
"count": waitForCount,
"time_remaining": timeLeft,
}).Infof("retry")
if interaction.HasRequests(waitForCount) {
return true
}
Expand Down
21 changes: 17 additions & 4 deletions pkg/pactproxy/proxyconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"

"github.com/form3tech-oss/pact-proxy/internal/app/configuration"
"github.com/form3tech-oss/pact-proxy/internal/app/pactproxy"
"github.com/pkg/errors"
)

Expand All @@ -27,12 +28,24 @@ func Configuration(url string) *ProxyConfiguration {
}

func (conf *ProxyConfiguration) SetupProxy(serverAddress, targetAddress string) (*PactProxy, error) {
config := &configuration.ProxyConfig{
ServerAddress: serverAddress,
Target: targetAddress,
serverURL, err := url.Parse(serverAddress)
if err != nil {
return nil, errors.Wrap(err, "failed to parse server address")
}
targetURL, err := url.Parse(targetAddress)
if err != nil {
return nil, errors.Wrap(err, "failed to parse target address")
}

config := &pactproxy.Config{
ServerAddress: *serverURL,
Target: *targetURL,
}

content, err := json.Marshal(config)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal config")
}

req, err := http.NewRequest("POST", strings.TrimSuffix(conf.url, "/")+"/proxies", bytes.NewReader(content))
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions vendor/github.com/sethvargo/go-envconfig/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b115d57

Please sign in to comment.