Skip to content

Commit

Permalink
Add option to derive queueUrl from incoming request, rather than conf…
Browse files Browse the repository at this point in the history
…ig/default.
  • Loading branch information
nhoughto committed Mar 4, 2020
1 parent 669d010 commit 2f32176
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type EnvQueueAttributes struct {
type Environment struct {
Host string
Port string
DeriveHostAndPort bool
SqsPort string
SnsPort string
Region string
Expand Down
9 changes: 9 additions & 0 deletions app/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"net/http"
"hash"
"io"
"sort"
"strings"
log "github.com/sirupsen/logrus"

"github.com/p4tin/goaws/app"
)
Expand Down Expand Up @@ -58,6 +61,12 @@ func HashAttributes(attributes map[string]app.MessageAttributeValue) string {
return hex.EncodeToString(hasher.Sum(nil))
}

func DeriveQueueUrl(queueUrl string, req *http.Request) string {
derivedQueueUrl := strings.Replace(queueUrl, app.CurrentEnvironment.Host + ":" + app.CurrentEnvironment.Port, req.Host, -1)
log.Debugf("Derived new queue URL: %s from request: %s with original: %s", derivedQueueUrl, req.Host, queueUrl)
return derivedQueueUrl
}

func sortedKeys(attributes map[string]app.MessageAttributeValue) []string {
var keys []string
for key := range attributes {
Expand Down
11 changes: 10 additions & 1 deletion app/gosqs/gosqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ func ListQueues(w http.ResponseWriter, req *http.Request) {
for _, queue := range app.SyncQueues.Queues {
app.SyncQueues.RLock()
if strings.HasPrefix(queue.Name, queueNamePrefix) {
respStruct.Result.QueueUrl = append(respStruct.Result.QueueUrl, queue.URL)
queueUrl := queue.URL
// If derive host is enabled, generate it dynamically from the incoming requests, solves for multiple ingress paths.
if app.CurrentEnvironment.DeriveHostAndPort {
queueUrl = common.DeriveQueueUrl(queueUrl, req)
}
respStruct.Result.QueueUrl = append(respStruct.Result.QueueUrl, queueUrl)
}
app.SyncQueues.RUnlock()
}
Expand Down Expand Up @@ -754,6 +759,10 @@ func GetQueueUrl(w http.ResponseWriter, req *http.Request) {
queueName := req.FormValue("QueueName")
if queue, ok := app.SyncQueues.Queues[queueName]; ok {
url := queue.URL
// If derive host is enabled, generate it dynamically from the incoming requests, solves for multiple ingress paths.
if app.CurrentEnvironment.DeriveHostAndPort {
url = common.DeriveQueueUrl(url, req)
}
log.Infof("Get Queue URL: %s", queueName)
// Create, encode/xml and send response
result := app.GetQueueUrlResult{QueueUrl: url}
Expand Down
3 changes: 2 additions & 1 deletion app/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"

"github.com/gorilla/mux"
app "github.com/p4tin/goaws/app"
sns "github.com/p4tin/goaws/app/gosns"
sqs "github.com/p4tin/goaws/app/gosqs"
)
Expand Down Expand Up @@ -73,7 +74,7 @@ func actionHandler(w http.ResponseWriter, req *http.Request) {
}).Debug("Handling URL request")
fn, ok := routingTable[req.FormValue("Action")]
if !ok {
log.Println("Bad Request - Action:", req.FormValue("Action"))
log.Warnf("Bad Request - Action: %s", req.FormValue("Action"))
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "Bad Request")
return
Expand Down

0 comments on commit 2f32176

Please sign in to comment.