Multitenant streams api (#72)

This commit is contained in:
Dominic Wong
2021-03-18 17:21:41 +00:00
committed by GitHub
parent 28ad626d91
commit 8dfe49f813
87 changed files with 1890 additions and 1064 deletions

View File

@@ -13,6 +13,11 @@ proto:
build:
go build -o seen *.go
docs:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/seen.proto
@redoc-cli bundle api-protobuf.json
.PHONY: test
test:
go test -v ./... -cover

View File

@@ -2,6 +2,7 @@ package handler_test
import (
"context"
"os"
"testing"
"time"
@@ -16,7 +17,11 @@ import (
func testHandler(t *testing.T) *handler.Seen {
// connect to the database
db, err := gorm.Open(postgres.Open("postgresql://postgres@localhost:5433/postgres?sslmode=disable"), &gorm.Config{})
addr := os.Getenv("POSTGRES_URL")
if len(addr) == 0 {
addr = "postgresql://postgres@localhost:5432/postgres?sslmode=disable"
}
db, err := gorm.Open(postgres.Open(addr), &gorm.Config{})
if err != nil {
t.Fatalf("Error connecting to database: %v", err)
}
@@ -222,13 +227,13 @@ func TestRead(t *testing.T) {
assert.Len(t, rsp.Timestamps, 2)
if v := rsp.Timestamps["message-1"]; v != nil {
assert.True(t, v.AsTime().Equal(tn))
assert.Equal(t, microSecondTime(v.AsTime()), microSecondTime(tn))
} else {
t.Errorf("Expected a timestamp for message-1")
}
if v := rsp.Timestamps["message-2"]; v != nil {
assert.True(t, v.AsTime().Equal(tn.Add(time.Minute*-10)))
assert.Equal(t, microSecondTime(v.AsTime()), microSecondTime(tn.Add(time.Minute*-10).UTC()))
} else {
t.Errorf("Expected a timestamp for message-2")
}
@@ -249,4 +254,10 @@ func TestRead(t *testing.T) {
}, &rsp)
assert.NoError(t, err)
assert.Len(t, rsp.Timestamps, 1)
}
// postgres has a resolution of 100microseconds so just test that it's accurate to the second
func microSecondTime(tt time.Time) time.Time {
return time.Unix(tt.Unix(), int64(tt.Nanosecond()-tt.Nanosecond()%1000)).UTC()
}

View File

@@ -1,16 +1,16 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.23.0
// protoc v3.13.0
// protoc-gen-go v1.25.0
// protoc v3.15.5
// source: proto/seen.proto
package seen
import (
proto "github.com/golang/protobuf/proto"
timestamp "github.com/golang/protobuf/ptypes/timestamp"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
reflect "reflect"
sync "sync"
)
@@ -86,10 +86,10 @@ type SetRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
UserId string `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
ResourceType string `protobuf:"bytes,2,opt,name=resource_type,json=resourceType,proto3" json:"resource_type,omitempty"`
ResourceId string `protobuf:"bytes,3,opt,name=resource_id,json=resourceId,proto3" json:"resource_id,omitempty"`
Timestamp *timestamp.Timestamp `protobuf:"bytes,4,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
UserId string `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
ResourceType string `protobuf:"bytes,2,opt,name=resource_type,json=resourceType,proto3" json:"resource_type,omitempty"`
ResourceId string `protobuf:"bytes,3,opt,name=resource_id,json=resourceId,proto3" json:"resource_id,omitempty"`
Timestamp *timestamppb.Timestamp `protobuf:"bytes,4,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
}
func (x *SetRequest) Reset() {
@@ -145,7 +145,7 @@ func (x *SetRequest) GetResourceId() string {
return ""
}
func (x *SetRequest) GetTimestamp() *timestamp.Timestamp {
func (x *SetRequest) GetTimestamp() *timestamppb.Timestamp {
if x != nil {
return x.Timestamp
}
@@ -359,7 +359,7 @@ type ReadResponse struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Timestamps map[string]*timestamp.Timestamp `protobuf:"bytes,1,rep,name=timestamps,proto3" json:"timestamps,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
Timestamps map[string]*timestamppb.Timestamp `protobuf:"bytes,1,rep,name=timestamps,proto3" json:"timestamps,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
}
func (x *ReadResponse) Reset() {
@@ -394,7 +394,7 @@ func (*ReadResponse) Descriptor() ([]byte, []int) {
return file_proto_seen_proto_rawDescGZIP(), []int{6}
}
func (x *ReadResponse) GetTimestamps() map[string]*timestamp.Timestamp {
func (x *ReadResponse) GetTimestamps() map[string]*timestamppb.Timestamp {
if x != nil {
return x.Timestamps
}
@@ -475,15 +475,15 @@ func file_proto_seen_proto_rawDescGZIP() []byte {
var file_proto_seen_proto_msgTypes = make([]protoimpl.MessageInfo, 8)
var file_proto_seen_proto_goTypes = []interface{}{
(*Resource)(nil), // 0: seen.Resource
(*SetRequest)(nil), // 1: seen.SetRequest
(*SetResponse)(nil), // 2: seen.SetResponse
(*UnsetRequest)(nil), // 3: seen.UnsetRequest
(*UnsetResponse)(nil), // 4: seen.UnsetResponse
(*ReadRequest)(nil), // 5: seen.ReadRequest
(*ReadResponse)(nil), // 6: seen.ReadResponse
nil, // 7: seen.ReadResponse.TimestampsEntry
(*timestamp.Timestamp)(nil), // 8: google.protobuf.Timestamp
(*Resource)(nil), // 0: seen.Resource
(*SetRequest)(nil), // 1: seen.SetRequest
(*SetResponse)(nil), // 2: seen.SetResponse
(*UnsetRequest)(nil), // 3: seen.UnsetRequest
(*UnsetResponse)(nil), // 4: seen.UnsetResponse
(*ReadRequest)(nil), // 5: seen.ReadRequest
(*ReadResponse)(nil), // 6: seen.ReadResponse
nil, // 7: seen.ReadResponse.TimestampsEntry
(*timestamppb.Timestamp)(nil), // 8: google.protobuf.Timestamp
}
var file_proto_seen_proto_depIdxs = []int32{
8, // 0: seen.SetRequest.timestamp:type_name -> google.protobuf.Timestamp

View File

@@ -6,7 +6,7 @@ package seen
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
_ "github.com/golang/protobuf/ptypes/timestamp"
_ "google.golang.org/protobuf/types/known/timestamppb"
math "math"
)

View File

@@ -1,7 +1,7 @@
syntax = "proto3";
package seen;
option go_package = "proto;seen";
option go_package = "./proto;seen";
import "google/protobuf/timestamp.proto";
// Seen is a service to keep track of which resources a user has seen (read). For example, it can
@@ -49,4 +49,4 @@ message ReadRequest {
message ReadResponse {
map<string, google.protobuf.Timestamp> timestamps = 1;
}
}