Created
May 29, 2019 16:22
-
-
Save slav123/2aff70f32b5bd2f7d3e43da11e0ccc19 to your computer and use it in GitHub Desktop.
AWS SNS SubscriptionConfirmation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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