You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.1 KiB
67 lines
2.1 KiB
package config |
|
|
|
import ( |
|
"os" |
|
"strconv" |
|
"time" |
|
) |
|
|
|
type Config struct { |
|
ServerPort int `env:"SERVER_PORT" envDefault:"3000"` |
|
JWTSecret string `env:"JWT_SECRET" envDefault:"your-secret-key"` |
|
DataDirectory string `env:"DATA_DIRECTORY" envDefault:"./data"` |
|
LogLevel string `env:"LOG_LEVEL" envDefault:"info"` |
|
SchedulerInterval time.Duration `env:"SCHEDULER_INTERVAL" envDefault:"1m"` |
|
ReadTimeout time.Duration `env:"READ_TIMEOUT" envDefault:"30s"` |
|
WriteTimeout time.Duration `env:"WRITE_TIMEOUT" envDefault:"30s"` |
|
ShutdownTimeout time.Duration `env:"SHUTDOWN_TIMEOUT" envDefault:"30s"` |
|
} |
|
|
|
func Load() (*Config, error) { |
|
cfg := &Config{ |
|
ServerPort: getEnvAsInt("SERVER_PORT", 3000), |
|
JWTSecret: getEnvAsString("JWT_SECRET", "your-secret-key"), |
|
DataDirectory: getEnvAsString("DATA_DIRECTORY", "./data"), |
|
LogLevel: getEnvAsString("LOG_LEVEL", "info"), |
|
SchedulerInterval: getEnvAsDuration("SCHEDULER_INTERVAL", "1m"), |
|
ReadTimeout: getEnvAsDuration("READ_TIMEOUT", "30s"), |
|
WriteTimeout: getEnvAsDuration("WRITE_TIMEOUT", "30s"), |
|
ShutdownTimeout: getEnvAsDuration("SHUTDOWN_TIMEOUT", "30s"), |
|
} |
|
|
|
return cfg, nil |
|
} |
|
|
|
func (c *Config) Validate() error { |
|
// For now, we'll keep it simple and not add validation |
|
// In a real application, you would validate the configuration here |
|
return nil |
|
} |
|
|
|
func getEnvAsString(key, defaultValue string) string { |
|
if value, exists := os.LookupEnv(key); exists { |
|
return value |
|
} |
|
return defaultValue |
|
} |
|
|
|
func getEnvAsInt(key string, defaultValue int) int { |
|
if value, exists := os.LookupEnv(key); exists { |
|
if intValue, err := strconv.Atoi(value); err == nil { |
|
return intValue |
|
} |
|
} |
|
return defaultValue |
|
} |
|
|
|
func getEnvAsDuration(key string, defaultValue string) time.Duration { |
|
if value, exists := os.LookupEnv(key); exists { |
|
if duration, err := time.ParseDuration(value); err == nil { |
|
return duration |
|
} |
|
} |
|
if duration, err := time.ParseDuration(defaultValue); err == nil { |
|
return duration |
|
} |
|
return time.Minute // Default to 1 minute if parsing fails |
|
}
|
|
|