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

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

View File

@@ -1,9 +1,11 @@
package handler_test
import (
"os"
"testing"
"time"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/micro/services/threads/handler"
pb "github.com/micro/services/threads/proto"
"github.com/stretchr/testify/assert"
@@ -14,11 +16,20 @@ import (
func testHandler(t *testing.T) *handler.Threads {
// connect to the database
db, err := gorm.Open(postgres.Open("postgresql://postgres@localhost:5432/threads?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 conversations, messages CASCADE").Error; err != nil {
t.Fatalf("Error cleaning database: %v", err)
}
// migrate the database
if err := db.AutoMigrate(&handler.Conversation{}, &handler.Message{}); err != nil {
t.Fatalf("Error migrating database: %v", err)
@@ -54,7 +65,7 @@ func assertConversationsMatch(t *testing.T, exp, act *pb.Conversation) {
return
}
assert.True(t, exp.CreatedAt.AsTime().Equal(act.CreatedAt.AsTime()))
assert.True(t, microSecondTime(exp.CreatedAt).Equal(microSecondTime(act.CreatedAt)))
}
func assertMessagesMatch(t *testing.T, exp, act *pb.Message) {
@@ -80,5 +91,11 @@ func assertMessagesMatch(t *testing.T, exp, act *pb.Message) {
return
}
assert.True(t, exp.SentAt.AsTime().Equal(act.SentAt.AsTime()))
assert.True(t, microSecondTime(exp.SentAt).Equal(microSecondTime(act.SentAt)))
}
// 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))
}

View File

@@ -1,17 +1,17 @@
// 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/threads.proto
package threads
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"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"
reflect "reflect"
sync "sync"
)
@@ -32,10 +32,10 @@ type Conversation struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
GroupId string `protobuf:"bytes,2,opt,name=group_id,json=groupId,proto3" json:"group_id,omitempty"`
Topic string `protobuf:"bytes,3,opt,name=topic,proto3" json:"topic,omitempty"`
CreatedAt *timestamp.Timestamp `protobuf:"bytes,4,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"`
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
GroupId string `protobuf:"bytes,2,opt,name=group_id,json=groupId,proto3" json:"group_id,omitempty"`
Topic string `protobuf:"bytes,3,opt,name=topic,proto3" json:"topic,omitempty"`
CreatedAt *timestamppb.Timestamp `protobuf:"bytes,4,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"`
}
func (x *Conversation) Reset() {
@@ -91,7 +91,7 @@ func (x *Conversation) GetTopic() string {
return ""
}
func (x *Conversation) GetCreatedAt() *timestamp.Timestamp {
func (x *Conversation) GetCreatedAt() *timestamppb.Timestamp {
if x != nil {
return x.CreatedAt
}
@@ -103,11 +103,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"`
ConversationId string `protobuf:"bytes,3,opt,name=conversation_id,json=conversationId,proto3" json:"conversation_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"`
ConversationId string `protobuf:"bytes,3,opt,name=conversation_id,json=conversationId,proto3" json:"conversation_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() {
@@ -170,7 +170,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
}
@@ -284,8 +284,8 @@ type ReadConversationRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
GroupId *wrappers.StringValue `protobuf:"bytes,2,opt,name=group_id,json=groupId,proto3" json:"group_id,omitempty"`
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
GroupId *wrapperspb.StringValue `protobuf:"bytes,2,opt,name=group_id,json=groupId,proto3" json:"group_id,omitempty"`
}
func (x *ReadConversationRequest) Reset() {
@@ -327,7 +327,7 @@ func (x *ReadConversationRequest) GetId() string {
return ""
}
func (x *ReadConversationRequest) GetGroupId() *wrappers.StringValue {
func (x *ReadConversationRequest) GetGroupId() *wrapperspb.StringValue {
if x != nil {
return x.GroupId
}
@@ -785,9 +785,9 @@ type ListMessagesRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ConversationId string `protobuf:"bytes,1,opt,name=conversation_id,json=conversationId,proto3" json:"conversation_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"`
ConversationId string `protobuf:"bytes,1,opt,name=conversation_id,json=conversationId,proto3" json:"conversation_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() {
@@ -829,14 +829,14 @@ func (x *ListMessagesRequest) GetConversationId() 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
}
@@ -895,8 +895,8 @@ type RecentMessagesRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ConversationIds []string `protobuf:"bytes,1,rep,name=conversation_ids,json=conversationIds,proto3" json:"conversation_ids,omitempty"`
LimitPerConversation *wrappers.Int32Value `protobuf:"bytes,2,opt,name=limit_per_conversation,json=limitPerConversation,proto3" json:"limit_per_conversation,omitempty"`
ConversationIds []string `protobuf:"bytes,1,rep,name=conversation_ids,json=conversationIds,proto3" json:"conversation_ids,omitempty"`
LimitPerConversation *wrapperspb.Int32Value `protobuf:"bytes,2,opt,name=limit_per_conversation,json=limitPerConversation,proto3" json:"limit_per_conversation,omitempty"`
}
func (x *RecentMessagesRequest) Reset() {
@@ -938,7 +938,7 @@ func (x *RecentMessagesRequest) GetConversationIds() []string {
return nil
}
func (x *RecentMessagesRequest) GetLimitPerConversation() *wrappers.Int32Value {
func (x *RecentMessagesRequest) GetLimitPerConversation() *wrapperspb.Int32Value {
if x != nil {
return x.LimitPerConversation
}
@@ -1189,9 +1189,9 @@ var file_proto_threads_proto_goTypes = []interface{}{
(*ListMessagesResponse)(nil), // 15: threads.ListMessagesResponse
(*RecentMessagesRequest)(nil), // 16: threads.RecentMessagesRequest
(*RecentMessagesResponse)(nil), // 17: threads.RecentMessagesResponse
(*timestamp.Timestamp)(nil), // 18: google.protobuf.Timestamp
(*wrappers.StringValue)(nil), // 19: google.protobuf.StringValue
(*wrappers.Int32Value)(nil), // 20: google.protobuf.Int32Value
(*timestamppb.Timestamp)(nil), // 18: google.protobuf.Timestamp
(*wrapperspb.StringValue)(nil), // 19: google.protobuf.StringValue
(*wrapperspb.Int32Value)(nil), // 20: google.protobuf.Int32Value
}
var file_proto_threads_proto_depIdxs = []int32{
18, // 0: threads.Conversation.created_at:type_name -> google.protobuf.Timestamp

View File

@@ -6,8 +6,8 @@ package threads
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"
)

View File

@@ -1,7 +1,7 @@
syntax = "proto3";
package threads;
option go_package = "proto;threads";
option go_package = "./proto;threads";
import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";