123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- package env
- import (
- "flag"
- "time"
- "github.com/spf13/viper"
- )
- const defaultConfigPath = "../../.env"
- type Service struct {
- appPort uint64
- appCORSAllowOrigins string
- appCORSAllowMethods string
- dbDriver string
- dbHost string
- dbPort uint64
- dbName string
- dbUser string
- dbPassword string
- dbSSLMode string
- dbMaxOpenConns uint16
- dbMaxIdleConns uint16
- dbMaxOpenConnLifetime time.Duration
- dbMaxIdleConnLifetime time.Duration
- cacheDefaultDuration time.Duration
- cacheCleanupInterval time.Duration
- smtpHost string
- smtpPort uint64
- smtpUser string
- smtpPassword string
- jwtSecretKey string
- jwtLifeTime time.Duration
- jwtCookie string
- basicAuthUser string
- basicAuthPassword string
- staticVersion uint64
- loggerStdoutEnabled bool
- loggerStdoutLevel string
- loggerStdoutAddSource bool
- loggerEmailEnabled bool
- loggerEmailLevel string
- loggerEmailAddSource bool
- loggerEmailSubject string
- loggerEmail string
- }
- func Init() (*Service, error) {
- var configPath string
- flag.StringVar(&configPath, "config", "", "Path to .env config file")
- flag.Parse()
- if configPath == "" {
- configPath = defaultConfigPath
- }
- viper.SetConfigFile(configPath)
- viper.SetConfigType("env")
- viper.AutomaticEnv()
- if err := viper.ReadInConfig(); err != nil {
- return nil, err
- }
- s := struct {
- AppPort uint64 `mapstructure:"APP_PORT"`
- AppCORSAllowOrigins string `mapstructure:"APP_CORS_ALLOW_ORIGIN"`
- AppCORSAllowMethods string `mapstructure:"APP_CORS_ALLOW_METHODS"`
- DBDriver string `mapstructure:"DB_DRIVER"`
- DBHost string `mapstructure:"DB_HOST"`
- DBPort uint64 `mapstructure:"DB_PORT"`
- DBName string `mapstructure:"DB_NAME"`
- DBUser string `mapstructure:"DB_USER"`
- DBPassword string `mapstructure:"DB_PASSWORD"`
- DBSSLMode string `mapstructure:"DB_SSL_MODE"`
- DBMaxOpenConns uint16 `mapstructure:"DB_MAX_OPEN_CONNS"`
- DBMaxIdleConns uint16 `mapstructure:"DB_MAX_IDLE_CONNS"`
- DBMaxOpenConnLifetime time.Duration `mapstructure:"DB_MAX_OPEN_CONN_LIFETIME"`
- DBMaxIdleConnLifetime time.Duration `mapstructure:"DB_MAX_IDLE_CONN_LIFETIME"`
- CacheDefaultDuration time.Duration `mapstructure:"CACHE_DEFAULT_EXPIRATION"`
- CacheCleanupInterval time.Duration `mapstructure:"CACHE_CLEANUP_INTERVAL"`
- SMTPHost string `mapstructure:"SMTP_HOST"`
- SMTPPort uint64 `mapstructure:"SMTP_PORT"`
- SMTPUser string `mapstructure:"SMTP_USER"`
- SMTPPassword string `mapstructure:"SMTP_PASSWORD"`
- JWTSecretKey string `mapstructure:"JWT_SECRET_KEY"`
- JWTLifeTime time.Duration `mapstructure:"JWT_LIFETIME"`
- JWTCookie string `mapstructure:"JWT_COOKIE"`
- BasicAuthUser string `mapstructure:"BASIC_AUTH_USER"`
- BasicAuthPassword string `mapstructure:"BASIC_AUTH_PASSWORD"`
- StaticVersion uint64 `mapstructure:"STATIC_VERSION"`
- LoggerStdoutEnabled bool `mapstructure:"LOGGER_STDOUT_ENABLED"`
- LoggerStdoutLevel string `mapstructure:"LOGGER_STDOUT_LEVEL"`
- LoggerStdoutAddSource bool `mapstructure:"LOGGER_STDOUT_ADD_SOURCE"`
- LoggerEmailEnabled bool `mapstructure:"LOGGER_EMAIL_ENABLED"`
- LoggerEmailLevel string `mapstructure:"LOGGER_EMAIL_LEVEL"`
- LoggerEmailAddSource bool `mapstructure:"LOGGER_EMAIL_ADD_SOURCE"`
- LoggerEmailSubject string `mapstructure:"LOGGER_EMAIL_SUBJECT"`
- LoggerEmail string `mapstructure:"LOGGER_EMAIL"`
- }{}
- if err := viper.Unmarshal(&s); err != nil {
- return nil, err
- }
- return &Service{
- appPort: s.AppPort,
- appCORSAllowOrigins: s.AppCORSAllowOrigins,
- appCORSAllowMethods: s.AppCORSAllowMethods,
- dbDriver: s.DBDriver,
- dbHost: s.DBHost,
- dbPort: s.DBPort,
- dbName: s.DBName,
- dbUser: s.DBUser,
- dbPassword: s.DBPassword,
- dbSSLMode: s.LoggerEmail,
- dbMaxOpenConns: s.DBMaxOpenConns,
- dbMaxIdleConns: s.DBMaxIdleConns,
- dbMaxOpenConnLifetime: s.DBMaxOpenConnLifetime,
- dbMaxIdleConnLifetime: s.DBMaxIdleConnLifetime,
- cacheDefaultDuration: s.CacheDefaultDuration,
- cacheCleanupInterval: s.CacheCleanupInterval,
- smtpHost: s.SMTPHost,
- smtpPort: s.SMTPPort,
- smtpUser: s.SMTPUser,
- smtpPassword: s.SMTPPassword,
- jwtSecretKey: s.JWTSecretKey,
- jwtLifeTime: s.JWTLifeTime,
- jwtCookie: s.JWTCookie,
- basicAuthUser: s.BasicAuthUser,
- basicAuthPassword: s.BasicAuthPassword,
- staticVersion: s.StaticVersion,
- loggerStdoutEnabled: s.LoggerStdoutEnabled,
- loggerStdoutLevel: s.LoggerStdoutLevel,
- loggerStdoutAddSource: s.LoggerStdoutAddSource,
- loggerEmailEnabled: s.LoggerEmailEnabled,
- loggerEmailLevel: s.LoggerEmailLevel,
- loggerEmailAddSource: s.LoggerEmailAddSource,
- loggerEmailSubject: s.LoggerEmailSubject,
- loggerEmail: s.LoggerEmail,
- }, nil
- }
- func (s Service) AppPort() uint64 {
- return s.appPort
- }
- func (s Service) AppCORSAllowOrigins() string {
- return s.appCORSAllowOrigins
- }
- func (s Service) AppCORSAllowMethods() string {
- return s.appCORSAllowMethods
- }
- func (s Service) DbDriver() string {
- return s.dbDriver
- }
- func (s Service) DbHost() string {
- return s.dbHost
- }
- func (s Service) DbPort() uint64 {
- return s.dbPort
- }
- func (s Service) DbName() string {
- return s.dbName
- }
- func (s Service) DbUser() string {
- return s.dbUser
- }
- func (s Service) DbPassword() string {
- return s.dbPassword
- }
- func (s Service) DbSSLMode() string {
- return s.dbSSLMode
- }
- func (s Service) DbMaxOpenConns() uint16 {
- return s.dbMaxOpenConns
- }
- func (s Service) DbMaxIdleConns() uint16 {
- return s.dbMaxIdleConns
- }
- func (s Service) DbMaxOpenConnLifetime() time.Duration {
- return s.dbMaxOpenConnLifetime
- }
- func (s Service) DbMaxIdleConnLifetime() time.Duration {
- return s.dbMaxIdleConnLifetime
- }
- func (s Service) CacheDefaultDuration() time.Duration {
- return s.cacheDefaultDuration
- }
- func (s Service) CacheCleanupInterval() time.Duration {
- return s.cacheCleanupInterval
- }
- func (s Service) SmtpHost() string {
- return s.smtpHost
- }
- func (s Service) SmtpPort() uint64 {
- return s.smtpPort
- }
- func (s Service) SmtpUser() string {
- return s.smtpUser
- }
- func (s Service) SmtpPassword() string {
- return s.smtpPassword
- }
- func (s Service) JwtSecretKey() string {
- return s.jwtSecretKey
- }
- func (s Service) JwtLifeTime() time.Duration {
- return s.jwtLifeTime
- }
- func (s Service) JwtCookie() string {
- return s.jwtCookie
- }
- func (s Service) BasicAuthUser() string {
- return s.basicAuthUser
- }
- func (s Service) BasicAuthPassword() string {
- return s.basicAuthPassword
- }
- func (s Service) StaticVersion() uint64 {
- return s.staticVersion
- }
- func (s Service) LoggerStdoutEnabled() bool {
- return s.loggerStdoutEnabled
- }
- func (s Service) LoggerStdoutLevel() string {
- return s.loggerStdoutLevel
- }
- func (s Service) LoggerStdoutAddSource() bool {
- return s.loggerStdoutAddSource
- }
- func (s Service) LoggerEmailEnabled() bool {
- return s.loggerEmailEnabled
- }
- func (s Service) LoggerEmailLevel() string {
- return s.loggerEmailLevel
- }
- func (s Service) LoggerEmailAddSource() bool {
- return s.loggerEmailAddSource
- }
- func (s Service) LoggerEmailSubject() string {
- return s.loggerEmailSubject
- }
- func (s Service) LoggerEmail() string {
- return s.loggerEmail
- }
|