From d6f3e7c87fd82052c9ceecb58955e474264b8226 Mon Sep 17 00:00:00 2001 From: Leo <27057423+hokkung@users.noreply.github.com> Date: Tue, 25 Jun 2024 22:48:28 +0700 Subject: [PATCH] refactor config package (#11) Co-authored-by: Boonyarit Rousamran --- config/app.go | 27 +++++++++++++++++++ config/config.go | 55 +++++++-------------------------------- config/mysql.go | 42 ++++++++++++++++++++++++++++++ internal/di/model.go | 3 +++ internal/di/wire_gen.go | 9 ++++--- internal/repository/db.go | 6 ++--- 6 files changed, 90 insertions(+), 52 deletions(-) create mode 100644 config/app.go create mode 100644 config/mysql.go diff --git a/config/app.go b/config/app.go new file mode 100644 index 0000000..8b62c8c --- /dev/null +++ b/config/app.go @@ -0,0 +1,27 @@ +package config + +import ( + "fmt" + "github.com/kelseyhightower/envconfig" +) + +type AppProperties struct { + Name string `envconfig:"NAME" default:"groceries_api"` +} + +// NewAppProperties creates instance +func NewAppProperties() *AppProperties { + var app AppProperties + err := envconfig.Process(AppPrefix, &app) + if err != nil { + fmt.Println("binding app configuration failed", err.Error()) + panic(err) + } + + return &app +} + +// ProvideAppProperties provides instance for di +func ProvideAppProperties() AppProperties { + return *NewAppProperties() +} diff --git a/config/config.go b/config/config.go index f7640c9..b9a743f 100644 --- a/config/config.go +++ b/config/config.go @@ -1,11 +1,5 @@ package config -import ( - "fmt" - - "github.com/kelseyhightower/envconfig" -) - const AppPrefix = "APP" type Configuration struct { @@ -13,45 +7,11 @@ type Configuration struct { MysqlProperties MysqlProperties } -type AppProperties struct { - Name string `envconfig:"NAME" default:"groceries_api"` -} - -type MysqlProperties struct { - Host string `envconfig:"MYSQL_HOST" default:"localhost"` - Port string `envconfig:"MYSQL_PORT" default:"3306"` - Name string `envconfig:"MYSQL_DB_NAME" default:"grocery"` - UserName string `envconfig:"MYSQL_USERNAME" default:"root"` - Password string `envconfig:"MYSQL_PASSWORD" default:"root"` -} - -func (c *MysqlProperties) DNS() string { - return fmt.Sprintf( - "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", - c.UserName, - c.Password, - c.Host, - c.Port, - c.Name, - ) -} - // NewConfiguration returns new config instance -func NewConfiguration() *Configuration { - var app AppProperties - err := envconfig.Process(AppPrefix, &app) - if err != nil { - fmt.Println("binding app configuration failed", err.Error()) - panic(err) - } - - var mysql MysqlProperties - err = envconfig.Process(AppPrefix, &mysql) - if err != nil { - fmt.Println("binding mysql configuration failed", err.Error()) - panic(err) - } - +func NewConfiguration( + app AppProperties, + mysql MysqlProperties, +) *Configuration { return &Configuration{ AppProperties: app, MysqlProperties: mysql, @@ -59,6 +19,9 @@ func NewConfiguration() *Configuration { } // ProvideConfiguration provides configuration for di -func ProvideConfiguration() Configuration { - return *NewConfiguration() +func ProvideConfiguration( + app AppProperties, + mysql MysqlProperties, +) Configuration { + return *NewConfiguration(app, mysql) } diff --git a/config/mysql.go b/config/mysql.go new file mode 100644 index 0000000..c6ebde8 --- /dev/null +++ b/config/mysql.go @@ -0,0 +1,42 @@ +package config + +import ( + "fmt" + "github.com/kelseyhightower/envconfig" +) + +type MysqlProperties struct { + Host string `envconfig:"MYSQL_HOST" default:"localhost"` + Port string `envconfig:"MYSQL_PORT" default:"3306"` + Name string `envconfig:"MYSQL_DB_NAME" default:"grocery"` + UserName string `envconfig:"MYSQL_USERNAME" default:"root"` + Password string `envconfig:"MYSQL_PASSWORD" default:"root"` +} + +func (c *MysqlProperties) DNS() string { + return fmt.Sprintf( + "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", + c.UserName, + c.Password, + c.Host, + c.Port, + c.Name, + ) +} + +// NewMysqlProperties creates instance +func NewMysqlProperties() *MysqlProperties { + var mysql MysqlProperties + err := envconfig.Process(AppPrefix, &mysql) + if err != nil { + fmt.Println("binding mysql configuration failed", err.Error()) + panic(err) + } + + return &mysql +} + +// ProvideMysqlProperties provides instance for di +func ProvideMysqlProperties() MysqlProperties { + return *NewMysqlProperties() +} diff --git a/internal/di/model.go b/internal/di/model.go index 2a8dc40..3876cac 100644 --- a/internal/di/model.go +++ b/internal/di/model.go @@ -26,6 +26,8 @@ var APISet = wire.NewSet( ) var ConfigSet = wire.NewSet( + config.ProvideAppProperties, + config.ProvideMysqlProperties, config.ProvideConfiguration, ) @@ -65,4 +67,5 @@ var ExternalSet = wire.NewSet( type ApplicationAPI struct { Server *srv.Server + Config config.Configuration } diff --git a/internal/di/wire_gen.go b/internal/di/wire_gen.go index 1e519d8..e49b9fa 100644 --- a/internal/di/wire_gen.go +++ b/internal/di/wire_gen.go @@ -1,6 +1,6 @@ // Code generated by Wire. DO NOT EDIT. -//go:generate go run github.com/google/wire/cmd/wire +//go:generate go run -mod=mod github.com/google/wire/cmd/wire //go:build !wireinject // +build !wireinject @@ -22,8 +22,8 @@ import ( // Injectors from wire.go: func InitializeApplication(context2 context.Context) (*ApplicationAPI, func(), error) { - configuration := config.ProvideConfiguration() - db, err := repository.ProvideGormDB(configuration) + mysqlProperties := config.ProvideMysqlProperties() + db, err := repository.ProvideGormDB(mysqlProperties) if err != nil { return nil, nil, err } @@ -61,8 +61,11 @@ func InitializeApplication(context2 context.Context) (*ApplicationAPI, func(), e cleanup() return nil, nil, err } + appProperties := config.ProvideAppProperties() + configuration := config.ProvideConfiguration(appProperties, mysqlProperties) applicationAPI := &ApplicationAPI{ Server: serverServer, + Config: configuration, } return applicationAPI, func() { cleanup4() diff --git a/internal/repository/db.go b/internal/repository/db.go index e8ae195..bf4fd14 100644 --- a/internal/repository/db.go +++ b/internal/repository/db.go @@ -13,7 +13,7 @@ type Entity interface { Table() string } -func ProvideGormDB(config config.Configuration) (*gorm.DB, error) { +func ProvideGormDB(config config.MysqlProperties) (*gorm.DB, error) { return NewGormDB(config) } @@ -22,8 +22,8 @@ var entities = []Entity{ &entity2.User{}, } -func NewGormDB(config config.Configuration) (*gorm.DB, error) { - dsn := config.MysqlProperties.DNS() +func NewGormDB(config config.MysqlProperties) (*gorm.DB, error) { + dsn := config.DNS() db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { return nil, err