1515namespace Elastic . Documentation . Api . Infrastructure ;
1616
1717[ EnumExtensions ]
18- public enum AppEnvironment
18+ public enum AppEnv
1919{
2020 [ Display ( Name = "dev" ) ] Dev ,
2121 [ Display ( Name = "staging" ) ] Staging ,
2222 [ Display ( Name = "edge" ) ] Edge ,
2323 [ Display ( Name = "prod" ) ] Prod
2424}
2525
26- public class LlmGatewayOptions
26+ public class AppEnvironment
2727{
28- public string ServiceAccount { get ; set ; } = string . Empty ;
29- public string FunctionUrl { get ; set ; } = string . Empty ;
30- public string TargetAudience { get ; set ; } = string . Empty ;
28+ public AppEnv Current { get ; init ; }
3129}
3230
3331public static class ServicesExtension
@@ -41,42 +39,44 @@ public static class ServicesExtension
4139
4240 public static void AddElasticDocsApiUsecases ( this IServiceCollection services , string ? appEnvironment )
4341 {
44- if ( AppEnvironmentExtensions . TryParse ( appEnvironment , out var parsedEnvironment , true ) )
42+ if ( AppEnvExtensions . TryParse ( appEnvironment , out var parsedEnvironment , true ) )
4543 {
4644 AddElasticDocsApiUsecases ( services , parsedEnvironment ) ;
4745 }
4846 else
4947 {
5048 var logger = GetLogger ( services ) ;
5149 logger ? . LogWarning ( "Unable to parse environment {AppEnvironment} into AppEnvironment. Using default AppEnvironment.Dev" , appEnvironment ) ;
52- AddElasticDocsApiUsecases ( services , AppEnvironment . Dev ) ;
50+ AddElasticDocsApiUsecases ( services , AppEnv . Dev ) ;
5351 }
5452 }
5553
5654
57- private static void AddElasticDocsApiUsecases ( this IServiceCollection services , AppEnvironment appEnvironment )
55+ private static void AddElasticDocsApiUsecases ( this IServiceCollection services , AppEnv appEnv )
5856 {
5957 _ = services . ConfigureHttpJsonOptions ( options =>
6058 {
6159 options . SerializerOptions . TypeInfoResolverChain . Insert ( 0 , ApiJsonContext . Default ) ;
6260 } ) ;
6361 _ = services . AddHttpClient ( ) ;
64- AddParameterProvider ( services , appEnvironment ) ;
65- AddAskAiUsecase ( services , appEnvironment ) ;
62+ // Register AppEnvironment as a singleton for dependency injection
63+ _ = services . AddSingleton ( new AppEnvironment { Current = appEnv } ) ;
64+ AddParameterProvider ( services , appEnv ) ;
65+ AddAskAiUsecase ( services , appEnv ) ;
6666 }
6767
68- // https://docs.aws.amazon.com/systems-manager/latest/userguide/ps-integration-lambda-extensions.html
69- private static void AddParameterProvider ( IServiceCollection services , AppEnvironment appEnvironment )
68+ // https://docs.aws.amazon.com/systems -manager/latest/userguide/ps-integration-lambda-extensions.html
69+ private static void AddParameterProvider ( IServiceCollection services , AppEnv appEnv )
7070 {
7171 var logger = GetLogger ( services ) ;
7272
73- switch ( appEnvironment )
73+ switch ( appEnv )
7474 {
75- case AppEnvironment . Prod :
76- case AppEnvironment . Staging :
77- case AppEnvironment . Edge :
75+ case AppEnv . Prod :
76+ case AppEnv . Staging :
77+ case AppEnv . Edge :
7878 {
79- logger ? . LogInformation ( "Configuring LambdaExtensionParameterProvider for environment {AppEnvironment}" , appEnvironment ) ;
79+ logger ? . LogInformation ( "Configuring LambdaExtensionParameterProvider for environment {AppEnvironment}" , appEnv ) ;
8080 _ = services . AddHttpClient ( LambdaExtensionParameterProvider . HttpClientName , client =>
8181 {
8282 client . BaseAddress = new Uri ( "http://localhost:2773" ) ;
@@ -85,39 +85,27 @@ private static void AddParameterProvider(IServiceCollection services, AppEnviron
8585 _ = services . AddSingleton < IParameterProvider , LambdaExtensionParameterProvider > ( ) ;
8686 break ;
8787 }
88- case AppEnvironment . Dev :
88+ case AppEnv . Dev :
8989 {
90- logger ? . LogInformation ( "Configuring LocalParameterProvider for environment {AppEnvironment}" , appEnvironment ) ;
90+ logger ? . LogInformation ( "Configuring LocalParameterProvider for environment {AppEnvironment}" , appEnv ) ;
9191 _ = services . AddSingleton < IParameterProvider , LocalParameterProvider > ( ) ;
9292 break ;
9393 }
9494 default :
9595 {
96- throw new ArgumentOutOfRangeException ( nameof ( appEnvironment ) , appEnvironment ,
96+ throw new ArgumentOutOfRangeException ( nameof ( appEnv ) , appEnv ,
9797 "Unsupported environment for parameter provider." ) ;
9898 }
9999 }
100100 }
101101
102- private static void AddAskAiUsecase ( IServiceCollection services , AppEnvironment appEnvironment )
102+ private static void AddAskAiUsecase ( IServiceCollection services , AppEnv appEnv )
103103 {
104104 var logger = GetLogger ( services ) ;
105- logger ? . LogInformation ( "Configuring AskAi use case for environment {AppEnvironment}" , appEnvironment ) ;
106-
107- _ = services . Configure < LlmGatewayOptions > ( options =>
108- {
109- var serviceProvider = services . BuildServiceProvider ( ) ;
110- var parameterProvider = serviceProvider . GetRequiredService < IParameterProvider > ( ) ;
111- var appEnvString = appEnvironment . ToStringFast ( true ) ;
112-
113- options . ServiceAccount = parameterProvider . GetParam ( $ "/elastic-docs-v3/{ appEnvString } /llm-gateway-service-account") . GetAwaiter ( ) . GetResult ( ) ;
114- options . FunctionUrl = parameterProvider . GetParam ( $ "/elastic-docs-v3/{ appEnvString } /llm-gateway-function-url") . GetAwaiter ( ) . GetResult ( ) ;
115-
116- var functionUri = new Uri ( options . FunctionUrl ) ;
117- options . TargetAudience = $ "{ functionUri . Scheme } ://{ functionUri . Host } ";
118- } ) ;
119- _ = services . AddScoped < GcpIdTokenProvider > ( ) ;
120- _ = services . AddScoped < IAskAiGateway < Stream > , LlmGatewayAskAiGateway > ( ) ;
105+ logger ? . LogInformation ( "Configuring AskAi use case for environment {AppEnvironment}" , appEnv ) ;
106+ _ = services . AddSingleton < GcpIdTokenProvider > ( ) ;
107+ _ = services . AddSingleton < IAskAiGateway < Stream > , LlmGatewayAskAiGateway > ( ) ;
108+ _ = services . AddScoped < LlmGatewayOptions > ( ) ;
121109 _ = services . AddScoped < AskAiUsecase > ( ) ;
122110 }
123111}
0 commit comments