func publishHandler(w http.ResponseWriter, r *http.Request) {
	id, err := sessionHandling(w, r)
	if err != nil {
		log.Printf("error in session handling (%v)", err)
		fmt.Fprintf(w, "Error: %v", err)
		return
	}
	decoder := json.NewDecoder(r.Body)
	evt := susigo.Event{}
	err = decoder.Decode(&evt)
	if err != nil {
		log.Printf("error while decoding event from http-publish-request body: %v", err)
		w.WriteHeader(400)
		w.Write([]byte("malformed payload"))
		return
	}
	if evt.Topic == "" {
		log.Printf("error while decoding event from http-publish-request body: %v", "topic empty")
		w.WriteHeader(400)
		w.Write([]byte("you MUST specify at least a topic for your event"))
		return
	}
	evt.SessionID = id
	ready := make(chan bool)

	susi.Publish(evt, func(evt *susigo.Event) {
		encoder := json.NewEncoder(w)
		encoder.Encode(evt)
		ready <- true
	})

	log.Printf("publish event with topic '%v' for session %v via http", evt.Topic, id)
	<-ready
}
Example #2
0
func main() {
	flag.Parse()
	s, err := susigo.NewSusi(*susiaddr, *cert, *key)
	if err != nil {
		log.Printf("Error while creating susi connection: %v", err)
		return
	}
	susi = s
	log.Println("successfully create susi connection")

	file, err := os.Open(*mappingFile)
	if err != nil {
		log.Printf("Error while opening the mapping file: %v", err)
		return
	}
	dec := json.NewDecoder(file)
	var mapping map[string]map[string]string
	err = dec.Decode(&mapping)
	if err != nil {
		log.Printf("Error while decoding the mapping file: %v", err)
		return
	}

	r := mux.NewRouter()

	for endpoint, specifier := range mapping {
		var spec = specifier
		r.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
			topic := spec[r.Method]
			if _, ok := spec[r.Method]; !ok {
				w.WriteHeader(http.StatusNotFound)
				return
			}
			event := susigo.Event{
				Topic: topic,
			}
			payload := make(map[string]interface{})
			for key, value := range mux.Vars(r) {
				payload[key] = value
			}
			event.Payload = payload
			if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" {
				bodyPayload := make(map[string]interface{})
				payloadDecoder := json.NewDecoder(r.Body)
				err = payloadDecoder.Decode(&bodyPayload)
				if err != nil {
					w.WriteHeader(http.StatusNotAcceptable)
					fmt.Fprintf(w, "Error while parsing body: %v", err)
					return
				}
				for key, value := range bodyPayload {
					payload[key] = value
				}
				event.Payload = payload
			}
			result := make(chan interface{})
			err = susi.Publish(event, func(event *susigo.Event) {
				result <- event.Payload
			})
			if err != nil {
				w.WriteHeader(http.StatusInternalServerError)
				fmt.Fprintf(w, "Error while publishing event: %v", err)
				return
			}
			resultPayload := <-result
			w.Header().Set("Content-Type", "application/json")
			enc := json.NewEncoder(w)
			enc.Encode(resultPayload)
		})
	}

	http.Handle("/", r)

	log.Printf("starting REST server on %v...", *webaddr)
	if *useHTTPS {
		log.Fatal(http.ListenAndServeTLS(*webaddr, *cert, *key, context.ClearHandler(http.DefaultServeMux)))
	} else {
		log.Fatal(http.ListenAndServe(*webaddr, context.ClearHandler(http.DefaultServeMux)))
	}
}