|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/nethesis/matrix2acrobits/logger" |
| 11 | + "maunium.net/go/mautrix/id" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + defaultPort = "8080" |
| 16 | + defaultCacheTTLSeconds = 3600 |
| 17 | + defaultPushTokenDBPath = "/tmp/push_tokens.db" |
| 18 | + defaultExtAuthTimeoutS = 5 |
| 19 | + defaultLogLevel = "INFO" |
| 20 | +) |
| 21 | + |
| 22 | +// Config holds all configuration loaded from environment variables |
| 23 | +type Config struct { |
| 24 | + // Server configuration |
| 25 | + ProxyPort string |
| 26 | + LogLevel string |
| 27 | + |
| 28 | + // Matrix configuration |
| 29 | + MatrixHomeserverURL string |
| 30 | + MatrixAsToken string |
| 31 | + MatrixAsUserID id.UserID |
| 32 | + MatrixHomeserverHost string |
| 33 | + |
| 34 | + // Push tokens database |
| 35 | + PushTokenDBPath string |
| 36 | + |
| 37 | + // Proxy configuration for push registration |
| 38 | + ProxyURL string |
| 39 | + |
| 40 | + // Message service configuration |
| 41 | + CacheTTLSeconds int |
| 42 | + CacheTTL time.Duration |
| 43 | + |
| 44 | + // External authentication configuration |
| 45 | + ExtAuthURL string |
| 46 | + ExtAuthTimeoutS int |
| 47 | + ExtAuthTimeout time.Duration |
| 48 | + |
| 49 | + // Mapping file |
| 50 | + MappingFile string |
| 51 | +} |
| 52 | + |
| 53 | +// NewConfig loads all configuration from environment variables with validation |
| 54 | +func NewConfig() (*Config, error) { |
| 55 | + cfg := &Config{} |
| 56 | + |
| 57 | + logger.Debug().Msg("starting configuration loading from environment variables") |
| 58 | + |
| 59 | + // Load server configuration |
| 60 | + cfg.LogLevel = os.Getenv("LOGLEVEL") |
| 61 | + if cfg.LogLevel == "" { |
| 62 | + cfg.LogLevel = defaultLogLevel |
| 63 | + logger.Debug().Str("LOGLEVEL", cfg.LogLevel).Msg("using default log level") |
| 64 | + } else { |
| 65 | + logger.Debug().Str("LOGLEVEL", cfg.LogLevel).Msg("log level loaded from environment") |
| 66 | + } |
| 67 | + |
| 68 | + cfg.ProxyPort = os.Getenv("PROXY_PORT") |
| 69 | + if cfg.ProxyPort == "" { |
| 70 | + cfg.ProxyPort = defaultPort |
| 71 | + logger.Debug().Str("PROXY_PORT", cfg.ProxyPort).Msg("using default proxy port") |
| 72 | + } else { |
| 73 | + logger.Debug().Str("PROXY_PORT", cfg.ProxyPort).Msg("proxy port loaded from environment") |
| 74 | + } |
| 75 | + |
| 76 | + // Load Matrix configuration (required) |
| 77 | + cfg.MatrixHomeserverURL = os.Getenv("MATRIX_HOMESERVER_URL") |
| 78 | + if cfg.MatrixHomeserverURL == "" { |
| 79 | + logger.Error().Msg("MATRIX_HOMESERVER_URL environment variable is missing") |
| 80 | + return nil, fmt.Errorf("MATRIX_HOMESERVER_URL is required") |
| 81 | + } |
| 82 | + logger.Debug().Str("MATRIX_HOMESERVER_URL", cfg.MatrixHomeserverURL).Msg("matrix homeserver URL loaded from environment") |
| 83 | + |
| 84 | + cfg.MatrixAsToken = os.Getenv("SUPER_ADMIN_TOKEN") |
| 85 | + if cfg.MatrixAsToken == "" { |
| 86 | + logger.Error().Msg("SUPER_ADMIN_TOKEN environment variable is missing") |
| 87 | + return nil, fmt.Errorf("SUPER_ADMIN_TOKEN is required (must be the Application Service as_token)") |
| 88 | + } |
| 89 | + logger.Debug().Msg("SUPER_ADMIN_TOKEN loaded from environment") |
| 90 | + |
| 91 | + asUserIDStr := os.Getenv("AS_USER_ID") |
| 92 | + if asUserIDStr == "" { |
| 93 | + logger.Error().Msg("AS_USER_ID environment variable is missing") |
| 94 | + return nil, fmt.Errorf("AS_USER_ID is required (e.g., '@_acrobits_proxy:your.server.com')") |
| 95 | + } |
| 96 | + cfg.MatrixAsUserID = id.UserID(asUserIDStr) |
| 97 | + logger.Debug().Str("AS_USER_ID", asUserIDStr).Msg("application service user ID loaded from environment") |
| 98 | + |
| 99 | + // Derive homeserver host from MATRIX_HOMESERVER_URL |
| 100 | + if u, err := url.Parse(cfg.MatrixHomeserverURL); err == nil { |
| 101 | + cfg.MatrixHomeserverHost = u.Hostname() |
| 102 | + logger.Debug().Str("homeserver_host", cfg.MatrixHomeserverHost).Msg("extracted homeserver host from URL") |
| 103 | + } else { |
| 104 | + logger.Warn().Err(err).Str("MATRIX_HOMESERVER_URL", cfg.MatrixHomeserverURL).Msg("failed to parse homeserver URL") |
| 105 | + } |
| 106 | + |
| 107 | + // Load push tokens database configuration |
| 108 | + cfg.PushTokenDBPath = os.Getenv("PUSH_TOKEN_DB_PATH") |
| 109 | + if cfg.PushTokenDBPath == "" { |
| 110 | + cfg.PushTokenDBPath = defaultPushTokenDBPath |
| 111 | + logger.Debug().Str("PUSH_TOKEN_DB_PATH", cfg.PushTokenDBPath).Msg("using default push token database path") |
| 112 | + } else { |
| 113 | + logger.Debug().Str("PUSH_TOKEN_DB_PATH", cfg.PushTokenDBPath).Msg("push token database path loaded from environment") |
| 114 | + } |
| 115 | + |
| 116 | + // Load proxy configuration |
| 117 | + cfg.ProxyURL = os.Getenv("PROXY_URL") |
| 118 | + if cfg.ProxyURL == "" { |
| 119 | + cfg.ProxyURL = cfg.MatrixHomeserverURL |
| 120 | + logger.Info().Str("PROXY_URL", cfg.ProxyURL).Msg("PROXY_URL not configured, assuming same as MATRIX_HOMESERVER_URL") |
| 121 | + } else { |
| 122 | + logger.Debug().Str("PROXY_URL", cfg.ProxyURL).Msg("proxy URL loaded from environment") |
| 123 | + } |
| 124 | + |
| 125 | + // Load cache configuration |
| 126 | + cacheTTLStr := os.Getenv("CACHE_TTL_SECONDS") |
| 127 | + cfg.CacheTTLSeconds = defaultCacheTTLSeconds |
| 128 | + if cacheTTLStr != "" { |
| 129 | + if parsed, err := strconv.Atoi(cacheTTLStr); err == nil && parsed > 0 { |
| 130 | + cfg.CacheTTLSeconds = parsed |
| 131 | + logger.Debug().Int("CACHE_TTL_SECONDS", cfg.CacheTTLSeconds).Msg("cache TTL loaded from environment") |
| 132 | + } else { |
| 133 | + logger.Warn().Str("CACHE_TTL_SECONDS", cacheTTLStr).Err(err).Int("default", defaultCacheTTLSeconds).Msg("invalid cache TTL value, using default") |
| 134 | + } |
| 135 | + } else { |
| 136 | + logger.Debug().Int("CACHE_TTL_SECONDS", cfg.CacheTTLSeconds).Msg("using default cache TTL") |
| 137 | + } |
| 138 | + cfg.CacheTTL = time.Duration(cfg.CacheTTLSeconds) * time.Second |
| 139 | + |
| 140 | + // Load external authentication configuration |
| 141 | + cfg.ExtAuthURL = os.Getenv("EXT_AUTH_URL") |
| 142 | + if cfg.ExtAuthURL == "" { |
| 143 | + logger.Warn().Msg("EXT_AUTH_URL not set - external authentication will not be available") |
| 144 | + } else { |
| 145 | + logger.Debug().Str("EXT_AUTH_URL", cfg.ExtAuthURL).Msg("external authentication URL loaded from environment") |
| 146 | + } |
| 147 | + |
| 148 | + cfg.ExtAuthTimeoutS = defaultExtAuthTimeoutS |
| 149 | + if v := os.Getenv("EXT_AUTH_TIMEOUT_S"); v != "" { |
| 150 | + if parsed, err := strconv.Atoi(v); err == nil && parsed > 0 { |
| 151 | + cfg.ExtAuthTimeoutS = parsed |
| 152 | + logger.Debug().Int("EXT_AUTH_TIMEOUT_S", cfg.ExtAuthTimeoutS).Msg("external auth timeout loaded from environment") |
| 153 | + } else { |
| 154 | + logger.Warn().Str("EXT_AUTH_TIMEOUT_S", v).Err(err).Int("default", defaultExtAuthTimeoutS).Msg("invalid external auth timeout value, using default") |
| 155 | + } |
| 156 | + } else { |
| 157 | + logger.Debug().Int("EXT_AUTH_TIMEOUT_S", cfg.ExtAuthTimeoutS).Msg("using default external auth timeout") |
| 158 | + } |
| 159 | + cfg.ExtAuthTimeout = time.Duration(cfg.ExtAuthTimeoutS) * time.Second |
| 160 | + |
| 161 | + // Load mapping file configuration |
| 162 | + cfg.MappingFile = os.Getenv("MAPPING_FILE") |
| 163 | + if cfg.MappingFile != "" { |
| 164 | + logger.Debug().Str("MAPPING_FILE", cfg.MappingFile).Msg("mapping file path loaded from environment") |
| 165 | + } else { |
| 166 | + logger.Debug().Msg("MAPPING_FILE not set - no mapping file will be loaded at startup") |
| 167 | + } |
| 168 | + |
| 169 | + logger.Debug().Msg("configuration loading completed successfully") |
| 170 | + |
| 171 | + return cfg, nil |
| 172 | +} |
| 173 | + |
| 174 | +// NewTestConfig creates a minimal Config for testing purposes |
| 175 | +func NewTestConfig() *Config { |
| 176 | + return &Config{ |
| 177 | + ProxyPort: defaultPort, |
| 178 | + LogLevel: defaultLogLevel, |
| 179 | + MatrixHomeserverURL: "https://example.com", |
| 180 | + MatrixAsToken: "test_token", |
| 181 | + MatrixAsUserID: "@test:example.com", |
| 182 | + MatrixHomeserverHost: "example.com", |
| 183 | + PushTokenDBPath: defaultPushTokenDBPath, |
| 184 | + ProxyURL: "https://example.com", |
| 185 | + CacheTTLSeconds: defaultCacheTTLSeconds, |
| 186 | + CacheTTL: time.Duration(defaultCacheTTLSeconds) * time.Second, |
| 187 | + ExtAuthTimeoutS: defaultExtAuthTimeoutS, |
| 188 | + ExtAuthTimeout: time.Duration(defaultExtAuthTimeoutS) * time.Second, |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +// NewTestConfigWithAuth creates a Config for testing with custom auth URL |
| 193 | +func NewTestConfigWithAuth(extAuthURL string) *Config { |
| 194 | + cfg := NewTestConfig() |
| 195 | + cfg.ExtAuthURL = extAuthURL |
| 196 | + return cfg |
| 197 | +} |
0 commit comments