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

@@ -11,7 +11,7 @@ proto:
docs:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=. proto/chats.proto
@redoc-cli bundle api-chats.json
@redoc-cli bundle api-protobuf.json
.PHONY: build
build:

View File

@@ -1,6 +1,7 @@
package handler_test
import (
"os"
"testing"
"time"
@@ -8,27 +9,32 @@ import (
pb "github.com/micro/services/chats/proto"
"github.com/stretchr/testify/assert"
"github.com/golang/protobuf/ptypes/timestamp"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func testHandler(t *testing.T) *handler.Chats {
// connect to the database
db, err := gorm.Open(postgres.Open("postgresql://postgres@localhost:5432/chats?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)
}
// clean any data from a previous run
if err := db.Exec("DROP TABLE IF EXISTS chats, messages CASCADE").Error; err != nil {
t.Fatalf("Error cleaning database: %v", err)
}
// migrate the database
if err := db.AutoMigrate(&handler.Chat{}, &handler.Message{}); err != nil {
t.Fatalf("Error migrating database: %v", err)
}
// clean any data from a previous run
if err := db.Exec("TRUNCATE TABLE chats, messages CASCADE").Error; err != nil {
t.Fatalf("Error cleaning database: %v", err)
}
return &handler.Chats{DB: db, Time: func() time.Time { return time.Unix(1611327673, 0) }}
}
@@ -53,7 +59,13 @@ func assertChatsMatch(t *testing.T, exp, act *pb.Chat) {
return
}
assert.True(t, exp.CreatedAt.AsTime().Equal(act.CreatedAt.AsTime()))
assert.True(t, microSecondTime(exp.CreatedAt).Equal(microSecondTime(act.CreatedAt)))
}
// postgres has a resolution of 100microseconds so just test that it's accurate to the second
func microSecondTime(t *timestamp.Timestamp) time.Time {
tt:=t.AsTime()
return time.Unix(tt.Unix(), int64( tt.Nanosecond() - tt.Nanosecond() % 1000))
}
func assertMessagesMatch(t *testing.T, exp, act *pb.Message) {
@@ -77,6 +89,5 @@ func assertMessagesMatch(t *testing.T, exp, act *pb.Message) {
t.Errorf("SentAt not set")
return
}
assert.True(t, exp.SentAt.AsTime().Equal(act.SentAt.AsTime()))
assert.True(t, microSecondTime(exp.SentAt).Equal(microSecondTime(act.SentAt)))
}

View File

@@ -1,16 +1,17 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.11.4
// protoc-gen-go v1.25.0
// protoc v3.15.5
// source: proto/chats.proto
package chats
import (
timestamp "github.com/golang/protobuf/ptypes/timestamp"
wrappers "github.com/golang/protobuf/ptypes/wrappers"
proto "github.com/golang/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"
reflect "reflect"
sync "sync"
)
@@ -22,14 +23,18 @@ const (
_ = 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 Chat struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
UserIds []string `protobuf:"bytes,2,rep,name=user_ids,json=userIds,proto3" json:"user_ids,omitempty"`
CreatedAt *timestamp.Timestamp `protobuf:"bytes,3,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"`
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
UserIds []string `protobuf:"bytes,2,rep,name=user_ids,json=userIds,proto3" json:"user_ids,omitempty"`
CreatedAt *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"`
}
func (x *Chat) Reset() {
@@ -78,7 +83,7 @@ func (x *Chat) GetUserIds() []string {
return nil
}
func (x *Chat) GetCreatedAt() *timestamp.Timestamp {
func (x *Chat) GetCreatedAt() *timestamppb.Timestamp {
if x != nil {
return x.CreatedAt
}
@@ -90,11 +95,11 @@ type Message struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
AuthorId string `protobuf:"bytes,2,opt,name=author_id,json=authorId,proto3" json:"author_id,omitempty"`
ChatId string `protobuf:"bytes,3,opt,name=chat_id,json=chatId,proto3" json:"chat_id,omitempty"`
Text string `protobuf:"bytes,4,opt,name=text,proto3" json:"text,omitempty"`
SentAt *timestamp.Timestamp `protobuf:"bytes,5,opt,name=sent_at,json=sentAt,proto3" json:"sent_at,omitempty"`
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
AuthorId string `protobuf:"bytes,2,opt,name=author_id,json=authorId,proto3" json:"author_id,omitempty"`
ChatId string `protobuf:"bytes,3,opt,name=chat_id,json=chatId,proto3" json:"chat_id,omitempty"`
Text string `protobuf:"bytes,4,opt,name=text,proto3" json:"text,omitempty"`
SentAt *timestamppb.Timestamp `protobuf:"bytes,5,opt,name=sent_at,json=sentAt,proto3" json:"sent_at,omitempty"`
}
func (x *Message) Reset() {
@@ -157,7 +162,7 @@ func (x *Message) GetText() string {
return ""
}
func (x *Message) GetSentAt() *timestamp.Timestamp {
func (x *Message) GetSentAt() *timestamppb.Timestamp {
if x != nil {
return x.SentAt
}
@@ -381,9 +386,9 @@ type ListMessagesRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ChatId string `protobuf:"bytes,1,opt,name=chat_id,json=chatId,proto3" json:"chat_id,omitempty"`
SentBefore *timestamp.Timestamp `protobuf:"bytes,2,opt,name=sent_before,json=sentBefore,proto3" json:"sent_before,omitempty"`
Limit *wrappers.Int32Value `protobuf:"bytes,3,opt,name=limit,proto3" json:"limit,omitempty"`
ChatId string `protobuf:"bytes,1,opt,name=chat_id,json=chatId,proto3" json:"chat_id,omitempty"`
SentBefore *timestamppb.Timestamp `protobuf:"bytes,2,opt,name=sent_before,json=sentBefore,proto3" json:"sent_before,omitempty"`
Limit *wrapperspb.Int32Value `protobuf:"bytes,3,opt,name=limit,proto3" json:"limit,omitempty"`
}
func (x *ListMessagesRequest) Reset() {
@@ -425,14 +430,14 @@ func (x *ListMessagesRequest) GetChatId() string {
return ""
}
func (x *ListMessagesRequest) GetSentBefore() *timestamp.Timestamp {
func (x *ListMessagesRequest) GetSentBefore() *timestamppb.Timestamp {
if x != nil {
return x.SentBefore
}
return nil
}
func (x *ListMessagesRequest) GetLimit() *wrappers.Int32Value {
func (x *ListMessagesRequest) GetLimit() *wrapperspb.Int32Value {
if x != nil {
return x.Limit
}
@@ -583,8 +588,8 @@ var file_proto_chats_proto_goTypes = []interface{}{
(*CreateMessageResponse)(nil), // 5: chats.CreateMessageResponse
(*ListMessagesRequest)(nil), // 6: chats.ListMessagesRequest
(*ListMessagesResponse)(nil), // 7: chats.ListMessagesResponse
(*timestamp.Timestamp)(nil), // 8: google.protobuf.Timestamp
(*wrappers.Int32Value)(nil), // 9: google.protobuf.Int32Value
(*timestamppb.Timestamp)(nil), // 8: google.protobuf.Timestamp
(*wrapperspb.Int32Value)(nil), // 9: google.protobuf.Int32Value
}
var file_proto_chats_proto_depIdxs = []int32{
8, // 0: chats.Chat.created_at:type_name -> google.protobuf.Timestamp

View File

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