Exemplo n.º 1
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
}
Exemplo n.º 2
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
}
Exemplo n.º 3
0
func (s *Say) Hello(ctx context.Context, req *hello.Request, rsp *hello.Response) error {
	log.Info("Received Say.Hello request")

	rsp.Msg = server.Config().Id() + ": Hello " + req.Name

	return nil
}
Exemplo n.º 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
}
Exemplo n.º 5
0
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {

	// Endpoint construction
	var ep endpoint.Endpoint
	ep = func() error {
		md, _ := c.GetMetadata(ctx)
		log.Infof("Received Example.Call request with metadata: %v", md)
		rsp.Msg = server.Config().Id() + ": Hello " + req.Name
		return nil
	}

	// Including middlewares
	ep = ratelimit.NewTokenBucketLimiter(testBucket)(ep)
	ep = circuitbreaker.Hystrix("test_command", ep, nil, 0, 50, 1000, 0, 0)(ep)

	// Excuting
	ep()

	return nil

}
Exemplo n.º 6
0
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
	md, _ := c.GetMetadata(ctx)
	log.Infof("Received Example.Call request with metadata: %v", md)
	rsp.Msg = server.Config().Id() + ": Hello " + req.Name
	return nil
}