package config import ( "errors" "os" "strconv" "time" "github.com/joho/godotenv" ) type DbConfig struct { DBUser string DBPass string DBHost string DBPort string DBName string } type AuthConfig struct { UserAuthEnabled bool JWTSecret string TokenExpiry time.Duration } type LoginConfig struct { LoginAPI string AuthMeAPI string LogoutAPI string SkipSSLVerification bool } type TokenConfig struct { PassiveAssetIssuer string JwtSignatureKey []byte } type ApiConfig struct { ApiPort string } type TokenApi struct { TokenApiKey string } type Config struct { DbConfig TokenConfig TokenApi ApiConfig LoginConfig AuthConfig } func (c *Config) readConfig() error { if err := godotenv.Load(); err != nil { return errors.New("failed to load environment variables") } c.DbConfig = DbConfig{ DBHost: os.Getenv("DB_HOST"), DBPort: os.Getenv("DB_PORT"), DBName: os.Getenv("DB_NAME"), DBUser: os.Getenv("DB_USER"), DBPass: os.Getenv("DB_PASSWORD"), } c.ApiConfig = ApiConfig{ ApiPort: os.Getenv("API_PORT"), } c.TokenConfig = TokenConfig{ PassiveAssetIssuer: os.Getenv("PASSIVE_ASSET_ISSUER"), JwtSignatureKey: []byte(os.Getenv("TOKEN_KEY")), } c.TokenApi = TokenApi{ TokenApiKey: os.Getenv("TOKEN_API_KEY"), } skipSSL, err := strconv.ParseBool(os.Getenv("SKIP_SSL_VERIFICATION")) if err != nil { return errors.New("failed to read SKIP_SSL_VERIFICATION environment variable") } c.LoginConfig = LoginConfig{ LoginAPI: os.Getenv("API_LOGIN_URL"), AuthMeAPI: os.Getenv("API_ME_URL"), LogoutAPI: os.Getenv("API_LOGOUT_URL"), SkipSSLVerification: skipSSL, } userAuthEnabled := true // Default to true for security if authEnabledStr := os.Getenv("USER_AUTH_ENABLED"); authEnabledStr != "" { var err error userAuthEnabled, err = strconv.ParseBool(authEnabledStr) if err != nil { return errors.New("invalid USER_AUTH_ENABLED value, must be true or false") } } // Parse token expiry (default to 24 hours) tokenExpiry := 24 * time.Hour if expiryStr := os.Getenv("JWT_TOKEN_EXPIRY_HOURS"); expiryStr != "" { if hours, err := strconv.Atoi(expiryStr); err == nil { tokenExpiry = time.Duration(hours) * time.Hour } } c.AuthConfig = AuthConfig{ UserAuthEnabled: userAuthEnabled, JWTSecret: os.Getenv("JWT_SECRET"), TokenExpiry: tokenExpiry, } if c.ApiConfig.ApiPort == "" { return errors.New("failed to read environment variables") } return nil } func NewConfig() (*Config, error) { config := &Config{} err := config.readConfig() if err != nil { return nil, err } return config, nil }