Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): support multiple tenants #1068

Merged
merged 5 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func StartServerAndRepos(t *testing.T, cfg *app.Config, useMongo bool, seeder Se
if useMongo {
db := mongotest.Connect(t)(t)
log.Infof("test: new db created with name: %v", db.Name())
accountRepos = lo.Must(accountmongo.New(ctx, db.Client(), db.Name(), false, false))
accountRepos = lo.Must(accountmongo.New(ctx, db.Client(), db.Name(), false, false, nil))
repos = lo.Must(mongo.NewWithDB(ctx, db, false, accountRepos))
} else {
repos = memory.New()
Expand Down Expand Up @@ -120,7 +120,7 @@ func StartGQLServerAndRepos(t *testing.T, cfg *app.Config, useMongo bool, seeder
if useMongo {
db := mongotest.Connect(t)(t)
log.Infof("test: new db created with name: %v", db.Name())
accountRepos = lo.Must(accountmongo.New(ctx, db.Client(), db.Name(), false, false))
accountRepos = lo.Must(accountmongo.New(ctx, db.Client(), db.Name(), false, false, nil))
repos = lo.Must(mongo.New(ctx, db.Client(), db.Name(), false, accountRepos))
} else {
repos = memory.New()
Expand Down
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/labstack/echo/v4 v4.11.4
github.com/oapi-codegen/runtime v1.1.1
github.com/ravilushqa/otelgqlgen v0.15.0
github.com/reearth/reearthx v0.0.0-20240201064205-4a2be423e40d
github.com/reearth/reearthx v0.0.0-20240206154804-13a660b86aad
github.com/robbiet480/go.sns v0.0.0-20230523235941-e8d832c79d68
github.com/samber/lo v1.39.0
github.com/sendgrid/sendgrid-go v3.14.0+incompatible
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ github.com/ravilushqa/otelgqlgen v0.15.0 h1:U85nrlweMXTGaMChUViYM39/MXBZVeVVlpuH
github.com/ravilushqa/otelgqlgen v0.15.0/go.mod h1:o+1Eju0VySmgq2BP8Vupz2YrN21Bj7D7imBqu3m2uB8=
github.com/reearth/reearthx v0.0.0-20240201064205-4a2be423e40d h1:OWU1s9hnxc7pfQ55kbc7M6SndYIM/hWxkeytDq8L/Jc=
github.com/reearth/reearthx v0.0.0-20240201064205-4a2be423e40d/go.mod h1:8DSD6e+h1KhfhO4TceYDDPpLkhZJH2CLF6nHxyV8hts=
github.com/reearth/reearthx v0.0.0-20240206154804-13a660b86aad h1:NsO9H6wNLxkKjh2Xz14u13hPz5tSOoFS+3M6PvPdxVo=
github.com/reearth/reearthx v0.0.0-20240206154804-13a660b86aad/go.mod h1:8DSD6e+h1KhfhO4TceYDDPpLkhZJH2CLF6nHxyV8hts=
github.com/robbiet480/go.sns v0.0.0-20230523235941-e8d832c79d68 h1:Jknsfy5cqCH6qAuoU1qNZ51hfBJfMSJYwsH9j9mdVnw=
github.com/robbiet480/go.sns v0.0.0-20230523235941-e8d832c79d68/go.mod h1:9CDhL7uDVy8vEVDNPJzxq89dPaPBWP6hxQcC8woBHus=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
Expand Down
12 changes: 12 additions & 0 deletions server/internal/adapter/gql/gqlmodel/convert_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
"github.com/reearth/reearthx/util"
)

func SimpleToUser(u *user.Simple) *User {
if u == nil {
return nil
}

Check warning on line 13 in server/internal/adapter/gql/gqlmodel/convert_user.go

View check run for this annotation

Codecov / codecov/patch

server/internal/adapter/gql/gqlmodel/convert_user.go#L10-L13

Added lines #L10 - L13 were not covered by tests

return &User{
ID: IDFrom(u.ID),
Name: u.Name,
Email: u.Email,
}

Check warning on line 19 in server/internal/adapter/gql/gqlmodel/convert_user.go

View check run for this annotation

Codecov / codecov/patch

server/internal/adapter/gql/gqlmodel/convert_user.go#L15-L19

Added lines #L15 - L19 were not covered by tests
}

func ToUser(u *user.User) *User {
if u == nil {
return nil
Expand Down
6 changes: 3 additions & 3 deletions server/internal/adapter/gql/loader_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (c *UserLoader) Fetch(ctx context.Context, ids []gqlmodel.ID) ([]*gqlmodel.
return nil, []error{err}
}

res, err := c.usecase.Fetch(ctx, uids, getAcOperator(ctx))
res, err := c.usecase.FetchByID(ctx, uids)
if err != nil {
return nil, []error{err}
}
Expand All @@ -38,12 +38,12 @@ func (c *UserLoader) Fetch(ctx context.Context, ids []gqlmodel.ID) ([]*gqlmodel.
}

func (c *UserLoader) SearchUser(ctx context.Context, nameOrEmail string) (*gqlmodel.User, error) {
res, err := c.usecase.SearchUser(ctx, nameOrEmail, getAcOperator(ctx))
res, err := c.usecase.SearchUser(ctx, nameOrEmail)
if err != nil {
return nil, err
}

return gqlmodel.ToUser(res), nil
return gqlmodel.SimpleToUser(res), nil
}

// data loader
Expand Down
9 changes: 2 additions & 7 deletions server/internal/app/auth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/reearth/reearthx/account/accountusecase"
"github.com/reearth/reearthx/account/accountusecase/accountinteractor"
"github.com/reearth/reearthx/account/accountusecase/accountinterfaces"
"github.com/reearth/reearthx/appx"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/usecasex"
Expand Down Expand Up @@ -55,12 +54,8 @@

if ai := adapter.GetAuthInfo(ctx); ai != nil {
var err error
userUsecase := accountinteractor.NewUser(cfg.AcRepos, cfg.AcGateways, cfg.Config.SignupSecret, cfg.Config.Host_Web)
u, err = userUsecase.FindOrCreate(ctx, accountinterfaces.UserFindOrCreateParam{
Sub: ai.Sub,
ISS: ai.Iss,
Token: ai.Token,
})
userUsecase := accountinteractor.NewMultiUser(cfg.AcRepos, cfg.AcGateways, cfg.Config.SignupSecret, cfg.Config.Host_Web, cfg.AcRepos.Users)
u, err = userUsecase.FetchBySub(ctx, ai.Sub)

Check warning on line 58 in server/internal/app/auth_client.go

View check run for this annotation

Codecov / codecov/patch

server/internal/app/auth_client.go#L57-L58

Added lines #L57 - L58 were not covered by tests
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions server/internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Config struct {
AuthM2M AuthM2MConfig

DB_Account string
DB_Users []appx.NamedURI
}

type AuthConfig struct {
Expand Down
11 changes: 10 additions & 1 deletion server/internal/app/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@
log.Fatalf("repo initialization error: %+v\n", err)
}

accountUsers := make([]accountrepo.User, 0, len(conf.DB_Users))
for _, u := range conf.DB_Users {
c, err := mongo.Connect(ctx, options.Client().ApplyURI(u.URI).SetMonitor(otelmongo.NewMonitor()))
if err != nil {
log.Fatalf("mongo error: %+v\n", err)
}
accountUsers = append(accountUsers, accountmongo.NewUserWithHost(mongox.NewClient("reearth_account", c), u.Name))

Check warning on line 50 in server/internal/app/repo.go

View check run for this annotation

Codecov / codecov/patch

server/internal/app/repo.go#L44-L50

Added lines #L44 - L50 were not covered by tests
}

txAvailable := mongox.IsTransactionAvailable(conf.DB)

accountDatabase := conf.DB_Account
if accountDatabase == "" {
accountDatabase = databaseName
}
acRepos, err := accountmongo.New(ctx, client, accountDatabase, txAvailable, false)
acRepos, err := accountmongo.New(ctx, client, accountDatabase, txAvailable, false, accountUsers)

Check warning on line 59 in server/internal/app/repo.go

View check run for this annotation

Codecov / codecov/patch

server/internal/app/repo.go#L59

Added line #L59 was not covered by tests
if err != nil {
log.Fatalf("Failed to init mongo: %+v\n", err)
}
Expand Down
7 changes: 4 additions & 3 deletions server/internal/usecase/interactor/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/reearth/reearthx/account/accountdomain/user"
"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/reearth/reearthx/account/accountusecase/accountinteractor"
"github.com/reearth/reearthx/account/accountusecase/accountrepo"
"github.com/reearth/reearthx/util"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -124,12 +125,12 @@ func TestCommon_webhook(t *testing.T) {
}

func TestNew(t *testing.T) {
uc := New(nil, nil, nil, nil, ContainerConfig{})
uc := New(nil, nil, &accountrepo.Container{}, nil, ContainerConfig{})
assert.NotNil(t, uc)
assert.Equal(t, interfaces.Container{
Asset: NewAsset(nil, nil),
Workspace: accountinteractor.NewWorkspace(nil, nil),
User: accountinteractor.NewUser(nil, nil, "", ""),
Workspace: accountinteractor.NewWorkspace(&accountrepo.Container{}, nil),
User: accountinteractor.NewUser(&accountrepo.Container{}, nil, "", ""),
Item: NewItem(nil, nil),
View: NewView(nil, nil),
Project: NewProject(nil, nil),
Expand Down
Loading