Skip to content

Commit

Permalink
Merge branch 'main' into feat-server/schemata-item-copy
Browse files Browse the repository at this point in the history
  • Loading branch information
mimoham24 authored Jan 15, 2025
2 parents 90b5277 + eda8b24 commit 8cce3b8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
5 changes: 3 additions & 2 deletions server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
PORT=8080
REEARTH_CMS_DB=mongodb://localhost
REEARTH_CMS_HOST=https://localhost:8080
REEARTH_CMS_SERVERHOST=https://localhost:8080
REEARTH_CMS_SERVERHOST=localhost
REEARTH_CMS_HOST_WEB=https://localhost:3000
REEARTH_CMS_ASSETBASEURL=https://localhost:8080/assets
REEARTH_CMS_DEV=false
REEARTH_CMS_SIGNUPSECRET=
REEARTH_CMS_ORIGINS=www.example.com,localhost:3000

REEARTH_CMS_DB_ACCOUNT=mongodb://localhost
REEARTH_CMS_DB_ACCOUNT=reearth_account
REEARTH_CMS_DB_CMS=reearth_cms
REEARTH_CMS_DB_USERS=Test1=mongodb://localhost,Test2=mongodb://localhost

#GraphQL
Expand Down
3 changes: 2 additions & 1 deletion server/internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ type Config struct {
// auth for m2m
AuthM2M AuthM2MConfig `pp:",omitempty"`

DB_Account string `pp:",omitempty"`
DB_Account string `default:"reearth_account" pp:",omitempty"`
DB_CMS string `default:"reearth_cms" pp:",omitempty"`
DB_Users []appx.NamedURI `pp:",omitempty"`
}

Expand Down
59 changes: 35 additions & 24 deletions server/internal/app/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,38 @@ import (
"go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo"
)

const databaseName = "reearth_cms"
func initAccountDB(client *mongo.Client, txAvailable bool, ctx context.Context, conf *Config) *accountrepo.Container {
accountDatabase := conf.DB_Account
log.Infof("account database: %s", accountDatabase)

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(accountDatabase, c), u.Name))
}

acRepos, err := accountmongo.New(ctx, client, accountDatabase, txAvailable, false, accountUsers)
if err != nil {
log.Fatalf("Failed to init mongo: %+v\n", err)
}

return acRepos
}

func initCMSDB(client *mongo.Client, txAvailable bool, acRepos *accountrepo.Container, ctx context.Context, conf *Config) *repo.Container {
cmsDatabase := conf.DB_CMS
log.Infof("cms database: %s", cmsDatabase)

repos, err := mongorepo.New(ctx, client, cmsDatabase, txAvailable, acRepos)
if err != nil {
log.Fatalf("Failed to init mongo: %+v\n", err)
}

return repos
}

func initReposAndGateways(ctx context.Context, conf *Config) (*repo.Container, *gateway.Container, *accountrepo.Container, *accountgateway.Container) {
gateways := &gateway.Container{}
Expand All @@ -42,31 +73,11 @@ func initReposAndGateways(ctx context.Context, conf *Config) (*repo.Container, *
log.Fatalf("repo initialization error: %+v\n", err)
}

accountDatabase := conf.DB_Account
if accountDatabase == "" {
accountDatabase = databaseName
}

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(accountDatabase, c), u.Name))
}

txAvailable := mongox.IsTransactionAvailable(conf.DB)

acRepos, err := accountmongo.New(ctx, client, accountDatabase, txAvailable, false, accountUsers)
if err != nil {
log.Fatalf("Failed to init mongo: %+v\n", err)
}
acRepos := initAccountDB(client, txAvailable, ctx, conf)
cmsRepos := initCMSDB(client, txAvailable, acRepos, ctx, conf)

repos, err := mongorepo.New(ctx, client, databaseName, txAvailable, acRepos)
if err != nil {
log.Fatalf("Failed to init mongo: %+v\n", err)
}
// File
var fileRepo gateway.File
if conf.GCS.BucketName != "" {
Expand Down Expand Up @@ -117,7 +128,7 @@ func initReposAndGateways(ctx context.Context, conf *Config) (*repo.Container, *
log.Infof("task runner: not used")
}

return repos, gateways, acRepos, acGateways
return cmsRepos, gateways, acRepos, acGateways
}

func NewLogMonitor() *event.CommandMonitor {
Expand Down

0 comments on commit 8cce3b8

Please sign in to comment.