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 }
func pub() { msg := client.NewPublication("topic.go.micro.srv.example", &example.Message{ Say: "This is a publication", }) // create context with metadata ctx := c.WithMetadata(context.Background(), map[string]string{ "X-User-Id": "john", "X-From-Id": "script", }) // publish message if err := client.Publish(ctx, msg); err != nil { fmt.Println("pub err: ", err) return } fmt.Printf("Published: %v\n", msg) }