From bc857f4bf2f03590c5f8db48457acca2b3925c9c Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Wed, 7 Aug 2019 14:16:12 -0700 Subject: [PATCH] Replace endpoints map with list of endpoint routes (#79) --- pkg/cmd/listen.go | 22 ++++++++++++++-------- pkg/cmd/listen_test.go | 8 +++++--- pkg/proxy/proxy.go | 19 ++++++++++++++----- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/pkg/cmd/listen.go b/pkg/cmd/listen.go index c084643cf..39463ac63 100644 --- a/pkg/cmd/listen.go +++ b/pkg/cmd/listen.go @@ -84,7 +84,7 @@ func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error { return err } - endpointsMap := make(map[string][]string) + endpointRoutes := make([]proxy.EndpointRoute, 0) key, err := Config.Profile.GetSecretKey() if err != nil { @@ -96,7 +96,10 @@ func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error { } if len(lc.forwardURL) > 0 { - endpointsMap[parseURL(lc.forwardURL)] = lc.events + endpointRoutes = append(endpointRoutes, proxy.EndpointRoute{ + URL: lc.forwardURL, + EventTypes: lc.events, + }) } if lc.loadFromWebhooksAPI && len(lc.forwardURL) > 0 { @@ -109,7 +112,7 @@ func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error { return errors.New("You have not defined any webhook endpoints on your account. Go to the Stripe Dashboard to add some: https://dashboard.stripe.com/test/webhooks") } - endpointsMap = buildEndpointsMap(endpoints, parseURL(lc.forwardURL)) + endpointRoutes = buildEndpointRoutes(endpoints, parseURL(lc.forwardURL)) } else if lc.loadFromWebhooksAPI && len(lc.forwardURL) == 0 { return errors.New("--load-from-webhooks-api requires a location to forward to with --forward-to") } @@ -117,7 +120,7 @@ func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error { p := proxy.New(&proxy.Config{ DeviceName: deviceName, Key: key, - EndpointsMap: endpointsMap, + EndpointRoutes: endpointRoutes, APIBaseURL: lc.apiBaseURL, WebSocketFeature: webhooksWebSocketFeature, PrintJSON: lc.printJSON, @@ -143,19 +146,22 @@ func (lc *listenCmd) getEndpointsFromAPI(secretKey string) requests.WebhookEndpo return examples.WebhookEndpointsList() } -func buildEndpointsMap(endpoints requests.WebhookEndpointList, forwardURL string) map[string][]string { - endpointsMap := make(map[string][]string) +func buildEndpointRoutes(endpoints requests.WebhookEndpointList, forwardURL string) []proxy.EndpointRoute { + endpointRoutes := make([]proxy.EndpointRoute, 0) for _, endpoint := range endpoints.Data { u, err := url.Parse(endpoint.URL) // Silently skip over invalid paths if err == nil { // Since webhooks in the dashboard may have a more generic url, only extract // the path. We'll use this with `localhost` or with the `--forward-to` flag - endpointsMap[path.Join(forwardURL, u.Path)] = endpoint.EnabledEvents + endpointRoutes = append(endpointRoutes, proxy.EndpointRoute{ + URL: path.Join(forwardURL, u.Path), + EventTypes: endpoint.EnabledEvents, + }) } } - return endpointsMap + return endpointRoutes } // parseURL parses the potentially incomplete URL provided in the configuration diff --git a/pkg/cmd/listen_test.go b/pkg/cmd/listen_test.go index 5926aa616..3826670f3 100644 --- a/pkg/cmd/listen_test.go +++ b/pkg/cmd/listen_test.go @@ -19,7 +19,7 @@ func TestParseUrl(t *testing.T) { assert.Equal(t, "http://localhost:3000", parseURL("3000")) } -func TestBuildEndpointsMap(t *testing.T) { +func TestBuildEndpointRoutes(t *testing.T) { localURL := "http://localhost" endpoint := requests.WebhookEndpoint{ @@ -31,6 +31,8 @@ func TestBuildEndpointsMap(t *testing.T) { Data: []requests.WebhookEndpoint{endpoint}, } - output := buildEndpointsMap(endpointList, localURL) - assert.Equal(t, output["http:/localhost/hooks"], []string{"*"}) + output := buildEndpointRoutes(endpointList, localURL) + assert.Equal(t, 1, len(output)) + assert.Equal(t, output[0].URL, "http:/localhost/hooks") + assert.Equal(t, output[0].EventTypes, []string{"*"}) } diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go index a4f0f1c8a..8c3c55653 100644 --- a/pkg/proxy/proxy.go +++ b/pkg/proxy/proxy.go @@ -21,7 +21,16 @@ import ( // Public types // -// Config provides the cfguration of a Proxy +// EndpointRoute describes a local endpoint's routing configuration. +type EndpointRoute struct { + // URL is the endpoint's URL. + URL string + + // EventTypes is the list of event types that should be sent to the endpoint. + EventTypes []string +} + +// Config provides the configuration of a Proxy type Config struct { // DeviceName is the name of the device sent to Stripe to help identify the device DeviceName string @@ -30,7 +39,7 @@ type Config struct { Key string // EndpointsMap is a mapping of local webhook endpoint urls to the events they consume - EndpointsMap map[string][]string + EndpointRoutes []EndpointRoute APIBaseURL string @@ -223,11 +232,11 @@ func New(cfg *Config) *Proxy { interruptCh: make(chan os.Signal, 1), } - for url, events := range cfg.EndpointsMap { + for _, route := range cfg.EndpointRoutes { // append to endpointClients p.endpointClients = append(p.endpointClients, NewEndpointClient( - url, - events, + route.URL, + route.EventTypes, &EndpointConfig{ Log: p.cfg.Log, ResponseHandler: EndpointResponseHandlerFunc(p.processEndpointResponse),