Ejemplo n.º 1
0
func main() {

	//json.NewDecoder(r.Body).Decode(&c)
	jstr := `{"firstName": "Infosys", "lastName": "Inc.", "emailAddress": "*****@*****.**", "Street": "Canary Wharf", "City": "London", "Postcode": "E14 5NP"}`

	c := models.Customer{}
	json.Unmarshal([]byte(jstr), &c)
	c.Id = bson.NewObjectId()
	//cj, _ := json.Marshal(c)

	opt := models.OptCinfo{}
	opt.Opt = "INSERT"
	opt.Customer = &c
	optj1, _ := json.Marshal(opt)

	//reply, _ := zmq_req_byte("tcp://localhost:5555", optj1)
	//zmq_req("tcp://localhost:5555", string(optj1))

	reply, _ := zmqClient.Req_bytes("tcp://localhost:5555", optj1)
	log.Println(42, reply)

	c2 := models.Customer{}
	json.Unmarshal(reply, &c2)
	log.Println(41, c2)
}
Ejemplo n.º 2
0
func (cc CustomerController) GetCustomer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	id := p.ByName("id")

	if !bson.IsObjectIdHex(id) {
		w.WriteHeader(404)
		return
	}

	oid := bson.ObjectIdHex(id)

	opt := models.OptKey{}
	opt.Opt = "GET"
	opt.Id = oid
	optj, _ := json.Marshal(opt)

	cj, err := zmqClient.Req_bytes(zmq_broker_frontend_url_port, optj)

	if err != nil {
		log.Println(id, err.Error())
		w.WriteHeader(404)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)
	fmt.Fprintf(w, "%s", cj)
}
Ejemplo n.º 3
0
func (cc CustomerController) RemoveCustomer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	id := p.ByName("id")

	if !bson.IsObjectIdHex(id) {
		w.WriteHeader(404)
		return
	}

	oid := bson.ObjectIdHex(id)

	opt := models.OptKey{}
	opt.Opt = "GET"
	opt.Id = oid
	optj, _ := json.Marshal(opt)

	if _, err := zmqClient.Req_bytes(zmq_broker_frontend_url_port, optj); err != nil {
		log.Println(id, err.Error())
		w.WriteHeader(404)
		return
	}
	w.WriteHeader(200)
}
Ejemplo n.º 4
0
//send customer info to backend service via zmq
func (cc CustomerController) CreateCustomer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {

	c := models.Customer{}
	json.NewDecoder(r.Body).Decode(&c)
	//c.Id = bson.NewObjectId()

	opt := models.OptCinfo{}
	opt.Opt = "INSERT"
	opt.Customer = &c
	optj, _ := json.Marshal(opt)

	reply, err := zmqClient.Req_bytes(zmq_broker_frontend_url_port, optj)
	if err != nil {
		log.Println(c.Id, err.Error())
		w.WriteHeader(404)
		return
	}

	cj, _ := json.Marshal(reply)
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(201)

	fmt.Fprintf(w, "%s", cj)
}