|
6 | 6 |
|
7 | 7 | namespace Elastic.Documentation.Api.Infrastructure.Adapters.Search; |
8 | 8 |
|
9 | | -public class ElasticsearchOptions(IConfiguration configuration) |
| 9 | +public class ElasticsearchOptions |
10 | 10 | { |
| 11 | + public ElasticsearchOptions(IConfiguration configuration) |
| 12 | + { |
| 13 | + // Build a new ConfigurationBuilder to read user secrets |
| 14 | + var configBuilder = new ConfigurationBuilder(); |
| 15 | + _ = configBuilder.AddUserSecrets("72f50f33-6fb9-4d08-bff3-39568fe370b3"); |
| 16 | + var userSecretsConfig = configBuilder.Build(); |
| 17 | + var elasticUrlFromSecret = userSecretsConfig["Parameters:DocumentationElasticUrl"]; |
| 18 | + var elasticApiKeyFromSecret = userSecretsConfig["Parameters:DocumentationElasticApiKey"]; |
| 19 | + |
| 20 | + Url = GetEnv("DOCUMENTATION_ELASTIC_URL", elasticUrlFromSecret); |
| 21 | + ApiKey = GetEnv("DOCUMENTATION_ELASTIC_APIKEY", elasticApiKeyFromSecret); |
| 22 | + IndexName = configuration["DOCUMENTATION_ELASTIC_INDEX"] ?? "semantic-docs-dev-latest"; |
| 23 | + } |
| 24 | + |
| 25 | + private static string GetEnv(string name, string? defaultValue = null) |
| 26 | + { |
| 27 | + var value = Environment.GetEnvironmentVariable(name); |
| 28 | + if (!string.IsNullOrEmpty(value)) |
| 29 | + return value; |
| 30 | + if (defaultValue != null) |
| 31 | + return defaultValue; |
| 32 | + throw new ArgumentException($"Environment variable '{name}' not found."); |
| 33 | + } |
| 34 | + |
11 | 35 | // Read from environment variables (set by Terraform from SSM at deploy time) |
12 | | - public string Url { get; } = configuration["DOCUMENTATION_ELASTIC_URL"] |
13 | | - ?? throw new InvalidOperationException("DOCUMENTATION_ELASTIC_URL not configured"); |
14 | | - public string ApiKey { get; } = configuration["DOCUMENTATION_ELASTIC_APIKEY"] |
15 | | - ?? throw new InvalidOperationException("DOCUMENTATION_ELASTIC_APIKEY not configured"); |
16 | | - public string IndexName { get; } = configuration["DOCUMENTATION_ELASTIC_INDEX"] |
17 | | - ?? "documentation-latest"; |
| 36 | + public string Url { get; } |
| 37 | + public string ApiKey { get; } |
| 38 | + public string IndexName { get; } |
18 | 39 | } |
0 commit comments