Skip to content

Commit 0c4f6f9

Browse files
authored
Fixes service Db dependency on launch time (#59)
1 parent d3df545 commit 0c4f6f9

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/db/mongo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func InitDb() *mongo.Database {
2525
err = client.Ping(context.Background(), nil)
2626
if err != nil {
2727
utils.LogError("Error connecting to MongoDB: %s", err.Error())
28+
return nil
2829
}
2930

3031
utils.LogInfo("Connected to MongoDB!")

src/server/app.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,26 @@ func startServerWithSsl(routerHandlers *mux.Router) *http.Server {
9797
}
9898

9999
func initRoutes(db *mongo.Database, coreHandler *core.CoreHandler) *mux.Router {
100-
accountRepository := repository.NewAccountRepositoryMongo(db)
100+
r := mux.NewRouter()
101101

102102
apiController := controller.NewApiController(coreHandler)
103-
accountController := controller.NewAccountController(accountRepository, coreHandler)
104-
105-
r := mux.NewRouter()
106103
apiController.RegisterRoutes(r)
107-
accountController.RegisterRoutes(r)
104+
105+
if db != nil {
106+
accountRepository := repository.NewAccountRepositoryMongo(db)
107+
accountController := controller.NewAccountController(accountRepository, coreHandler)
108+
accountController.RegisterRoutes(r)
109+
}
108110

109111
return r
110112
}
111113

112114
func initCoreHandler(db *mongo.Database) *core.CoreHandler {
115+
var coreHandler *core.CoreHandler
116+
if db == nil {
117+
return core.NewCoreHandler(nil, nil, nil)
118+
}
119+
113120
accountRepository := repository.NewAccountRepositoryMongo(db)
114121
comparatorService := core.NewComparatorService()
115122
apiService := core.NewApiService(
@@ -118,7 +125,7 @@ func initCoreHandler(db *mongo.Database) *core.CoreHandler {
118125
config.GetEnv("SWITCHER_API_CA_CERT"),
119126
)
120127

121-
coreHandler := core.NewCoreHandler(accountRepository, apiService, comparatorService)
128+
coreHandler = core.NewCoreHandler(accountRepository, apiService, comparatorService)
122129
coreHandler.InitCoreHandlerGoroutine()
123130

124131
return coreHandler

0 commit comments

Comments
 (0)