diff --git a/Gopkg.lock b/Gopkg.lock index 0a40bb6..7ffdfcd 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -50,20 +50,19 @@ version = "v5.0.1" [[projects]] + branch = "master" name = "github.com/fabric8-services/fabric8-common" packages = [ "configuration", - "errors", "goasupport/jsonapi_errors_helpers", "goasupport/status", - "httpsupport", "log", "metric", "resource", "sentry" ] - revision = "fc46960" - source = "https://github.com/xcoulon/fabric8-common.git" + revision = "13443074c10b54b60327441aa49dc321d5cac6b8" + source = "https://github.com/fabric8-services/fabric8-common.git" [[projects]] name = "github.com/fsnotify/fsnotify" @@ -418,6 +417,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "66354ee675a2bf1a1232fa4f25ec41e7d4427e76bb3ca11dec4677b2a0c60cee" + inputs-digest = "adf744b57b55f1955795e63debf36a7d0d37568b99ea3559f88641866f8a39dd" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 926b173..d349bda 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -49,12 +49,7 @@ ignored = [ "github.com/fabric8-services/fabric8-build-service/app", ] - -[prune] - go-tests = true - unused-packages = true - [[constraint]] name = "github.com/fabric8-services/fabric8-common" - source = "https://github.com/xcoulon/fabric8-common.git" - revision = "fc46960" + source = "https://github.com/fabric8-services/fabric8-common.git" + branch = "master" diff --git a/configuration/configuration.go b/configuration/configuration.go new file mode 100644 index 0000000..a580816 --- /dev/null +++ b/configuration/configuration.go @@ -0,0 +1,147 @@ +package configuration + +import ( + "fmt" + "strings" + "time" + + commonconfig "github.com/fabric8-services/fabric8-common/configuration" + errs "github.com/pkg/errors" + "github.com/spf13/viper" +) + +const ( + varPostgresHost = "postgres.host" + varPostgresPort = "postgres.port" + varPostgresUser = "postgres.user" + varPostgresDatabase = "postgres.database" + varPostgresPassword = "postgres.password" + varPostgresSSLMode = "postgres.sslmode" + varPostgresConnectionTimeout = "postgres.connection.timeout" + varPostgresTransactionTimeout = "postgres.transaction.timeout" + varPostgresConnectionRetrySleep = "postgres.connection.retrysleep" + varPostgresConnectionMaxIdle = "postgres.connection.maxidle" + varPostgresConnectionMaxOpen = "postgres.connection.maxopen" +) + +// New creates a configuration reader object using a configurable configuration +// file path. +func New(configFilePath string) (*Config, error) { + c := Config{ + v: viper.New(), + } + c.v.SetEnvPrefix("F8") + c.v.AutomaticEnv() + c.v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + c.v.SetTypeByDefaultValue(true) + c.setConfigDefaults() + + if configFilePath != "" { + c.v.SetConfigType("yaml") + c.v.SetConfigFile(configFilePath) + err := c.v.ReadInConfig() // Find and read the config file + if err != nil { // Handle errors reading the config file + return nil, errs.Errorf("Fatal error config file: %s \n", err) + } + } + return &c, nil +} + +// Config extends fabric8-common Viper configuration which stores the +// configuration data in-memory. +type Config struct { + commonconfig.Registry + v *viper.Viper +} + +func (c *Config) setConfigDefaults() { + //--------- + // Postgres + //--------- + c.v.SetTypeByDefaultValue(true) + c.v.SetDefault(varPostgresHost, "localhost") + c.v.SetDefault(varPostgresPort, 5432) + c.v.SetDefault(varPostgresUser, "postgres") + c.v.SetDefault(varPostgresDatabase, "postgres") + c.v.SetDefault(varPostgresPassword, "mysecretpassword") + c.v.SetDefault(varPostgresSSLMode, "disable") + c.v.SetDefault(varPostgresConnectionTimeout, 5) + c.v.SetDefault(varPostgresConnectionMaxIdle, -1) + c.v.SetDefault(varPostgresConnectionMaxOpen, -1) + // Number of seconds to wait before trying to connect again + c.v.SetDefault(varPostgresConnectionRetrySleep, time.Second) + + // Timeout of a transaction in minutes + c.v.SetDefault(varPostgresTransactionTimeout, 5*time.Minute) +} + +// GetPostgresHost returns the postgres host as set via default, config file, or environment variable +func (c *Config) GetPostgresHost() string { + return c.v.GetString(varPostgresHost) +} + +// GetPostgresPort returns the postgres port as set via default, config file, or environment variable +func (c *Config) GetPostgresPort() int64 { + return c.v.GetInt64(varPostgresPort) +} + +// GetPostgresUser returns the postgres user as set via default, config file, or environment variable +func (c *Config) GetPostgresUser() string { + return c.v.GetString(varPostgresUser) +} + +// GetPostgresDatabase returns the postgres database as set via default, config file, or environment variable +func (c *Config) GetPostgresDatabase() string { + return c.v.GetString(varPostgresDatabase) +} + +// GetPostgresPassword returns the postgres password as set via default, config file, or environment variable +func (c *Config) GetPostgresPassword() string { + return c.v.GetString(varPostgresPassword) +} + +// GetPostgresSSLMode returns the postgres sslmode as set via default, config file, or environment variable +func (c *Config) GetPostgresSSLMode() string { + return c.v.GetString(varPostgresSSLMode) +} + +// GetPostgresConnectionTimeout returns the postgres connection timeout as set via default, config file, or environment variable +func (c *Config) GetPostgresConnectionTimeout() int64 { + return c.v.GetInt64(varPostgresConnectionTimeout) +} + +// GetPostgresConnectionRetrySleep returns the number of seconds (as set via default, config file, or environment variable) +// to wait before trying to connect again +func (c *Config) GetPostgresConnectionRetrySleep() time.Duration { + return c.v.GetDuration(varPostgresConnectionRetrySleep) +} + +// GetPostgresTransactionTimeout returns the number of minutes to timeout a transaction +func (c *Config) GetPostgresTransactionTimeout() time.Duration { + return c.v.GetDuration(varPostgresTransactionTimeout) +} + +// GetPostgresConnectionMaxIdle returns the number of connections that should be keept alive in the database connection pool at +// any given time. -1 represents no restrictions/default behavior +func (c *Config) GetPostgresConnectionMaxIdle() int { + return c.v.GetInt(varPostgresConnectionMaxIdle) +} + +// GetPostgresConnectionMaxOpen returns the max number of open connections that should be open in the database connection pool. +// -1 represents no restrictions/default behavior +func (c *Config) GetPostgresConnectionMaxOpen() int { + return c.v.GetInt(varPostgresConnectionMaxOpen) +} + +// GetPostgresConfigString returns a ready to use string for usage in sql.Open() +func (c *Config) GetPostgresConfigString() string { + return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s connect_timeout=%d", + c.GetPostgresHost(), + c.GetPostgresPort(), + c.GetPostgresUser(), + c.GetPostgresPassword(), + c.GetPostgresDatabase(), + c.GetPostgresSSLMode(), + c.GetPostgresConnectionTimeout(), + ) +} diff --git a/main.go b/main.go index bdaa2ae..635303f 100644 --- a/main.go +++ b/main.go @@ -10,9 +10,9 @@ import ( "time" "github.com/fabric8-services/fabric8-build-service/app" + "github.com/fabric8-services/fabric8-build-service/configuration" "github.com/fabric8-services/fabric8-build-service/controller" "github.com/fabric8-services/fabric8-build-service/migration" - "github.com/fabric8-services/fabric8-common/configuration" "github.com/fabric8-services/fabric8-common/log" "github.com/fabric8-services/fabric8-common/metric" "github.com/fabric8-services/fabric8-common/sentry" @@ -169,7 +169,7 @@ func main() { } -func connect(config *configuration.Registry) *gorm.DB { +func connect(config *configuration.Config) *gorm.DB { var err error var db *gorm.DB for { diff --git a/migration/migration_test.go b/migration/migration_test.go index 806ec65..b6e7070 100644 --- a/migration/migration_test.go +++ b/migration/migration_test.go @@ -10,7 +10,7 @@ import ( _ "github.com/lib/pq" "github.com/stretchr/testify/assert" - config "github.com/fabric8-services/fabric8-common/configuration" + config "github.com/fabric8-services/fabric8-build-service/configuration" ) // fail - as t.Fatalf() is not goroutine safe, this function behaves like t.Fatalf().