Skip to content

Instantly share code, notes, and snippets.

@slav123
Created May 29, 2019 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slav123/2aff70f32b5bd2f7d3e43da11e0ccc19 to your computer and use it in GitHub Desktop.
Save slav123/2aff70f32b5bd2f7d3e43da11e0ccc19 to your computer and use it in GitHub Desktop.
AWS SNS SubscriptionConfirmation
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const subConfrmType = "SubscriptionConfirmation"
const notificationType = "Notification"
//confirmSubscription Confirms Subscription by makeing get request to Subscribe URL
func confirmSubscription(subcribeURL string) {
response, err := http.Get(subcribeURL)
if err != nil {
fmt.Printf("Unbale to confirm subscriptions")
} else {
fmt.Printf("Subscription Confirmed sucessfully. %d", response.StatusCode)
}
}
//handler processes messages sent by SNS
func handler(w http.ResponseWriter, r *http.Request) {
var f interface{}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Printf("Unable to Parse Body")
}
fmt.Printf(string(body))
err = json.Unmarshal(body, &f)
if err != nil {
fmt.Printf("Unable to Unmarshal request")
}
data := f.(map[string]interface{})
fmt.Println(data["Type"].(string))
if data["Type"].(string) == subConfrmType {
subcribeURL := data["SubscribeURL"].(string)
go confirmSubscription(subcribeURL)
} else if data["Type"].(string) == notificationType {
fmt.Println("Recieved this message : ", data["Message"].(string))
}
fmt.Fprintf(w, "Sucess")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8081", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment