1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

Add config to services.

This commit is contained in:
Ferdinand Mütsch 2019-05-16 22:53:03 +02:00
parent c7a8372516
commit de65ab1814
4 changed files with 11 additions and 8 deletions

12
main.go
View File

@ -23,7 +23,7 @@ import (
_ "github.com/jinzhu/gorm/dialects/mysql" _ "github.com/jinzhu/gorm/dialects/mysql"
) )
func readConfig() models.Config { func readConfig() *models.Config {
if err := godotenv.Load(); err != nil { if err := godotenv.Load(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -46,7 +46,7 @@ func readConfig() models.Config {
intervalStr := cfg.Section("data").Key("aggregation_interval").String() intervalStr := cfg.Section("data").Key("aggregation_interval").String()
interval, _ := time.ParseDuration(intervalStr) interval, _ := time.ParseDuration(intervalStr)
return models.Config{ return &models.Config{
Port: port, Port: port,
DbHost: dbHost, DbHost: dbHost,
DbUser: dbUser, DbUser: dbUser,
@ -62,7 +62,7 @@ func main() {
config := readConfig() config := readConfig()
// Connect to database // Connect to database
db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(&config)) db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config))
if err != nil { if err != nil {
// log.Fatal("Could not connect to database.") // log.Fatal("Could not connect to database.")
log.Fatal(err) log.Fatal(err)
@ -76,9 +76,9 @@ func main() {
db.AutoMigrate(&models.AggregationItem{}).AddForeignKey("aggregation_id", "aggregations(id)", "RESTRICT", "RESTRICT") db.AutoMigrate(&models.AggregationItem{}).AddForeignKey("aggregation_id", "aggregations(id)", "RESTRICT", "RESTRICT")
// Services // Services
heartbeatSrvc := &services.HeartbeatService{db} heartbeatSrvc := &services.HeartbeatService{config, db}
userSrvc := &services.UserService{db} userSrvc := &services.UserService{config, db}
aggregationSrvc := &services.AggregationService{db, heartbeatSrvc} aggregationSrvc := &services.AggregationService{config, db, heartbeatSrvc}
// Handlers // Handlers
heartbeatHandler := &routes.HeartbeatHandler{HeartbeatSrvc: heartbeatSrvc} heartbeatHandler := &routes.HeartbeatHandler{HeartbeatSrvc: heartbeatSrvc}

View File

@ -10,6 +10,7 @@ import (
) )
type AggregationService struct { type AggregationService struct {
Config *models.Config
Db *gorm.DB Db *gorm.DB
HeartbeatService *HeartbeatService HeartbeatService *HeartbeatService
} }

View File

@ -11,7 +11,8 @@ import (
const TableHeartbeat = "heartbeat" const TableHeartbeat = "heartbeat"
type HeartbeatService struct { type HeartbeatService struct {
Db *gorm.DB Cofnig *models.Config
Db *gorm.DB
} }
func (srv *HeartbeatService) InsertBatch(heartbeats *[]models.Heartbeat) error { func (srv *HeartbeatService) InsertBatch(heartbeats *[]models.Heartbeat) error {

View File

@ -8,7 +8,8 @@ import (
const TableUser = "user" const TableUser = "user"
type UserService struct { type UserService struct {
Db *gorm.DB Config *models.Config
Db *gorm.DB
} }
func (srv *UserService) GetUserById(userId string) (*models.User, error) { func (srv *UserService) GetUserById(userId string) (*models.User, error) {