Skip to content
This repository has been archived by the owner on May 6, 2021. It is now read-only.

Commit

Permalink
Fixes #12 Update Gopkg.toml to use fabric8-common from f8-services (#13)
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
chmouel authored Oct 3, 2018
1 parent ee3e22f commit 2967c99
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 15 deletions.
9 changes: 4 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
147 changes: 147 additions & 0 deletions configuration/configuration.go
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(),
)
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion migration/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down

0 comments on commit 2967c99

Please sign in to comment.