Skip to content

Commit

Permalink
Merge pull request #3 from Codigami/url-in-message
Browse files Browse the repository at this point in the history
Lookup endpoint url inside the message by default
  • Loading branch information
ApsOps authored Jul 31, 2017
2 parents c16ce1f + 0504295 commit 9d289be
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cmd

import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -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(&parallelRequests, "parallel", 1, "Number of messages to be consumed in parallel")
RootCmd.MarkPersistentFlagRequired("queuename")
RootCmd.MarkPersistentFlagRequired("url")

httpClient = &http.Client{}

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

0 comments on commit 9d289be

Please sign in to comment.