// PubSubMessage is the payload of a Pub/Sub event.
// See the documentation for more details:
// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
type PubSubMessage struct {
Message struct {
Data []byte `json:"data,omitempty"`
ID string `json:"id"`
} `json:"message"`
Subscription string `json:"subscription"`
}
// HelloPubSub receives and processes a Pub/Sub push message.
func HelloPubSub(w http.ResponseWriter, r *http.Request) {
var m PubSubMessage
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("ioutil.ReadAll: %v", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
if err := json.Unmarshal(body, &m); err != nil {
log.Printf("json.Unmarshal: %v", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
name := string(m.Message.Data)
if name == "" {
name = "World"
}
log.Printf("Hello %s!", name)
}