From de65ab1814566594e53afe8b2f7f0fbfc3cbd70d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Thu, 16 May 2019 22:53:03 +0200 Subject: [PATCH] Add config to services. --- main.go | 12 ++++++------ services/aggregation.go | 1 + services/heartbeat.go | 3 ++- services/user.go | 3 ++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index defc0ec..49a55fa 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ import ( _ "github.com/jinzhu/gorm/dialects/mysql" ) -func readConfig() models.Config { +func readConfig() *models.Config { if err := godotenv.Load(); err != nil { log.Fatal(err) } @@ -46,7 +46,7 @@ func readConfig() models.Config { intervalStr := cfg.Section("data").Key("aggregation_interval").String() interval, _ := time.ParseDuration(intervalStr) - return models.Config{ + return &models.Config{ Port: port, DbHost: dbHost, DbUser: dbUser, @@ -62,7 +62,7 @@ func main() { config := readConfig() // Connect to database - db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(&config)) + db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config)) if err != nil { // log.Fatal("Could not connect to database.") log.Fatal(err) @@ -76,9 +76,9 @@ func main() { db.AutoMigrate(&models.AggregationItem{}).AddForeignKey("aggregation_id", "aggregations(id)", "RESTRICT", "RESTRICT") // Services - heartbeatSrvc := &services.HeartbeatService{db} - userSrvc := &services.UserService{db} - aggregationSrvc := &services.AggregationService{db, heartbeatSrvc} + heartbeatSrvc := &services.HeartbeatService{config, db} + userSrvc := &services.UserService{config, db} + aggregationSrvc := &services.AggregationService{config, db, heartbeatSrvc} // Handlers heartbeatHandler := &routes.HeartbeatHandler{HeartbeatSrvc: heartbeatSrvc} diff --git a/services/aggregation.go b/services/aggregation.go index 97ad35b..eaedafc 100644 --- a/services/aggregation.go +++ b/services/aggregation.go @@ -10,6 +10,7 @@ import ( ) type AggregationService struct { + Config *models.Config Db *gorm.DB HeartbeatService *HeartbeatService } diff --git a/services/heartbeat.go b/services/heartbeat.go index 4bf6f56..901b67d 100644 --- a/services/heartbeat.go +++ b/services/heartbeat.go @@ -11,7 +11,8 @@ import ( const TableHeartbeat = "heartbeat" type HeartbeatService struct { - Db *gorm.DB + Cofnig *models.Config + Db *gorm.DB } func (srv *HeartbeatService) InsertBatch(heartbeats *[]models.Heartbeat) error { diff --git a/services/user.go b/services/user.go index 747bc26..08809b1 100644 --- a/services/user.go +++ b/services/user.go @@ -8,7 +8,8 @@ import ( const TableUser = "user" type UserService struct { - Db *gorm.DB + Config *models.Config + Db *gorm.DB } func (srv *UserService) GetUserById(userId string) (*models.User, error) {