Beispiel #1
0
func init() {
	setup()
	var wrap data.Wrapper
	var msg data.Message
	wrap.Action = "Send"
	wrap.QueueID = "main"
	msg.Uuid = uuid.NewRandom().String()
	msg.Body = "Some exciting stuff"
	msg.Created = time.Now().Unix()
	msg.Persistent = true
	wrap.Message = msg
}
Beispiel #2
0
// DeliverMessage takes POST JSON data containing a message wrapper which is then processed.
func DeliverMessage(w http.ResponseWriter, r *http.Request, context *data.Context) {

	// Get the data from the request body and decode the JSON into a MessageWrapper
	defer r.Body.Close()
	var mw data.Wrapper
	if err := json.NewDecoder(r.Body).Decode(&mw); err != nil {
		mr := MessageResponse{
			Received:    false,
			ErrorString: "Unable to decode JSON : " + err.Error(),
		}
		if err := json.NewEncoder(w).Encode(&mr); err != nil {
			log.Printf("Unable to send response back to client : %v\n", err)
		}
		return
	}

	// If the message doesn't already have an id, generate one.
	if mw.Message.Uuid == "" {
		u := uuid.NewRandom()
		mw.Message.Uuid = u.String()
	}

	// Send a response back to the client to confirm receipt of the message
	mr := MessageResponse{
		Received:  true,
		MessageId: mw.Message.Uuid,
	}
	if err := json.NewEncoder(w).Encode(&mr); err != nil {
		log.Printf("Unable to send response back to client : %v\n", err)
		return
	}

	// Check to see whether a queue has been specified, if not, set it to default.
	if mw.QueueID == "" {
		mw.QueueID = "default"
	}

	// Queue the message for delivery.
	context.InFlight <- &mw
}