From 069d655d9c6dc8b490c72c0296458c69010aa838 Mon Sep 17 00:00:00 2001 From: dhax Date: Thu, 30 Jan 2020 14:59:00 +0100 Subject: [PATCH] split DB connection string into separate env vars --- README.md | 6 +++++- cmd/root.go | 3 --- database/postgres.go | 17 ++++++++++++----- go.mod | 2 ++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e01df15..fc297b5 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,11 @@ Name | Type | Default | Description PORT | string | localhost:3000 | http address (accepts also port number only for heroku compability) LOG_LEVEL | string | debug | log level LOG_TEXTLOGGING | bool | false | defaults to json logging -DATABASE_URL | string | postgres://postgres:postgres
@localhost:5432/gobase?sslmode=disable | PostgreSQL connection string +DB_NETWORK | string | tcp | database 'tcp' or 'unix' connection +DB_ADDR | string | localhost:5432 | database tcp address or unix socket +DB_USER | string | postgres | database user name +DB_PASSWORD | string | postgres | database user password +DB_DATABASE | string | gobase | database shema name AUTH_LOGIN_URL | string | http://localhost:3000/login | client login url as sent in login token email AUTH_LOGIN_TOKEN_LENGTH | int | 8 | length of login token AUTH_LOGIN_TOKEN_EXPIRY | time.Duration | 11m | login token expiry diff --git a/cmd/root.go b/cmd/root.go index 539537d..fd8bbd4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,9 +43,6 @@ func init() { // Cobra supports persistent flags, which, if defined here, // will be global for your application. RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.go-base.yaml)") - - viper.SetDefault("database_url", "postgres://postgres:postgres@localhost:5432/gobase?sslmode=disable") - RootCmd.PersistentFlags().Bool("db_debug", false, "log sql to console") viper.BindPFlag("db_debug", RootCmd.PersistentFlags().Lookup("db_debug")) diff --git a/database/postgres.go b/database/postgres.go index 8981ab8..62b19ae 100644 --- a/database/postgres.go +++ b/database/postgres.go @@ -11,13 +11,20 @@ import ( // DBConn returns a postgres connection pool. func DBConn() (*pg.DB, error) { + viper.SetDefault("db_network", "tcp") + viper.SetDefault("db_addr", "localhost:5432") + viper.SetDefault("db_user", "postgres") + viper.SetDefault("db_password", "postgres") + viper.SetDefault("db_database", "gobase") - opts, err := pg.ParseURL(viper.GetString("database_url")) - if err != nil { - return nil, err - } + db := pg.Connect(&pg.Options{ + Network: viper.GetString("db_network"), + Addr: viper.GetString("db_addr"), + User: viper.GetString("db_user"), + Password: viper.GetString("db_password"), + Database: viper.GetString("db_database"), + }) - db := pg.Connect(opts) if err := checkConn(db); err != nil { return nil, err } diff --git a/go.mod b/go.mod index ef5615f..c094087 100644 --- a/go.mod +++ b/go.mod @@ -42,3 +42,5 @@ require ( gopkg.in/mail.v2 v2.3.1 // indirect mellium.im/sasl v0.2.1 // indirect ) + +go 1.13