()
| 28 | } |
| 29 | |
| 30 | func Load() (*Config, error) { |
| 31 | err := godotenv.Load() |
| 32 | if err != nil { |
| 33 | log.Printf("[Warning] Failed to load env file, if you're using 'Docker' and you set 'environment' or 'env_file' variable, don't worry, everything is fine. Error: %v", err) |
| 34 | } |
| 35 | |
| 36 | cfg := &Config{ |
| 37 | ServicePort: GetEnvAsInt("SERVICE_PORT", 62050), |
| 38 | XrayExecutablePath: GetEnv("XRAY_EXECUTABLE_PATH", "/usr/local/bin/xray"), |
| 39 | XrayAssetsPath: GetEnv("XRAY_ASSETS_PATH", "/usr/local/share/xray"), |
| 40 | SslCertFile: GetEnv("SSL_CERT_FILE", "/var/lib/pg-node/certs/ssl_cert.pem"), |
| 41 | SslKeyFile: GetEnv("SSL_KEY_FILE", "/var/lib/pg-node/certs/ssl_key.pem"), |
| 42 | GeneratedConfigPath: GetEnv("GENERATED_CONFIG_PATH", "/var/lib/pg-node/generated/"), |
| 43 | ServiceProtocol: GetEnv("SERVICE_PROTOCOL", "grpc"), |
| 44 | Debug: GetEnvAsBool("DEBUG", false), |
| 45 | LogBufferSize: GetEnvAsInt("LOG_BUFFER_SIZE", 10000), |
| 46 | StartupLogTailSize: GetEnvAsInt("STARTUP_LOG_TAIL_SIZE", 200), |
| 47 | StatsUpdateIntervalSeconds: GetEnvAsInt("STATS_UPDATE_INTERVAL_SECONDS", 10), |
| 48 | StatsCleanupIntervalSeconds: GetEnvAsInt("STATS_CLEANUP_INTERVAL_SECONDS", 300), |
| 49 | } |
| 50 | |
| 51 | if cfg.LogBufferSize <= 0 { |
| 52 | log.Printf("[Warning] LOG_BUFFER_SIZE must be greater than 0, got %d. Falling back to 1.", cfg.LogBufferSize) |
| 53 | cfg.LogBufferSize = 1 |
| 54 | } |
| 55 | |
| 56 | cfg.ApiKey, err = GetEnvAsUUID("API_KEY") |
| 57 | if err != nil { |
| 58 | log.Printf("[Error] Failed to load API Key, error: %v", err) |
| 59 | } |
| 60 | |
| 61 | nodeHostStr := GetEnv("NODE_HOST", "0.0.0.0") |
| 62 | ipPattern := `^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$` |
| 63 | re := regexp.MustCompile(ipPattern) |
| 64 | |
| 65 | if re.MatchString(nodeHostStr) { |
| 66 | cfg.NodeHost = nodeHostStr |
| 67 | } else { |
| 68 | log.Println(nodeHostStr, " is not a valid IP address.\n NODE_HOST will be set to 127.0.0.1") |
| 69 | cfg.NodeHost = "127.0.0.1" |
| 70 | } |
| 71 | |
| 72 | return cfg, nil |
| 73 | } |
| 74 | |
| 75 | // NewTestConfig creates a config for testing |
| 76 | func NewTestConfig(generatedConfigPath string, key uuid.UUID) *Config { |
no test coverage detected