Skip to content

Commit

Permalink
feat(server): support multiple tenants (#1068)
Browse files Browse the repository at this point in the history
* feat(server): support multiple tenants

* test(server): fix broken tests

* chore: update dependencies

---------

Co-authored-by: rot1024 <[email protected]>
  • Loading branch information
orisano and rot1024 authored Mar 7, 2024
1 parent 73ed11c commit 9a24e83
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 17 deletions.
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 @@ import (
"github.com/reearth/reearthx/util"
)

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

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

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 @@ import (
"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 @@ func attachUserOperator(ctx context.Context, req *http.Request, cfg *ServerConfi

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)
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 @@ func initReposAndGateways(ctx context.Context, conf *Config, debug bool) (*repo.
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))
}

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)
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

0 comments on commit 9a24e83

Please sign in to comment.