LoadConfig loads configuration from file with environment variable overrides
(filename string)
| 40 | |
| 41 | // LoadConfig loads configuration from file with environment variable overrides |
| 42 | func LoadConfig(filename string) (*Config, error) { |
| 43 | // Set defaults |
| 44 | cfg := &Config{ |
| 45 | Server: ServerConfig{ |
| 46 | Port: 50443, |
| 47 | Insecure: false, |
| 48 | }, |
| 49 | Logging: LoggingConfig{ |
| 50 | Level: "info", |
| 51 | JSONFormat: false, |
| 52 | }, |
| 53 | Timeouts: TimeoutConfig{ |
| 54 | HTTPTimeout: 30, |
| 55 | TaskTimeout: 300, |
| 56 | }, |
| 57 | } |
| 58 | |
| 59 | // Load from file if exists |
| 60 | if filename != "" { |
| 61 | data, err := os.ReadFile(filename) |
| 62 | if err != nil { |
| 63 | if !os.IsNotExist(err) { |
| 64 | return nil, fmt.Errorf("failed to read config file: %w", err) |
| 65 | } |
| 66 | // File doesn't exist, use defaults + env vars |
| 67 | } else { |
| 68 | if err := yaml.Unmarshal(data, cfg); err != nil { |
| 69 | return nil, fmt.Errorf("failed to parse config file: %w", err) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Override with environment variables |
| 75 | if host := os.Getenv("CS_HOST"); host != "" { |
| 76 | cfg.Server.Host = host |
| 77 | } |
| 78 | if port := os.Getenv("CS_PORT"); port != "" { |
| 79 | fmt.Sscanf(port, "%d", &cfg.Server.Port) |
| 80 | } |
| 81 | if username := os.Getenv("CS_USERNAME"); username != "" { |
| 82 | cfg.Server.Username = username |
| 83 | } |
| 84 | if password := os.Getenv("CS_PASSWORD"); password != "" { |
| 85 | cfg.Server.Password = password |
| 86 | } |
| 87 | if insecure := os.Getenv("CS_INSECURE"); insecure != "" { |
| 88 | cfg.Server.Insecure = strings.ToLower(insecure) == "true" |
| 89 | } |
| 90 | // Support both CS_PROXY and standard HTTP_PROXY/HTTPS_PROXY |
| 91 | if proxy := os.Getenv("CS_PROXY"); proxy != "" { |
| 92 | cfg.Server.Proxy = proxy |
| 93 | } else if proxy := os.Getenv("HTTPS_PROXY"); proxy != "" { |
| 94 | cfg.Server.Proxy = proxy |
| 95 | } else if proxy := os.Getenv("HTTP_PROXY"); proxy != "" { |
| 96 | cfg.Server.Proxy = proxy |
| 97 | } |
| 98 | if level := os.Getenv("CS_LOG_LEVEL"); level != "" { |
| 99 | cfg.Logging.Level = level |