change default viper config file to ./dev.env, fixes #2, #13

This commit is contained in:
dhax 2024-09-20 02:07:12 +02:00
parent 8c140ba3fa
commit 359d35fdd2
5 changed files with 25 additions and 21 deletions

View file

@ -83,18 +83,14 @@ Package auth/pwdless contains example api tests using a mocked database. Run the
### Environment Variables ### Environment Variables
By default viper will look at $HOME/.go-base.yaml for a config file. Setting your config as Environment Variables is recommended as by 12-Factor App. By default viper will look at ./dev.env for a config file. Setting your config as Environment Variables is recommended as by 12-Factor App.
| Name | Type | Default | Description | | Name | Type | Default | Description |
| ----------------------- | ------------- | --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ----------------------- | ------------- | --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| PORT | string | localhost:3000 | http address (accepts also port number only for heroku compability) | | PORT | string | localhost:3000 | http address (accepts also port number only for heroku compability) |
| LOG_LEVEL | string | debug | log level | | LOG_LEVEL | string | debug | log level |
| LOG_TEXTLOGGING | bool | false | defaults to json logging | | LOG_TEXTLOGGING | bool | false | defaults to json logging |
| DB_NETWORK | string | tcp | database 'tcp' or 'unix' connection | | DB_DSN | string | postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable | database dsn connection string |
| 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 | postgres | database shema name |
| AUTH_LOGIN_URL | string | <http://localhost:3000/login> | client login url as sent in login token email | | 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_LENGTH | int | 8 | length of login token |
| AUTH_LOGIN_TOKEN_EXPIRY | time.Duration | 11m | login token expiry | | AUTH_LOGIN_TOKEN_EXPIRY | time.Duration | 11m | login token expiry |

View file

@ -2,10 +2,8 @@ package cmd
import ( import (
"fmt" "fmt"
"log"
"os" "os"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -37,7 +35,7 @@ func init() {
// Here you will define your flags and configuration settings. // Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here, // Cobra supports persistent flags, which, if defined here,
// will be global for your application. // will be global for your application.
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.go-base.yaml)") RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./dev.env")
RootCmd.PersistentFlags().Bool("db_debug", false, "log sql to console") RootCmd.PersistentFlags().Bool("db_debug", false, "log sql to console")
viper.BindPFlag("db_debug", RootCmd.PersistentFlags().Lookup("db_debug")) viper.BindPFlag("db_debug", RootCmd.PersistentFlags().Lookup("db_debug"))
@ -52,15 +50,9 @@ func initConfig() {
// Use config file from the flag. // Use config file from the flag.
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
} else { } else {
// Find home directory. viper.AddConfigPath(".")
home, err := homedir.Dir() viper.SetConfigName("dev")
if err != nil { viper.SetConfigType("env")
log.Fatal(err)
}
// Search config in home directory with name ".go-base" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".go-base")
} }
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match

19
dev.env Normal file
View file

@ -0,0 +1,19 @@
PORT=3000
LOG_LEVEL=DEBUG
DB_DSN=postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable
AUTH_LOGIN_URL=http://localhost:3000/login
AUTH_LOGIN_TOKEN_LENGTH=8
AUTH_LOGIN_TOKEN_EXPIRY=11m
AUTH_JWT_EXPIRY=15m
AUTH_JWT_REFRESH_EXPIRY=1h
EMAIL_SMTP_HOST=
EMAIL_SMTP_PORT=
EMAIL_SMTP_USER=
EMAIL_SMTP_PASSWORD=
EMAIL_FROM_ADDRESS=
EMAIL_FROM_NAME=
ENABLE_CORS=false

1
go.mod
View file

@ -15,7 +15,6 @@ require (
github.com/gofrs/uuid v4.4.0+incompatible github.com/gofrs/uuid v4.4.0+incompatible
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
github.com/lestrrat-go/jwx/v2 v2.0.13 github.com/lestrrat-go/jwx/v2 v2.0.13
github.com/mitchellh/go-homedir v1.1.0
github.com/mssola/user_agent v0.6.0 github.com/mssola/user_agent v0.6.0
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0 github.com/spf13/cobra v1.7.0

2
go.sum
View file

@ -200,8 +200,6 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4= github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4=