123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package env
- import (
- "flag"
- "github.com/spf13/viper"
- )
- const defaultConfigPath = "../../.env"
- type Service struct {
- appPort string
- dbHost string
- dbPort string
- dbName string
- dbUser string
- dbPassword string
- dbMaxOpenConns int
- dbMaxIdleConns int
- dbMaxConnLifetime int
- dbMaxIdleConnLifetime int
- corsAllowOrigins string
- corsAllowMethods string
- jwtSecretKey string
- jwtLifeTime int
- basicAuthUser string
- basicAuthPassword string
- smtpHost string
- smtpPort string
- smtpUser string
- smtpPassword string
- errorsEmail 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 string `mapstructure:"APP_PORT"`
- DBHost string `mapstructure:"DB_HOST"`
- DBPort string `mapstructure:"DB_PORT"`
- DBName string `mapstructure:"DB_NAME"`
- DBUser string `mapstructure:"DB_USER"`
- DBPassword string `mapstructure:"DB_PASSWORD"`
- DBMaxOpenConns int `mapstructure:"DB_MAX_OPEN_CONNS"`
- DBMaxIdleConns int `mapstructure:"DB_MAX_IDLE_CONNS"`
- DBMaxConnLifetime int `mapstructure:"DB_MAX_CONN_LIFETIME"`
- DBMaxIdleConnLifetime int `mapstructure:"DB_MAX_IDLE_CONN_LIFETIME"`
- CORSAllowOrigins string `mapstructure:"CORS_ALLOW_ORIGING"`
- CORSAllowMethods string `mapstructure:"CORS_ALLOW_METHODS"`
- JWTSecretKey string `mapstructure:"JWT_SECRET_KEY"`
- JWTLifeTime int `mapstructure:"JWT_LIFETIME"`
- BasicAuthUser string `mapstructure:"BASIC_AUTH_USER"`
- BasicAuthPassword string `mapstructure:"BASIC_AUTH_PASSWORD"`
- SMTPHost string `mapstructure:"SMTP_HOST"`
- SMTPPort string `mapstructure:"SMTP_PORT"`
- SMTPUser string `mapstructure:"SMTP_USER"`
- SMTPPassword string `mapstructure:"SMTP_PASSWORD"`
- ErrorsEmail string `mapstructure:"ERRORS_EMAIL"`
- }{}
- if err := viper.Unmarshal(&s); err != nil {
- return nil, err
- }
- return &Service{
- appPort: s.AppPort,
- dbHost: s.DBHost,
- dbPort: s.DBPort,
- dbName: s.DBName,
- dbUser: s.DBUser,
- dbPassword: s.DBPassword,
- dbMaxOpenConns: s.DBMaxOpenConns,
- dbMaxIdleConns: s.DBMaxIdleConns,
- dbMaxConnLifetime: s.DBMaxConnLifetime,
- dbMaxIdleConnLifetime: s.DBMaxIdleConnLifetime,
- corsAllowOrigins: s.CORSAllowOrigins,
- corsAllowMethods: s.CORSAllowMethods,
- jwtSecretKey: s.JWTSecretKey,
- jwtLifeTime: s.JWTLifeTime,
- basicAuthUser: s.BasicAuthUser,
- basicAuthPassword: s.BasicAuthPassword,
- smtpHost: s.SMTPHost,
- smtpPort: s.SMTPPort,
- smtpUser: s.SMTPUser,
- smtpPassword: s.SMTPPassword,
- errorsEmail: s.ErrorsEmail,
- }, nil
- }
- func (e *Service) AppPort() string {
- return e.appPort
- }
- func (e *Service) DBHost() string {
- return e.dbHost
- }
- func (e *Service) DBPort() string {
- return e.dbPort
- }
- func (e *Service) DBName() string {
- return e.dbName
- }
- func (e *Service) DBUser() string {
- return e.dbUser
- }
- func (e *Service) DBPassword() string {
- return e.dbPassword
- }
- func (e *Service) CORSAllowOrigins() string {
- return e.corsAllowOrigins
- }
- func (e *Service) CORSAllowMethods() string {
- return e.corsAllowMethods
- }
- func (e *Service) DBMaxOpenConns() int {
- return e.dbMaxOpenConns
- }
- func (e *Service) DBMaxIdleConns() int {
- return e.dbMaxIdleConns
- }
- func (e *Service) DBMaxConnLifetime() int {
- return e.dbMaxConnLifetime
- }
- func (e *Service) DBMaxIdleConnLifetime() int {
- return e.dbMaxIdleConnLifetime
- }
- func (e *Service) SMTPHost() string {
- return e.smtpHost
- }
- func (e *Service) SMTPPort() string {
- return e.smtpPort
- }
- func (e *Service) SMTPUser() string {
- return e.smtpUser
- }
- func (e *Service) SMTPPassword() string {
- return e.smtpPassword
- }
- func (e *Service) JWTSecretKey() string {
- return e.jwtSecretKey
- }
- func (e *Service) JWTLifetime() int {
- return e.jwtLifeTime
- }
- func (e *Service) ErrorsEmail() string {
- return e.errorsEmail
- }
- func (e *Service) BasicAuthUser() string {
- return e.basicAuthUser
- }
- func (e *Service) BasicAuthPassword() string {
- return e.basicAuthPassword
- }
|