From 050429519f1653e35a68086db3779c82382d2e37 Mon Sep 17 00:00:00 2001 From: Aman Date: Mon, 31 Jul 2017 12:32:55 +0530 Subject: [PATCH] Lookup endpoint url inside the message by default --- cmd/root.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 64947cc..bb7c68a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,6 +22,7 @@ package cmd import ( "bytes" + "encoding/json" "io/ioutil" "log" "net/http" @@ -60,12 +61,11 @@ func Execute() { func init() { RootCmd.PersistentFlags().StringVarP(&queueName, "queue-name", "q", "", "queue name to use") - RootCmd.PersistentFlags().StringVarP(&url, "url", "u", "", "endpoint to send an HTTP POST request with contents of queue message in the body") + RootCmd.PersistentFlags().StringVarP(&url, "url", "u", "", "endpoint to send an HTTP POST request with contents of queue message in the body. Takes the URL from the message by default") RootCmd.PersistentFlags().StringVar(&awsRegion, "aws-region", "us-east-1", "AWS Region for the SQS queue") RootCmd.PersistentFlags().StringVar(&sqsEndpoint, "sqs-endpoint", "", "SQS Endpoint for using with fake_sqs") RootCmd.PersistentFlags().IntVar(¶llelRequests, "parallel", 1, "Number of messages to be consumed in parallel") RootCmd.MarkPersistentFlagRequired("queuename") - RootCmd.MarkPersistentFlagRequired("url") httpClient = &http.Client{} @@ -148,8 +148,24 @@ func sendMessageToURL(msg string) bool { var resp *http.Response var err error + endpoint := url + + if endpoint == "" { + m := make(map[string]string) + err := json.Unmarshal([]byte(msg), &m) + if err != nil { + log.Printf("Unable to parse JSON message to get the URL: %s", msg) + return false + } + endpoint = m["url"] + if endpoint == "" { + log.Printf("No 'url' field found in JSON message: %s", msg) + return false + } + } + for { - resp, err = httpClient.Post(url, "application/json", bytes.NewBuffer([]byte(msg))) + resp, err = httpClient.Post(endpoint, "application/json", bytes.NewBuffer([]byte(msg))) if err == nil { break }