// this handles messages published to "pubsub-a-topic"
this handles messages published to "pubsub-a-topic"
// this handles messages published to " pubsub - a - topic "
func subscribeHandler(w http.ResponseWriter, r *http.Request) {
	log.Printf("aHandler is called %s\n", r.URL)
	w.WriteHeader(http.StatusOK)
	defer r.Body.Close()
	var err error
	var data []byte
	var body []byte
	if r.Body != nil {
		if data, err = ioutil.ReadAll(r.Body); err == nil {
			body = data
			log.Printf("assigned\n")
		}
	} else {
		err = errors.New("r.Body is nil")
	}
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(appResponse{
			Message: err.Error(),
		})
		return
	}
	msg, err := extractMessage(body)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(appResponse{
			Message: err.Error(),
		})
		return
	}
	lock.Lock()
	defer lock.Unlock()
	if strings.HasSuffix(r.URL.String(), pubsubA) {
		receivedMessagesA = append(receivedMessagesA, msg)
	} else if strings.HasSuffix(r.URL.String(), pubsubB) {
		receivedMessagesB = append(receivedMessagesB, msg)
	} else {
		errorMessage := fmt.Sprintf("Unexpected message from %s", r.URL.String())
		log.Print(errorMessage)
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(appResponse{
			Message: errorMessage,
		})
		return
	}
	json.NewEncoder(w).Encode(appResponse{
		Message: "success",
	})
}
func subscribeHandler ( w http . ResponseWriter , r * http . Request ) { log . Printf ( " \n " , r . URL ) w . WriteHeader ( http . StatusOK ) defer r . Body . Close ( ) var err error var data [ ] byte var body [ ] byte if r . Body != nil { if data , err = ioutil . ReadAll ( r . Body ) ; err == nil { body = data log . Printf ( " \n " ) } } else { err = errors . New ( " " ) } if err != nil { w . WriteHeader ( http . StatusInternalServerError ) json . NewEncoder ( w ) . Encode ( appResponse { Message : err . Error ( ) , } ) return } msg , err := extractMessage ( body ) if err != nil { w . WriteHeader ( http . StatusInternalServerError ) json . NewEncoder ( w ) . Encode ( appResponse { Message : err . Error ( ) , } ) return } lock . Lock ( ) defer lock . Unlock ( ) if strings . HasSuffix ( r . URL . String ( ) , pubsubA ) { receivedMessagesA = append ( receivedMessagesA , msg ) } else if strings . HasSuffix ( r . URL . String ( ) , pubsubB ) { receivedMessagesB = append ( receivedMessagesB , msg ) } else { errorMessage := fmt . Sprintf ( " " , r . URL . String ( ) ) log . Print ( errorMessage ) w . WriteHeader ( http . StatusInternalServerError ) json . NewEncoder ( w ) . Encode ( appResponse { Message : errorMessage , } ) return } json . NewEncoder ( w ) . Encode ( appResponse { Message : " " , } ) }
this handles messages published to "pubsub-a-topic"
this handles messages published to " pubsub - a - topic "
// error