Exemple #1
0
func (s *Say) Hello(ctx context.Context, req *api.Request, rsp *api.Response) error {
	log.Info("Received Say.Hello API request")

	name, ok := req.Get["name"]
	if !ok || len(name.Values) == 0 {
		return errors.BadRequest("go.micro.api.greeter", "Name cannot be blank")
	}

	request := client.NewRequest("go.micro.srv.greeter", "Say.Hello", &hello.Request{
		Name: strings.Join(name.Values, " "),
	})

	response := &hello.Response{}

	if err := client.Call(ctx, request, response); err != nil {
		return err
	}

	rsp.StatusCode = 200
	b, _ := json.Marshal(map[string]string{
		"message": response.Msg,
	})
	rsp.Body = string(b)

	return nil
}
Exemple #2
0
func (l *Location) Read(ctx context.Context, req *api.Request, rsp *api.Response) error {
	id := extractValue(req.Post["id"])

	if len(id) == 0 {
		return errors.BadRequest(server.Config().Name()+".read", "Require Id")
	}

	request := client.NewRequest("go.micro.srv.geo", "Location.Read", &read.Request{
		Id: id,
	})

	response := &read.Response{}

	err := client.Call(ctx, request, response)
	if err != nil {
		return errors.InternalServerError(server.Config().Name()+".read", "failed to read location")
	}

	b, _ := json.Marshal(response.Entity)

	rsp.StatusCode = 200
	rsp.Body = string(b)

	return nil
}
Exemple #3
0
func (l *Location) Save(ctx context.Context, req *api.Request, rsp *api.Response) error {
	var latlon map[string]float64
	err := json.Unmarshal([]byte(extractValue(req.Post["location"])), &latlon)
	if err != nil {
		return errors.BadRequest(server.Config().Name()+".search", "invalid location")
	}

	unix, _ := strconv.ParseInt(extractValue(req.Post["timestamp"]), 10, 64)

	entity := &proto.Entity{
		Id:   extractValue(req.Post["id"]),
		Type: extractValue(req.Post["type"]),
		Location: &proto.Location{
			Latitude:  latlon["latitude"],
			Longitude: latlon["longitude"],
			Timestamp: time.Unix(unix, 0).Unix(),
		},
	}

	if len(entity.Id) == 0 {
		return errors.BadRequest(server.Config().Name()+".save", "ID cannot be blank")
	}

	p := client.NewPublication(topic, entity)

	if err := client.Publish(ctx, p); err != nil {
		log.Errorf("Error publishing to topic %s: %v", topic, err)
		return errors.InternalServerError(server.Config().Name()+".save", err.Error())
	}

	log.Infof("Publishing entity ID %s", entity.Id)
	rsp.StatusCode = 200
	rsp.Body = `{}`
	return nil
}
Exemple #4
0
func (l *Location) Search(ctx context.Context, req *api.Request, rsp *api.Response) error {
	radius, _ := strconv.ParseFloat(extractValue(req.Post["radius"]), 64)
	typ := extractValue(req.Post["type"])
	entities, _ := strconv.ParseInt(extractValue(req.Post["num_entities"]), 10, 64)

	var latlon map[string]float64
	err := json.Unmarshal([]byte(extractValue(req.Post["center"])), &latlon)
	if err != nil {
		return errors.BadRequest(server.Config().Name()+".search", "invalid center point")
	}

	if len(typ) == 0 {
		return errors.BadRequest(server.Config().Name()+".search", "type cannot be blank")
	}

	if entities == 0 {
		return errors.BadRequest(server.Config().Name()+".search", "num_entities must be greater than 0")
	}

	request := client.NewRequest("go.micro.srv.geo", "Location.Search", &search.Request{
		Center: &common.Location{
			Latitude:  latlon["latitude"],
			Longitude: latlon["longitude"],
		},
		Radius:      radius,
		NumEntities: entities,
		Type:        typ,
	})

	response := &search.Response{}

	err = client.Call(ctx, request, response)
	if err != nil {
		return errors.InternalServerError(server.Config().Name()+".search", "could not retrieve results")
	}

	b, _ := json.Marshal(response.Entities)
	rsp.StatusCode = 200
	rsp.Body = string(b)
	return nil
}