split DB connection string into separate env vars

This commit is contained in:
dhax 2020-01-30 14:59:00 +01:00
parent c6b8aff2a9
commit 069d655d9c
4 changed files with 19 additions and 9 deletions

View file

@ -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<br>@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

View file

@ -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"))

View file

@ -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
}

2
go.mod
View file

@ -42,3 +42,5 @@ require (
gopkg.in/mail.v2 v2.3.1 // indirect
mellium.im/sasl v0.2.1 // indirect
)
go 1.13