Skip to content

Commit

Permalink
refactor: abstract configs as a domain level object
Browse files Browse the repository at this point in the history
  • Loading branch information
whoAbhishekSah committed Mar 1, 2021
1 parent c7f8c9f commit 6d73029
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 40 deletions.
25 changes: 5 additions & 20 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,11 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"strings"
"github.com/odpf/siren/domain"
)

// DBConfig contains the database configuration
type DBConfig struct {
Host string `mapstructure:"host"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
Name string `mapstructure:"name" default:"postgres"`
Port string `mapstructure:"port" default:"5432"`
SslMode string `mapstructure:"sslmode"`
}

// Config contains the application configuration
type Config struct {
Port int `mapstructure:"port" default:"8080"`
DB DBConfig `mapstructure:"db"`
}

// LoadConfig returns application configuration
func LoadConfig() *Config {
func LoadConfig() *domain.Config {
viper.SetConfigName("config")
viper.AddConfigPath("./")
viper.AddConfigPath("../")
Expand All @@ -43,7 +28,7 @@ func LoadConfig() *Config {
}
}

err, configKeys := getFlattenedStructKeys(Config{})
err, configKeys := getFlattenedStructKeys(domain.Config{})
if err != nil {
panic(err)
}
Expand All @@ -56,7 +41,7 @@ func LoadConfig() *Config {
}
}

var config Config
var config domain.Config
defaults.SetDefaults(&config)

err = viper.Unmarshal(&config)
Expand All @@ -66,7 +51,7 @@ func LoadConfig() *Config {
return &config
}

func getFlattenedStructKeys(config Config) (error, []string) {
func getFlattenedStructKeys(config domain.Config) (error, []string) {
var structMap map[string]interface{}
err := mapstructure.Decode(config, &structMap)
if err != nil {
Expand Down
12 changes: 3 additions & 9 deletions app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"fmt"
"github.com/odpf/siren/domain"
"log"
"net/http"

Expand All @@ -10,15 +11,8 @@ import (
)

// RunServer runs the application server
func RunServer(c *Config) error {
db, err := store.New(&store.Config{
Host: c.DB.Host,
User: c.DB.User,
Password: c.DB.Password,
Name: c.DB.Name,
Port: c.DB.Port,
SslMode: c.DB.SslMode,
})
func RunServer(c *domain.Config) error {
db, err := store.New(&c.DB)
if err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions domain/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package domain

// DBConfig contains the database configuration
type DBConfig struct {
Host string `mapstructure:"host"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
Name string `mapstructure:"name" default:"postgres"`
Port string `mapstructure:"port" default:"5432"`
SslMode string `mapstructure:"sslmode"`
}

// Config contains the application configuration
type Config struct {
Port int `mapstructure:"port" default:"8080"`
DB DBConfig `mapstructure:"db"`
}
13 changes: 2 additions & 11 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@ package store

import (
"fmt"
"github.com/odpf/siren/domain"
"log"

"gorm.io/driver/postgres"
"gorm.io/gorm"
)

// Config for database connection
type Config struct {
Host string
User string
Password string
Name string
Port string
SslMode string
}

// New returns the database instance
func New(c *Config) (*gorm.DB, error) {
func New(c *domain.DBConfig) (*gorm.DB, error) {
dsn := fmt.Sprintf(
"host=%s user=%s dbname=%s password=%s port=%s sslmode=%s",
c.Host,
Expand Down

0 comments on commit 6d73029

Please sign in to comment.