Locations Service (#34)

* Locations Proto

* Add Read RPC

* Locations Service

* Add read locks
This commit is contained in:
ben-toogood
2021-01-08 09:20:22 +00:00
committed by GitHub
parent 9971d97582
commit de2c437c41
14 changed files with 1770 additions and 0 deletions

1
locations/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
locations

18
locations/Makefile Normal file
View File

@@ -0,0 +1,18 @@
GOPATH:=$(shell go env GOPATH)
.PHONY: init
init:
go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
.PHONY: proto
proto:
protoc --proto_path=. --micro_out=. --go_out=:. proto/locations.proto
.PHONY: build
build:
go build -o locations *.go
.PHONY: test
test:
go test -v ./... -cover

23
locations/README.md Normal file
View File

@@ -0,0 +1,23 @@
# Locations Service
This is the Locations service
Generated with
```
micro new locations
```
## Usage
Generate the proto code
```
make proto
```
Run the service
```
micro run .
```

2
locations/generate.go Normal file
View File

@@ -0,0 +1,2 @@
package main
//go:generate make proto

View File

@@ -0,0 +1,177 @@
package handler
import (
"context"
"sync"
"time"
"github.com/google/uuid"
geo "github.com/hailocab/go-geoindex"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
"gorm.io/gorm"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/services/locations/model"
pb "github.com/micro/services/locations/proto"
)
var (
ErrMissingLocations = errors.BadRequest("MISSING_LOCATIONS", "One or more locations are required")
ErrMissingLatitude = errors.BadRequest("MISSING_LATITUDE", "Latitude is required")
ErrMissingLongitude = errors.BadRequest("MISSING_LONGITUDE", "Longitude is required")
ErrMissingUserID = errors.BadRequest("MISSING_USER_ID", "UserID is required")
ErrMissingUserIDs = errors.BadRequest("MISSING_USER_IDS", "One or more UserIDs are required")
ErrMissingBefore = errors.BadRequest("MISSING_BEFORE", "Before timestamp is required")
ErrMissingAfter = errors.BadRequest("MISSING_AFTER", "After timestamp is required")
ErrMissingRadius = errors.BadRequest("MISSING_RADIUS", "Radius is required")
)
type Locations struct {
sync.RWMutex
Geoindex *geo.PointsIndex
DB *gorm.DB
}
// Save a set of locations
func (l *Locations) Save(ctx context.Context, req *pb.SaveRequest, rsp *pb.SaveResponse) error {
// validate the request
if len(req.Locations) == 0 {
return ErrMissingLocations
}
for _, l := range req.Locations {
if l.Latitude == nil {
return ErrMissingLatitude
}
if l.Longitude == nil {
return ErrMissingLongitude
}
if len(l.UserId) == 0 {
return ErrMissingUserID
}
}
// construct the database objects
ls := make([]*model.Location, len(req.Locations))
for i, lc := range req.Locations {
loc := &model.Location{
ID: uuid.New().String(),
UserID: lc.UserId,
Latitude: lc.Latitude.Value,
Longitude: lc.Longitude.Value,
}
if lc.Timestamp != nil {
loc.Timestamp = lc.Timestamp.AsTime()
} else {
loc.Timestamp = time.Now()
}
ls[i] = loc
}
// write to the database
if err := l.DB.Create(ls).Error; err != nil {
logger.Errorf("Error writing to the database: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error writing to the database")
}
// write to the geoindex
l.Lock()
defer l.Unlock()
for _, lc := range ls {
l.Geoindex.Add(lc)
}
return nil
}
// Last locations for a set of users
func (l *Locations) Last(ctx context.Context, req *pb.LastRequest, rsp *pb.ListResponse) error {
// validate the request
if req.UserIds == nil {
return ErrMissingUserIDs
}
// query the database
q := l.DB.Raw("SELECT DISTINCT ON (user_id) user_id, timestamp, latitude, longitude FROM locations WHERE user_id IN (?) ORDER BY user_id, timestamp DESC", req.UserIds)
var locs []*model.Location
if err := q.Find(&locs).Error; err != nil {
logger.Errorf("Error reading from the database: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error reading from the database")
}
// serialize the result
rsp.Locations = serializeLocations(locs)
return nil
}
// Near returns the locations near a point
func (l *Locations) Near(ctx context.Context, req *pb.NearRequest, rsp *pb.ListResponse) error {
// validate the request
if req.Latitude == nil {
return ErrMissingLatitude
}
if req.Longitude == nil {
return ErrMissingLongitude
}
if req.Radius == nil {
return ErrMissingRadius
}
// query the geoindex
l.RLock()
p := geo.NewGeoPoint("query", req.Latitude.Value, req.Longitude.Value)
result := l.Geoindex.PointsWithin(p, geo.Km(req.Radius.Value), func(p geo.Point) bool {
return true
})
l.RUnlock()
// serialize the result
locs := make([]*model.Location, len(result))
for i, r := range result {
locs[i] = r.(*model.Location)
}
rsp.Locations = serializeLocations(locs)
return nil
}
// Read locations for a group of users between two points in time
func (l *Locations) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ListResponse) error {
// validate the request
if len(req.UserIds) == 0 {
return ErrMissingUserIDs
}
if req.Before == nil {
return ErrMissingBefore
}
if req.After == nil {
return ErrMissingAfter
}
// construct the request
q := l.DB.Model(&model.Location{})
q = q.Order("timestamp ASC")
q = q.Where("user_id IN (?) AND timestamp > ? AND timestamp < ?", req.UserIds, req.After.AsTime(), req.Before.AsTime())
var locs []*model.Location
if err := q.Find(&locs).Error; err != nil {
logger.Errorf("Error reading from the database: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error reading from the database")
}
// serialize the result
rsp.Locations = serializeLocations(locs)
return nil
}
func serializeLocations(locs []*model.Location) []*pb.Location {
rsp := make([]*pb.Location, len(locs))
for i, l := range locs {
rsp[i] = &pb.Location{
UserId: l.UserID,
Latitude: &wrapperspb.DoubleValue{Value: l.Latitude},
Longitude: &wrapperspb.DoubleValue{Value: l.Longitude},
Timestamp: timestamppb.New(l.Timestamp),
}
}
return rsp
}

View File

@@ -0,0 +1,519 @@
package handler_test
import (
"context"
"sort"
"testing"
"time"
"github.com/google/uuid"
geo "github.com/hailocab/go-geoindex"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/micro/services/locations/handler"
"github.com/micro/services/locations/model"
pb "github.com/micro/services/locations/proto"
)
func testHandler(t *testing.T) pb.LocationsHandler {
// connect to the database
db, err := gorm.Open(postgres.Open("postgresql://postgres@localhost:5432/locations?sslmode=disable"), &gorm.Config{})
if err != nil {
t.Fatalf("Error connecting to database: %v", err)
}
// migrate the database
if err := db.AutoMigrate(&model.Location{}); err != nil {
t.Fatalf("Error migrating database: %v", err)
}
// clean any data from a previous run
if err := db.Exec("TRUNCATE TABLE locations CASCADE").Error; err != nil {
t.Fatalf("Error cleaning database: %v", err)
}
return &handler.Locations{DB: db, Geoindex: geo.NewPointsIndex(geo.Km(0.1))}
}
func TestSave(t *testing.T) {
tt := []struct {
Name string
Locations []*pb.Location
Error error
}{
{
Name: "NoLocations",
Error: handler.ErrMissingLocations,
},
{
Name: "NoLatitude",
Locations: []*pb.Location{
{
Longitude: &wrapperspb.DoubleValue{Value: -0.1246},
UserId: uuid.New().String(),
},
},
Error: handler.ErrMissingLatitude,
},
{
Name: "NoLongitude",
Locations: []*pb.Location{
{
Latitude: &wrapperspb.DoubleValue{Value: -0.1246},
UserId: uuid.New().String(),
},
},
Error: handler.ErrMissingLongitude,
},
{
Name: "OneLocation",
Locations: []*pb.Location{
{
Latitude: &wrapperspb.DoubleValue{Value: 51.5007},
Longitude: &wrapperspb.DoubleValue{Value: 0.1246},
Timestamp: timestamppb.New(time.Now()),
UserId: uuid.New().String(),
},
},
},
{
Name: "ManyLocations",
Locations: []*pb.Location{
{
Latitude: &wrapperspb.DoubleValue{Value: 51.5007},
Longitude: &wrapperspb.DoubleValue{Value: 0.1246},
Timestamp: timestamppb.New(time.Now()),
UserId: uuid.New().String(),
},
{
Latitude: &wrapperspb.DoubleValue{Value: 51.003},
Longitude: &wrapperspb.DoubleValue{Value: -0.1246},
UserId: uuid.New().String(),
},
},
},
}
h := testHandler(t)
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
err := h.Save(context.Background(), &pb.SaveRequest{
Locations: tc.Locations,
}, &pb.SaveResponse{})
assert.Equal(t, tc.Error, err)
})
}
}
func TestLast(t *testing.T) {
h := testHandler(t)
t.Run("MissingUserIDs", func(t *testing.T) {
err := h.Last(context.Background(), &pb.LastRequest{}, &pb.ListResponse{})
assert.Equal(t, handler.ErrMissingUserIDs, err)
})
t.Run("NoMatches", func(t *testing.T) {
var rsp pb.ListResponse
err := h.Last(context.Background(), &pb.LastRequest{
UserIds: []string{uuid.New().String()},
}, &rsp)
assert.NoError(t, err)
assert.Empty(t, rsp.Locations)
})
// generate some example data to work with
loc1 := &pb.Location{
Latitude: &wrapperspb.DoubleValue{Value: 51.5007},
Longitude: &wrapperspb.DoubleValue{Value: 0.1246},
Timestamp: timestamppb.New(time.Now()),
UserId: "a",
}
loc2 := &pb.Location{
Latitude: &wrapperspb.DoubleValue{Value: 51.6007},
Longitude: &wrapperspb.DoubleValue{Value: 0.1546},
Timestamp: timestamppb.New(time.Now()),
UserId: "b",
}
loc3 := &pb.Location{
Latitude: &wrapperspb.DoubleValue{Value: 52.6007},
Longitude: &wrapperspb.DoubleValue{Value: 0.2546},
Timestamp: timestamppb.New(time.Now()),
UserId: loc2.UserId,
}
err := h.Save(context.TODO(), &pb.SaveRequest{
Locations: []*pb.Location{loc1, loc2, loc3},
}, &pb.SaveResponse{})
assert.NoError(t, err)
t.Run("OneUser", func(t *testing.T) {
var rsp pb.ListResponse
err := h.Last(context.Background(), &pb.LastRequest{
UserIds: []string{loc2.UserId},
}, &rsp)
assert.NoError(t, err)
if len(rsp.Locations) != 1 {
t.Fatalf("One location should be returned")
}
assert.Equal(t, loc3.UserId, rsp.Locations[0].UserId)
assert.Equal(t, loc3.Latitude.Value, rsp.Locations[0].Latitude.Value)
assert.Equal(t, loc3.Longitude.Value, rsp.Locations[0].Longitude.Value)
assert.Equal(t, loc3.Timestamp.AsTime(), rsp.Locations[0].Timestamp.AsTime())
})
t.Run("ManyUser", func(t *testing.T) {
var rsp pb.ListResponse
err := h.Last(context.Background(), &pb.LastRequest{
UserIds: []string{loc1.UserId, loc2.UserId},
}, &rsp)
assert.NoError(t, err)
if len(rsp.Locations) != 2 {
t.Fatalf("Two locations should be returned")
}
// sort using user_id so we can hardcode the index
sort.Slice(rsp.Locations, func(i, j int) bool {
return rsp.Locations[i].UserId > rsp.Locations[j].UserId
})
assert.Equal(t, loc1.UserId, rsp.Locations[1].UserId)
assert.Equal(t, loc1.Latitude.Value, rsp.Locations[1].Latitude.Value)
assert.Equal(t, loc1.Longitude.Value, rsp.Locations[1].Longitude.Value)
assert.Equal(t, loc1.Timestamp.AsTime(), rsp.Locations[1].Timestamp.AsTime())
assert.Equal(t, loc3.UserId, rsp.Locations[0].UserId)
assert.Equal(t, loc3.Latitude.Value, rsp.Locations[0].Latitude.Value)
assert.Equal(t, loc3.Longitude.Value, rsp.Locations[0].Longitude.Value)
assert.Equal(t, loc3.Timestamp.AsTime(), rsp.Locations[0].Timestamp.AsTime())
})
}
func TestNear(t *testing.T) {
lat := &wrapperspb.DoubleValue{Value: 51.510357}
lng := &wrapperspb.DoubleValue{Value: -0.116773}
rad := &wrapperspb.DoubleValue{Value: 2.0}
inBoundsLat := &wrapperspb.DoubleValue{Value: 51.5110}
inBoundsLng := &wrapperspb.DoubleValue{Value: -0.1142}
outOfBoundsLat := &wrapperspb.DoubleValue{Value: 51.5415}
outOfBoundsLng := &wrapperspb.DoubleValue{Value: -0.0028}
tt := []struct {
Name string
Locations []*pb.Location
Results []*pb.Location
QueryLatitude *wrapperspb.DoubleValue
QueryLongitude *wrapperspb.DoubleValue
QueryRadius *wrapperspb.DoubleValue
Error error
}{
{
Name: "MissingLatitude",
QueryLongitude: lng,
QueryRadius: rad,
Error: handler.ErrMissingLatitude,
},
{
Name: "MissingLongitude",
QueryLatitude: lat,
QueryRadius: rad,
Error: handler.ErrMissingLongitude,
},
{
Name: "MissingRadius",
QueryLatitude: lat,
QueryLongitude: lng,
Error: handler.ErrMissingRadius,
},
{
Name: "NoLocations",
QueryLatitude: lat,
QueryLongitude: lng,
QueryRadius: rad,
},
{
Name: "OneWithinRadius",
QueryLatitude: lat,
QueryLongitude: lng,
QueryRadius: rad,
Locations: []*pb.Location{
&pb.Location{
Latitude: inBoundsLat,
Longitude: inBoundsLng,
UserId: "in",
},
&pb.Location{
Latitude: outOfBoundsLat,
Longitude: outOfBoundsLng,
UserId: "out",
},
},
Results: []*pb.Location{
&pb.Location{
Latitude: inBoundsLat,
Longitude: inBoundsLng,
UserId: "in",
},
},
},
{
Name: "NoneWithinRadius",
QueryLatitude: lat,
QueryLongitude: lng,
QueryRadius: &wrapperspb.DoubleValue{Value: 0.01},
Locations: []*pb.Location{
&pb.Location{
Latitude: inBoundsLat,
Longitude: inBoundsLng,
UserId: "in",
},
&pb.Location{
Latitude: outOfBoundsLat,
Longitude: outOfBoundsLng,
UserId: "out",
},
},
},
{
Name: "TwoLocationsForUser",
QueryLatitude: lat,
QueryLongitude: lng,
QueryRadius: rad,
Locations: []*pb.Location{
&pb.Location{
Latitude: inBoundsLat,
Longitude: inBoundsLng,
UserId: "in",
},
&pb.Location{
Latitude: outOfBoundsLat,
Longitude: outOfBoundsLng,
UserId: "out",
},
&pb.Location{
Latitude: inBoundsLat,
Longitude: inBoundsLng,
UserId: "out",
},
},
Results: []*pb.Location{
&pb.Location{
Latitude: inBoundsLat,
Longitude: inBoundsLng,
UserId: "in",
},
&pb.Location{
Latitude: inBoundsLat,
Longitude: inBoundsLng,
UserId: "out",
},
},
},
{
Name: "ManyWithinRadius",
QueryLatitude: lat,
QueryLongitude: lng,
QueryRadius: &wrapperspb.DoubleValue{Value: 20},
Locations: []*pb.Location{
&pb.Location{
Latitude: inBoundsLat,
Longitude: inBoundsLng,
UserId: "in",
},
&pb.Location{
Latitude: outOfBoundsLat,
Longitude: outOfBoundsLng,
UserId: "out",
},
},
Results: []*pb.Location{
&pb.Location{
Latitude: inBoundsLat,
Longitude: inBoundsLng,
UserId: "in",
},
&pb.Location{
Latitude: outOfBoundsLat,
Longitude: outOfBoundsLng,
UserId: "out",
},
},
},
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
h := testHandler(t)
// create the locations
if len(tc.Locations) > 0 {
err := h.Save(context.TODO(), &pb.SaveRequest{Locations: tc.Locations}, &pb.SaveResponse{})
assert.NoError(t, err)
}
// find near locations
var rsp pb.ListResponse
err := h.Near(context.TODO(), &pb.NearRequest{
Latitude: tc.QueryLatitude,
Longitude: tc.QueryLongitude,
Radius: tc.QueryRadius,
}, &rsp)
assert.Equal(t, tc.Error, err)
// check the count of the results matches
if len(tc.Results) != len(rsp.Locations) {
t.Errorf("Incorrect number of results returned. Expected %v, got %v", len(tc.Results), len(rsp.Locations))
}
// validate the results match
sort.Slice(rsp.Locations, func(i, j int) bool {
return rsp.Locations[i].UserId > rsp.Locations[j].UserId
})
sort.Slice(tc.Results, func(i, j int) bool {
return tc.Results[i].UserId > tc.Results[j].UserId
})
for i, r := range tc.Results {
l := rsp.Locations[i]
assert.Equal(t, r.UserId, l.UserId)
assert.Equal(t, r.Latitude.Value, l.Latitude.Value)
assert.Equal(t, r.Longitude.Value, l.Longitude.Value)
}
})
}
}
func TestRead(t *testing.T) {
h := testHandler(t)
baseTime := time.Now().Add(time.Hour * -24)
t.Run("MissingUserIDs", func(t *testing.T) {
err := h.Read(context.Background(), &pb.ReadRequest{
After: timestamppb.New(baseTime),
Before: timestamppb.New(baseTime),
}, &pb.ListResponse{})
assert.Equal(t, handler.ErrMissingUserIDs, err)
})
t.Run("MissingAfter", func(t *testing.T) {
err := h.Read(context.Background(), &pb.ReadRequest{
UserIds: []string{uuid.New().String()},
Before: timestamppb.New(baseTime),
}, &pb.ListResponse{})
assert.Equal(t, handler.ErrMissingAfter, err)
})
t.Run("MissingBefore", func(t *testing.T) {
err := h.Read(context.Background(), &pb.ReadRequest{
UserIds: []string{uuid.New().String()},
After: timestamppb.New(baseTime),
}, &pb.ListResponse{})
assert.Equal(t, handler.ErrMissingBefore, err)
})
// generate some example data to work with
loc1 := &pb.Location{
Latitude: &wrapperspb.DoubleValue{Value: 51.5007},
Longitude: &wrapperspb.DoubleValue{Value: 0.1246},
Timestamp: timestamppb.New(baseTime.Add(time.Minute * 10)),
UserId: "a",
}
loc2 := &pb.Location{
Latitude: &wrapperspb.DoubleValue{Value: 51.6007},
Longitude: &wrapperspb.DoubleValue{Value: 0.1546},
Timestamp: timestamppb.New(baseTime.Add(time.Minute * 20)),
UserId: "b",
}
loc3 := &pb.Location{
Latitude: &wrapperspb.DoubleValue{Value: 52.6007},
Longitude: &wrapperspb.DoubleValue{Value: 0.2546},
Timestamp: timestamppb.New(baseTime.Add(time.Minute * 40)),
UserId: loc2.UserId,
}
err := h.Save(context.TODO(), &pb.SaveRequest{
Locations: []*pb.Location{loc1, loc2, loc3},
}, &pb.SaveResponse{})
assert.NoError(t, err)
t.Run("NoMatches", func(t *testing.T) {
var rsp pb.ListResponse
err := h.Read(context.Background(), &pb.ReadRequest{
UserIds: []string{uuid.New().String()},
After: timestamppb.New(baseTime),
Before: timestamppb.New(baseTime.Add(time.Hour)),
}, &rsp)
assert.NoError(t, err)
assert.Empty(t, rsp.Locations)
})
t.Run("OneUserID", func(t *testing.T) {
var rsp pb.ListResponse
err := h.Read(context.Background(), &pb.ReadRequest{
UserIds: []string{loc2.UserId},
After: timestamppb.New(baseTime),
Before: timestamppb.New(baseTime.Add(time.Hour)),
}, &rsp)
assert.NoError(t, err)
if len(rsp.Locations) != 2 {
t.Fatalf("Two locations should be returned")
}
assert.Equal(t, loc2.UserId, rsp.Locations[0].UserId)
assert.Equal(t, loc2.Latitude.Value, rsp.Locations[0].Latitude.Value)
assert.Equal(t, loc2.Longitude.Value, rsp.Locations[0].Longitude.Value)
assert.Equal(t, loc2.Timestamp.AsTime(), rsp.Locations[0].Timestamp.AsTime())
assert.Equal(t, loc3.UserId, rsp.Locations[1].UserId)
assert.Equal(t, loc3.Latitude.Value, rsp.Locations[1].Latitude.Value)
assert.Equal(t, loc3.Longitude.Value, rsp.Locations[1].Longitude.Value)
assert.Equal(t, loc3.Timestamp.AsTime(), rsp.Locations[1].Timestamp.AsTime())
})
t.Run("OneUserIDReducedTime", func(t *testing.T) {
var rsp pb.ListResponse
err := h.Read(context.Background(), &pb.ReadRequest{
UserIds: []string{loc2.UserId},
After: timestamppb.New(baseTime),
Before: timestamppb.New(baseTime.Add(time.Minute * 30)),
}, &rsp)
assert.NoError(t, err)
if len(rsp.Locations) != 1 {
t.Fatalf("One location should be returned")
}
assert.Equal(t, loc2.UserId, rsp.Locations[0].UserId)
assert.Equal(t, loc2.Latitude.Value, rsp.Locations[0].Latitude.Value)
assert.Equal(t, loc2.Longitude.Value, rsp.Locations[0].Longitude.Value)
assert.Equal(t, loc2.Timestamp.AsTime(), rsp.Locations[0].Timestamp.AsTime())
})
t.Run("TwoUserIDs", func(t *testing.T) {
var rsp pb.ListResponse
err := h.Read(context.Background(), &pb.ReadRequest{
UserIds: []string{loc1.UserId, loc2.UserId},
After: timestamppb.New(baseTime),
Before: timestamppb.New(baseTime.Add(time.Minute * 30)),
}, &rsp)
assert.NoError(t, err)
if len(rsp.Locations) != 2 {
t.Fatalf("Two locations should be returned")
}
assert.Equal(t, loc1.UserId, rsp.Locations[0].UserId)
assert.Equal(t, loc1.Latitude.Value, rsp.Locations[0].Latitude.Value)
assert.Equal(t, loc1.Longitude.Value, rsp.Locations[0].Longitude.Value)
assert.Equal(t, loc1.Timestamp.AsTime(), rsp.Locations[0].Timestamp.AsTime())
assert.Equal(t, loc2.UserId, rsp.Locations[1].UserId)
assert.Equal(t, loc2.Latitude.Value, rsp.Locations[1].Latitude.Value)
assert.Equal(t, loc2.Longitude.Value, rsp.Locations[1].Longitude.Value)
assert.Equal(t, loc2.Timestamp.AsTime(), rsp.Locations[1].Timestamp.AsTime())
})
}

51
locations/main.go Normal file
View File

@@ -0,0 +1,51 @@
package main
import (
geo "github.com/hailocab/go-geoindex"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/micro/services/locations/handler"
"github.com/micro/services/locations/model"
pb "github.com/micro/services/locations/proto"
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/logger"
)
var dbAddress = "postgresql://postgres@localhost:5432/locations?sslmode=disable"
func main() {
// Create service
srv := service.New(
service.Name("locations"),
service.Version("latest"),
)
// Connect to the database
cfg, err := config.Get("locations.database")
if err != nil {
logger.Fatalf("Error loading config: %v", err)
}
addr := cfg.String(dbAddress)
db, err := gorm.Open(postgres.Open(addr), &gorm.Config{})
if err != nil {
logger.Fatalf("Error connecting to database: %v", err)
}
// Migrate the database
if err := db.AutoMigrate(&model.Location{}); err != nil {
logger.Fatalf("Error migrating database: %v", err)
}
// Register handler
pb.RegisterLocationsHandler(srv.Server(), &handler.Locations{
DB: db, Geoindex: geo.NewPointsIndex(geo.Km(0.1)),
})
// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}

1
locations/micro.mu Normal file
View File

@@ -0,0 +1 @@
service locations

View File

@@ -0,0 +1,26 @@
package model
import (
"time"
)
type Location struct {
ID string
UserID string `gorm:"index"`
Latitude float64
Longitude float64
Timestamp time.Time
}
// use the user id for the geoindex so only one result is returned per user
func (l *Location) Id() string {
return l.UserID
}
func (l *Location) Lat() float64 {
return l.Latitude
}
func (l *Location) Lon() float64 {
return l.Longitude
}

View File

@@ -0,0 +1,643 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.23.0
// protoc v3.13.0
// source: proto/locations.proto
package locations
import (
proto "github.com/golang/protobuf/proto"
timestamp "github.com/golang/protobuf/ptypes/timestamp"
wrappers "github.com/golang/protobuf/ptypes/wrappers"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// This is a compile-time assertion that a sufficiently up-to-date version
// of the legacy proto package is being used.
const _ = proto.ProtoPackageIsVersion4
type Location struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
UserId string `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
Timestamp *timestamp.Timestamp `protobuf:"bytes,2,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
Latitude *wrappers.DoubleValue `protobuf:"bytes,3,opt,name=latitude,proto3" json:"latitude,omitempty"`
Longitude *wrappers.DoubleValue `protobuf:"bytes,4,opt,name=longitude,proto3" json:"longitude,omitempty"`
}
func (x *Location) Reset() {
*x = Location{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_locations_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Location) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Location) ProtoMessage() {}
func (x *Location) ProtoReflect() protoreflect.Message {
mi := &file_proto_locations_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Location.ProtoReflect.Descriptor instead.
func (*Location) Descriptor() ([]byte, []int) {
return file_proto_locations_proto_rawDescGZIP(), []int{0}
}
func (x *Location) GetUserId() string {
if x != nil {
return x.UserId
}
return ""
}
func (x *Location) GetTimestamp() *timestamp.Timestamp {
if x != nil {
return x.Timestamp
}
return nil
}
func (x *Location) GetLatitude() *wrappers.DoubleValue {
if x != nil {
return x.Latitude
}
return nil
}
func (x *Location) GetLongitude() *wrappers.DoubleValue {
if x != nil {
return x.Longitude
}
return nil
}
type SaveRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Locations []*Location `protobuf:"bytes,1,rep,name=locations,proto3" json:"locations,omitempty"`
}
func (x *SaveRequest) Reset() {
*x = SaveRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_locations_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SaveRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SaveRequest) ProtoMessage() {}
func (x *SaveRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_locations_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SaveRequest.ProtoReflect.Descriptor instead.
func (*SaveRequest) Descriptor() ([]byte, []int) {
return file_proto_locations_proto_rawDescGZIP(), []int{1}
}
func (x *SaveRequest) GetLocations() []*Location {
if x != nil {
return x.Locations
}
return nil
}
type SaveResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
}
func (x *SaveResponse) Reset() {
*x = SaveResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_locations_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SaveResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SaveResponse) ProtoMessage() {}
func (x *SaveResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_locations_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SaveResponse.ProtoReflect.Descriptor instead.
func (*SaveResponse) Descriptor() ([]byte, []int) {
return file_proto_locations_proto_rawDescGZIP(), []int{2}
}
type LastRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
UserIds []string `protobuf:"bytes,1,rep,name=user_ids,json=userIds,proto3" json:"user_ids,omitempty"`
}
func (x *LastRequest) Reset() {
*x = LastRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_locations_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *LastRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*LastRequest) ProtoMessage() {}
func (x *LastRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_locations_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use LastRequest.ProtoReflect.Descriptor instead.
func (*LastRequest) Descriptor() ([]byte, []int) {
return file_proto_locations_proto_rawDescGZIP(), []int{3}
}
func (x *LastRequest) GetUserIds() []string {
if x != nil {
return x.UserIds
}
return nil
}
type ListResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Locations []*Location `protobuf:"bytes,1,rep,name=locations,proto3" json:"locations,omitempty"`
}
func (x *ListResponse) Reset() {
*x = ListResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_locations_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ListResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ListResponse) ProtoMessage() {}
func (x *ListResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_locations_proto_msgTypes[4]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ListResponse.ProtoReflect.Descriptor instead.
func (*ListResponse) Descriptor() ([]byte, []int) {
return file_proto_locations_proto_rawDescGZIP(), []int{4}
}
func (x *ListResponse) GetLocations() []*Location {
if x != nil {
return x.Locations
}
return nil
}
type NearRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Latitude *wrappers.DoubleValue `protobuf:"bytes,1,opt,name=latitude,proto3" json:"latitude,omitempty"`
Longitude *wrappers.DoubleValue `protobuf:"bytes,2,opt,name=longitude,proto3" json:"longitude,omitempty"`
// radius to search within, units km
Radius *wrappers.DoubleValue `protobuf:"bytes,3,opt,name=radius,proto3" json:"radius,omitempty"`
}
func (x *NearRequest) Reset() {
*x = NearRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_locations_proto_msgTypes[5]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *NearRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*NearRequest) ProtoMessage() {}
func (x *NearRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_locations_proto_msgTypes[5]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use NearRequest.ProtoReflect.Descriptor instead.
func (*NearRequest) Descriptor() ([]byte, []int) {
return file_proto_locations_proto_rawDescGZIP(), []int{5}
}
func (x *NearRequest) GetLatitude() *wrappers.DoubleValue {
if x != nil {
return x.Latitude
}
return nil
}
func (x *NearRequest) GetLongitude() *wrappers.DoubleValue {
if x != nil {
return x.Longitude
}
return nil
}
func (x *NearRequest) GetRadius() *wrappers.DoubleValue {
if x != nil {
return x.Radius
}
return nil
}
type ReadRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
UserIds []string `protobuf:"bytes,1,rep,name=user_ids,json=userIds,proto3" json:"user_ids,omitempty"`
After *timestamp.Timestamp `protobuf:"bytes,2,opt,name=after,proto3" json:"after,omitempty"`
Before *timestamp.Timestamp `protobuf:"bytes,3,opt,name=before,proto3" json:"before,omitempty"`
}
func (x *ReadRequest) Reset() {
*x = ReadRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_locations_proto_msgTypes[6]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ReadRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ReadRequest) ProtoMessage() {}
func (x *ReadRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_locations_proto_msgTypes[6]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ReadRequest.ProtoReflect.Descriptor instead.
func (*ReadRequest) Descriptor() ([]byte, []int) {
return file_proto_locations_proto_rawDescGZIP(), []int{6}
}
func (x *ReadRequest) GetUserIds() []string {
if x != nil {
return x.UserIds
}
return nil
}
func (x *ReadRequest) GetAfter() *timestamp.Timestamp {
if x != nil {
return x.After
}
return nil
}
func (x *ReadRequest) GetBefore() *timestamp.Timestamp {
if x != nil {
return x.Before
}
return nil
}
var File_proto_locations_proto protoreflect.FileDescriptor
var file_proto_locations_proto_rawDesc = []byte{
0x0a, 0x15, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e,
0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f,
0x6e, 0x73, 0x1a, 0x1f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x62, 0x75, 0x66, 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x1a, 0x1e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x62, 0x75, 0x66, 0x2f, 0x77, 0x72, 0x61, 0x70, 0x70, 0x65, 0x72, 0x73, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x22, 0xd3, 0x01, 0x0a, 0x08, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e,
0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x38, 0x0a, 0x09, 0x74, 0x69, 0x6d,
0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67,
0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54,
0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74,
0x61, 0x6d, 0x70, 0x12, 0x38, 0x0a, 0x08, 0x6c, 0x61, 0x74, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18,
0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x44, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x56, 0x61,
0x6c, 0x75, 0x65, 0x52, 0x08, 0x6c, 0x61, 0x74, 0x69, 0x74, 0x75, 0x64, 0x65, 0x12, 0x3a, 0x0a,
0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b,
0x32, 0x1c, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62,
0x75, 0x66, 0x2e, 0x44, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x09,
0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x22, 0x40, 0x0a, 0x0b, 0x53, 0x61, 0x76,
0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x31, 0x0a, 0x09, 0x6c, 0x6f, 0x63, 0x61,
0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x6c, 0x6f,
0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e,
0x52, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x0e, 0x0a, 0x0c, 0x53,
0x61, 0x76, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x28, 0x0a, 0x0b, 0x4c,
0x61, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x19, 0x0a, 0x08, 0x75, 0x73,
0x65, 0x72, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x75, 0x73,
0x65, 0x72, 0x49, 0x64, 0x73, 0x22, 0x41, 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73,
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x31, 0x0a, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f,
0x6e, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x09, 0x6c,
0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0xb9, 0x01, 0x0a, 0x0b, 0x4e, 0x65, 0x61,
0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x38, 0x0a, 0x08, 0x6c, 0x61, 0x74, 0x69,
0x74, 0x75, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x67, 0x6f, 0x6f,
0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x44, 0x6f, 0x75,
0x62, 0x6c, 0x65, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x08, 0x6c, 0x61, 0x74, 0x69, 0x74, 0x75,
0x64, 0x65, 0x12, 0x3a, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18,
0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x44, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x56, 0x61,
0x6c, 0x75, 0x65, 0x52, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x12, 0x34,
0x0a, 0x06, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c,
0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66,
0x2e, 0x44, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x06, 0x72, 0x61,
0x64, 0x69, 0x75, 0x73, 0x22, 0x8e, 0x01, 0x0a, 0x0b, 0x52, 0x65, 0x61, 0x64, 0x52, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x12, 0x19, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x73,
0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x73, 0x12,
0x30, 0x0a, 0x05, 0x61, 0x66, 0x74, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a,
0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66,
0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x05, 0x61, 0x66, 0x74, 0x65,
0x72, 0x12, 0x32, 0x0a, 0x06, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28,
0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x06, 0x62,
0x65, 0x66, 0x6f, 0x72, 0x65, 0x32, 0xf7, 0x01, 0x0a, 0x09, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69,
0x6f, 0x6e, 0x73, 0x12, 0x39, 0x0a, 0x04, 0x53, 0x61, 0x76, 0x65, 0x12, 0x16, 0x2e, 0x6c, 0x6f,
0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x71, 0x75,
0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x39,
0x0a, 0x04, 0x4c, 0x61, 0x73, 0x74, 0x12, 0x16, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f,
0x6e, 0x73, 0x2e, 0x4c, 0x61, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17,
0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52,
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x39, 0x0a, 0x04, 0x4e, 0x65, 0x61,
0x72, 0x12, 0x16, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x4e, 0x65,
0x61, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x6c, 0x6f, 0x63, 0x61,
0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
0x73, 0x65, 0x22, 0x00, 0x12, 0x39, 0x0a, 0x04, 0x52, 0x65, 0x61, 0x64, 0x12, 0x16, 0x2e, 0x6c,
0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x52, 0x65, 0x61, 0x64, 0x52, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73,
0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42,
0x35, 0x5a, 0x33, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6d, 0x69,
0x63, 0x72, 0x6f, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2f, 0x6c, 0x6f, 0x63,
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x6c, 0x6f, 0x63,
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_proto_locations_proto_rawDescOnce sync.Once
file_proto_locations_proto_rawDescData = file_proto_locations_proto_rawDesc
)
func file_proto_locations_proto_rawDescGZIP() []byte {
file_proto_locations_proto_rawDescOnce.Do(func() {
file_proto_locations_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_locations_proto_rawDescData)
})
return file_proto_locations_proto_rawDescData
}
var file_proto_locations_proto_msgTypes = make([]protoimpl.MessageInfo, 7)
var file_proto_locations_proto_goTypes = []interface{}{
(*Location)(nil), // 0: locations.Location
(*SaveRequest)(nil), // 1: locations.SaveRequest
(*SaveResponse)(nil), // 2: locations.SaveResponse
(*LastRequest)(nil), // 3: locations.LastRequest
(*ListResponse)(nil), // 4: locations.ListResponse
(*NearRequest)(nil), // 5: locations.NearRequest
(*ReadRequest)(nil), // 6: locations.ReadRequest
(*timestamp.Timestamp)(nil), // 7: google.protobuf.Timestamp
(*wrappers.DoubleValue)(nil), // 8: google.protobuf.DoubleValue
}
var file_proto_locations_proto_depIdxs = []int32{
7, // 0: locations.Location.timestamp:type_name -> google.protobuf.Timestamp
8, // 1: locations.Location.latitude:type_name -> google.protobuf.DoubleValue
8, // 2: locations.Location.longitude:type_name -> google.protobuf.DoubleValue
0, // 3: locations.SaveRequest.locations:type_name -> locations.Location
0, // 4: locations.ListResponse.locations:type_name -> locations.Location
8, // 5: locations.NearRequest.latitude:type_name -> google.protobuf.DoubleValue
8, // 6: locations.NearRequest.longitude:type_name -> google.protobuf.DoubleValue
8, // 7: locations.NearRequest.radius:type_name -> google.protobuf.DoubleValue
7, // 8: locations.ReadRequest.after:type_name -> google.protobuf.Timestamp
7, // 9: locations.ReadRequest.before:type_name -> google.protobuf.Timestamp
1, // 10: locations.Locations.Save:input_type -> locations.SaveRequest
3, // 11: locations.Locations.Last:input_type -> locations.LastRequest
5, // 12: locations.Locations.Near:input_type -> locations.NearRequest
6, // 13: locations.Locations.Read:input_type -> locations.ReadRequest
2, // 14: locations.Locations.Save:output_type -> locations.SaveResponse
4, // 15: locations.Locations.Last:output_type -> locations.ListResponse
4, // 16: locations.Locations.Near:output_type -> locations.ListResponse
4, // 17: locations.Locations.Read:output_type -> locations.ListResponse
14, // [14:18] is the sub-list for method output_type
10, // [10:14] is the sub-list for method input_type
10, // [10:10] is the sub-list for extension type_name
10, // [10:10] is the sub-list for extension extendee
0, // [0:10] is the sub-list for field type_name
}
func init() { file_proto_locations_proto_init() }
func file_proto_locations_proto_init() {
if File_proto_locations_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_proto_locations_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Location); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_locations_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SaveRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_locations_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SaveResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_locations_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*LastRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_locations_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ListResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_locations_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*NearRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_locations_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ReadRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_locations_proto_rawDesc,
NumEnums: 0,
NumMessages: 7,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_proto_locations_proto_goTypes,
DependencyIndexes: file_proto_locations_proto_depIdxs,
MessageInfos: file_proto_locations_proto_msgTypes,
}.Build()
File_proto_locations_proto = out.File
file_proto_locations_proto_rawDesc = nil
file_proto_locations_proto_goTypes = nil
file_proto_locations_proto_depIdxs = nil
}

View File

@@ -0,0 +1,154 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: proto/locations.proto
package locations
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
_ "github.com/golang/protobuf/ptypes/timestamp"
_ "github.com/golang/protobuf/ptypes/wrappers"
math "math"
)
import (
context "context"
api "github.com/micro/micro/v3/service/api"
client "github.com/micro/micro/v3/service/client"
server "github.com/micro/micro/v3/service/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for Locations service
func NewLocationsEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for Locations service
type LocationsService interface {
// Save a set of locations
Save(ctx context.Context, in *SaveRequest, opts ...client.CallOption) (*SaveResponse, error)
// Last locations for a set of users
Last(ctx context.Context, in *LastRequest, opts ...client.CallOption) (*ListResponse, error)
// Near returns the locations near a point at a given time
Near(ctx context.Context, in *NearRequest, opts ...client.CallOption) (*ListResponse, error)
// Read locations for a group of users between two points in time
Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ListResponse, error)
}
type locationsService struct {
c client.Client
name string
}
func NewLocationsService(name string, c client.Client) LocationsService {
return &locationsService{
c: c,
name: name,
}
}
func (c *locationsService) Save(ctx context.Context, in *SaveRequest, opts ...client.CallOption) (*SaveResponse, error) {
req := c.c.NewRequest(c.name, "Locations.Save", in)
out := new(SaveResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *locationsService) Last(ctx context.Context, in *LastRequest, opts ...client.CallOption) (*ListResponse, error) {
req := c.c.NewRequest(c.name, "Locations.Last", in)
out := new(ListResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *locationsService) Near(ctx context.Context, in *NearRequest, opts ...client.CallOption) (*ListResponse, error) {
req := c.c.NewRequest(c.name, "Locations.Near", in)
out := new(ListResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *locationsService) Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ListResponse, error) {
req := c.c.NewRequest(c.name, "Locations.Read", in)
out := new(ListResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Locations service
type LocationsHandler interface {
// Save a set of locations
Save(context.Context, *SaveRequest, *SaveResponse) error
// Last locations for a set of users
Last(context.Context, *LastRequest, *ListResponse) error
// Near returns the locations near a point at a given time
Near(context.Context, *NearRequest, *ListResponse) error
// Read locations for a group of users between two points in time
Read(context.Context, *ReadRequest, *ListResponse) error
}
func RegisterLocationsHandler(s server.Server, hdlr LocationsHandler, opts ...server.HandlerOption) error {
type locations interface {
Save(ctx context.Context, in *SaveRequest, out *SaveResponse) error
Last(ctx context.Context, in *LastRequest, out *ListResponse) error
Near(ctx context.Context, in *NearRequest, out *ListResponse) error
Read(ctx context.Context, in *ReadRequest, out *ListResponse) error
}
type Locations struct {
locations
}
h := &locationsHandler{hdlr}
return s.Handle(s.NewHandler(&Locations{h}, opts...))
}
type locationsHandler struct {
LocationsHandler
}
func (h *locationsHandler) Save(ctx context.Context, in *SaveRequest, out *SaveResponse) error {
return h.LocationsHandler.Save(ctx, in, out)
}
func (h *locationsHandler) Last(ctx context.Context, in *LastRequest, out *ListResponse) error {
return h.LocationsHandler.Last(ctx, in, out)
}
func (h *locationsHandler) Near(ctx context.Context, in *NearRequest, out *ListResponse) error {
return h.LocationsHandler.Near(ctx, in, out)
}
func (h *locationsHandler) Read(ctx context.Context, in *ReadRequest, out *ListResponse) error {
return h.LocationsHandler.Read(ctx, in, out)
}

View File

@@ -0,0 +1,52 @@
syntax = "proto3";
package locations;
option go_package = "github.com/micro/services/locations/proto;locations";
import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";
service Locations {
// Save a set of locations
rpc Save(SaveRequest) returns (SaveResponse) {}
// Last locations for a set of users
rpc Last(LastRequest) returns (ListResponse) {}
// Near returns the locations near a point at a given time
rpc Near(NearRequest) returns (ListResponse) {}
// Read locations for a group of users between two points in time
rpc Read(ReadRequest) returns (ListResponse) {}
}
message Location {
string user_id = 1;
google.protobuf.Timestamp timestamp = 2;
google.protobuf.DoubleValue latitude = 3;
google.protobuf.DoubleValue longitude = 4;
}
message SaveRequest {
repeated Location locations = 1;
}
message SaveResponse {}
message LastRequest {
repeated string user_ids = 1;
}
message ListResponse {
repeated Location locations = 1;
}
message NearRequest {
google.protobuf.DoubleValue latitude = 1;
google.protobuf.DoubleValue longitude = 2;
// radius to search within, units km
google.protobuf.DoubleValue radius = 3;
}
message ReadRequest {
repeated string user_ids = 1;
google.protobuf.Timestamp after = 2;
google.protobuf.Timestamp before = 3;
}