Skip to content

Commit

Permalink
Replace endpoints map with list of endpoint routes (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Aug 7, 2019
1 parent ef36972 commit bc857f4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
22 changes: 14 additions & 8 deletions pkg/cmd/listen.go
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -109,15 +112,15 @@ 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")
}

p := proxy.New(&proxy.Config{
DeviceName: deviceName,
Key: key,
EndpointsMap: endpointsMap,
EndpointRoutes: endpointRoutes,
APIBaseURL: lc.apiBaseURL,
WebSocketFeature: webhooksWebSocketFeature,
PrintJSON: lc.printJSON,
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions pkg/cmd/listen_test.go
Expand Up @@ -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{
Expand All @@ -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{"*"})
}
19 changes: 14 additions & 5 deletions pkg/proxy/proxy.go
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit bc857f4

Please sign in to comment.