123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- package config
- import (
- "flag"
- "time"
- "github.com/spf13/viper"
- )
- const defaultConfigPath = "./.env"
- type Service struct {
- appPort uint16
- corsAllowOrigins string
- corsAllowMethods string
- dbDriver string
- dbHost string
- dbPort uint16
- 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 uint16
- smtpUser string
- smtpPassword string
- jwtSecretKey string
- jwtLifeTime time.Duration
- jwtCookie string
- basicAuthUser string
- basicAuthPassword string
- staticVersion uint16
- loggerStdoutEnabled bool
- loggerStdoutLevel string
- loggerStdoutAddSource bool
- loggerEmailEnabled bool
- loggerEmailLevel string
- loggerEmailAddSource bool
- loggerEmailSubject string
- loggerEmailRecipient string
- loginRateLimiterMaxRequests uint16
- loginRateLimiterExpiration time.Duration
- }
- 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 uint16 `mapstructure:"APP_PORT"`
- CorsAllowOriginsOrigins string `mapstructure:"CORS_ALLOW_ORIGIN"`
- CorsAllowMethods string `mapstructure:"CORS_ALLOW_METHODS"`
- DBDriver string `mapstructure:"DB_DRIVER"`
- DBHost string `mapstructure:"DB_HOST"`
- DBPort uint16 `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 uint16 `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 uint16 `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"`
- LoggerEmailRecipient string `mapstructure:"LOGGER_EMAIL_RECIPIENT"`
- LoginRateLimiterMaxRequests uint16 `mapstructure:"LOGIN_RATE_LIMITER_MAX_REQUESTS"`
- LoginRateLimiterExpiration time.Duration `mapstructure:"LOGIN_RATE_LIMITER_EXPIRATION"`
- }{}
- if err := viper.Unmarshal(&s); err != nil {
- return nil, err
- }
- return &Service{
- appPort: s.AppPort,
- corsAllowOrigins: s.CorsAllowOriginsOrigins,
- corsAllowMethods: s.CorsAllowMethods,
- dbDriver: s.DBDriver,
- dbHost: s.DBHost,
- dbPort: s.DBPort,
- dbName: s.DBName,
- dbUser: s.DBUser,
- dbPassword: s.DBPassword,
- dbSSLMode: s.DBSSLMode,
- 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,
- loggerEmailRecipient: s.LoggerEmailRecipient,
- loginRateLimiterMaxRequests: s.LoginRateLimiterMaxRequests,
- loginRateLimiterExpiration: s.LoginRateLimiterExpiration,
- }, nil
- }
- func (s Service) AppPort() uint16 {
- return s.appPort
- }
- func (s Service) CORSAllowOrigins() string {
- return s.corsAllowOrigins
- }
- func (s Service) CORSAllowMethods() string {
- return s.corsAllowMethods
- }
- func (s Service) DBDriver() string {
- return s.dbDriver
- }
- func (s Service) DBHost() string {
- return s.dbHost
- }
- func (s Service) DBPort() uint16 {
- 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() uint16 {
- 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() uint16 {
- 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) LoggerEmailRecipient() string {
- return s.loggerEmailRecipient
- }
- func (s Service) LoginRateLimiterMaxRequests() uint16 {
- return s.loginRateLimiterMaxRequests
- }
- func (s Service) LoginRateLimiterExpiration() time.Duration {
- return s.loginRateLimiterExpiration
- }
|