initial POC
This commit is contained in:
35
internal/config/config.go
Normal file
35
internal/config/config.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"git.asperti.com/paspo/mail-autoconfig/internal/myerrors"
|
||||
"github.com/gookit/validate"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
HttpPort int `mapstructure:"PORT" validate:"required|int|min:1|max:65535"`
|
||||
DBPath string `mapstructure:"DB_PATH" validate:"required"`
|
||||
}
|
||||
|
||||
var AppConfig Config
|
||||
|
||||
func InitConfig() error {
|
||||
var err error
|
||||
viper.SetConfigType("env")
|
||||
viper.AllowEmptyEnv(true)
|
||||
viper.AutomaticEnv()
|
||||
|
||||
viper.SetDefault("PORT", 9000)
|
||||
viper.SetDefault("DB_PATH", "./database.sqlite")
|
||||
|
||||
err = viper.Unmarshal(&AppConfig)
|
||||
if err != nil {
|
||||
return myerrors.ErrConfigParse
|
||||
}
|
||||
|
||||
validation := validate.Struct(AppConfig)
|
||||
if !validation.Validate() {
|
||||
return myerrors.ErrConfigValidation
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user