This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Since postgresql configuration was removed from the latest, we do our own configurations by extending common ones, where we add back the postgres there. We may have more need for this in the future anyway. Don't clean the non used go package, because deps don't see the goa generated ones and cleans them up
- Loading branch information
Showing
5 changed files
with
156 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters