@@ -38,7 +38,8 @@ public static IServiceCollection AddOpenShockMemDB(this IServiceCollection servi
3838 {
3939 // <---- Redis ---->
4040 services . AddSingleton < IConnectionMultiplexer > ( ConnectionMultiplexer . Connect ( options ) ) ;
41- services . AddSingleton < IRedisConnectionProvider , RedisConnectionProvider > ( serviceProvider => new RedisConnectionProvider ( serviceProvider . GetRequiredService < IConnectionMultiplexer > ( ) ) ) ;
41+ services . AddSingleton < IRedisConnectionProvider , RedisConnectionProvider > ( serviceProvider =>
42+ new RedisConnectionProvider ( serviceProvider . GetRequiredService < IConnectionMultiplexer > ( ) ) ) ;
4243 services . AddSingleton < IRedisPubService , RedisPubService > ( ) ;
4344
4445 return services ;
@@ -47,7 +48,8 @@ public static IServiceCollection AddOpenShockMemDB(this IServiceCollection servi
4748 public static IServiceCollection AddOpenShockDB ( this IServiceCollection services , DatabaseOptions options )
4849 {
4950 // <---- Postgres EF Core ---->
50- services . AddDbContextPool < OpenShockContext > ( builder => OpenShockContext . ConfigureOptionsBuilder ( builder , options . Conn , options . Debug ) ) ;
51+ services . AddDbContextPool < OpenShockContext > ( builder =>
52+ OpenShockContext . ConfigureOptionsBuilder ( builder , options . Conn , options . Debug ) ) ;
5153 services . AddPooledDbContextFactory < OpenShockContext > ( builder =>
5254 {
5355 builder . UseNpgsql ( options . Conn ) ;
@@ -67,7 +69,8 @@ public static IServiceCollection AddOpenShockDB(this IServiceCollection services
6769 /// <param name="services"></param>
6870 /// <param name="configureOptions"></param>
6971 /// <returns></returns>
70- private static AuthenticationBuilder AddOpenShockAuthentication ( this IServiceCollection services , Action < AuthenticationOptions > configureOptions )
72+ private static AuthenticationBuilder AddOpenShockAuthentication ( this IServiceCollection services ,
73+ Action < AuthenticationOptions > configureOptions )
7174 {
7275 ArgumentNullException . ThrowIfNull ( services ) ;
7376 ArgumentNullException . ThrowIfNull ( configureOptions ) ;
@@ -78,9 +81,9 @@ private static AuthenticationBuilder AddOpenShockAuthentication(this IServiceCol
7881 services . TryAddSingleton ( TimeProvider . System ) ;
7982 // services.TryAddSingleton<ISystemClock, SystemClock>(); // Exists in original AddAuthentication method
8083 // services.TryAddSingleton<IAuthenticationConfigurationProvider, DefaultAuthenticationConfigurationProvider>(); // Exists in original AddAuthentication method
81-
84+
8285 var builder = new AuthenticationBuilder ( services ) ;
83-
86+
8487 services . Configure ( configureOptions ) ;
8588
8689 return builder ;
@@ -91,8 +94,10 @@ private static AuthenticationBuilder AddOpenShockAuthentication(this IServiceCol
9194 /// </summary>
9295 /// <param name="services"></param>
9396 /// <param name="configureAuth"></param>
97+ /// <param name="configureMetrics"></param>
9498 /// <returns></returns>
95- public static IServiceCollection AddOpenShockServices ( this IServiceCollection services , Action < AuthenticationBuilder > ? configureAuth = null )
99+ public static IServiceCollection AddOpenShockServices ( this IServiceCollection services ,
100+ Action < AuthenticationBuilder > ? configureAuth = null , Action < MeterProviderBuilder > ? configureMetrics = null )
96101 {
97102 // <---- ASP.NET ---->
98103 services . AddExceptionHandler < OpenShockExceptionHandler > ( ) ;
@@ -116,7 +121,8 @@ public static IServiceCollection AddOpenShockServices(this IServiceCollection se
116121 opt . DefaultScheme = OpenShockAuthSchemes . UserSessionCookie ;
117122 opt . DefaultAuthenticateScheme = OpenShockAuthSchemes . UserSessionCookie ;
118123 } )
119- . AddScheme < AuthenticationSchemeOptions , UserSessionAuthentication > ( OpenShockAuthSchemes . UserSessionCookie , _ => { } )
124+ . AddScheme < AuthenticationSchemeOptions , UserSessionAuthentication > ( OpenShockAuthSchemes . UserSessionCookie ,
125+ _ => { } )
120126 . AddScheme < AuthenticationSchemeOptions , ApiTokenAuthentication > ( OpenShockAuthSchemes . ApiToken , _ => { } )
121127 . AddScheme < AuthenticationSchemeOptions , HubAuthentication > ( OpenShockAuthSchemes . HubToken , _ => { } ) ;
122128
@@ -127,12 +133,12 @@ public static IServiceCollection AddOpenShockServices(this IServiceCollection se
127133 options . AddPolicy ( OpenShockAuthPolicies . RankAdmin , policy => policy . RequireRole ( "Admin" , "System" ) ) ;
128134 // TODO: Add token permission policies
129135 } ) ;
130-
136+
131137 services . AddSingleton < IAuthorizationMiddlewareResultHandler , OpenShockAuthorizationMiddlewareResultHandler > ( ) ;
132-
138+
133139 services . ConfigureHttpJsonOptions ( opt => JsonOptions . ConfigureDefault ( opt . SerializerOptions ) ) ;
134140 services . AddControllers ( ) . AddJsonOptions ( opt => JsonOptions . ConfigureDefault ( opt . JsonSerializerOptions ) ) ;
135-
141+
136142 var apiVersioningBuilder = services . AddApiVersioning ( options =>
137143 {
138144 options . DefaultApiVersion = new ApiVersion ( 1 , 0 ) ;
@@ -146,13 +152,13 @@ public static IServiceCollection AddOpenShockServices(this IServiceCollection se
146152 setup . DefaultApiVersion = new ApiVersion ( 1 , 0 ) ;
147153 setup . AssumeDefaultVersionWhenUnspecified = true ;
148154 } ) ;
149-
155+
150156 // generic ASP.NET stuff
151157 services . AddMemoryCache ( ) ;
152158 services . AddHttpContextAccessor ( ) ;
153159 services . AddWebEncoders ( ) ;
154160 services . AddProblemDetails ( ) ;
155-
161+
156162 services . AddCors ( options =>
157163 {
158164 options . AddDefaultPolicy ( builder =>
@@ -164,7 +170,7 @@ public static IServiceCollection AddOpenShockServices(this IServiceCollection se
164170 builder . SetPreflightMaxAge ( TimeSpan . FromHours ( 24 ) ) ;
165171 } ) ;
166172 } ) ;
167-
173+
168174 // This needs to be at this position, earlier will break validation error responses
169175 services . Configure < ApiBehaviorOptions > ( options =>
170176 {
@@ -174,16 +180,21 @@ public static IServiceCollection AddOpenShockServices(this IServiceCollection se
174180 return problemDetails . ToObjectResult ( context . HttpContext ) ;
175181 } ;
176182 } ) ;
177-
183+
178184 // OpenTelemetry
179185
180186 services . AddOpenTelemetry ( )
181- . WithMetrics ( metrics => metrics
182- . AddRuntimeInstrumentation ( )
183- . AddAspNetCoreInstrumentation ( )
184- . AddHttpClientInstrumentation ( )
185- . AddPrometheusExporter ( ) ) ;
186-
187+ . WithMetrics ( metrics =>
188+ {
189+ metrics
190+ . AddRuntimeInstrumentation ( )
191+ . AddAspNetCoreInstrumentation ( )
192+ . AddHttpClientInstrumentation ( )
193+ . AddPrometheusExporter ( ) ;
194+
195+ configureMetrics ? . Invoke ( metrics ) ;
196+ } ) ;
197+
187198 // <---- OpenShock Services ---->
188199
189200 services . AddScoped < IConfigurationService , ConfigurationService > ( ) ;
@@ -233,16 +244,17 @@ await context.HttpContext.Response.WriteAsync("Too Many Requests. Please try aga
233244 var ip = context . GetRemoteIP ( ) ;
234245 if ( IPAddress . IsLoopback ( ip ) ) return RateLimitPartition . GetNoLimiter ( "ip-loopback-nolimit" ) ;
235246
236- return RateLimitPartition . GetSlidingWindowLimiter ( $ "ip-{ ip } ", _ => new SlidingWindowRateLimiterOptions
237- {
238- PermitLimit = 1000 ,
239- Window = TimeSpan . FromMinutes ( 1 ) ,
240- SegmentsPerWindow = 6 ,
241- QueueProcessingOrder = QueueProcessingOrder . OldestFirst ,
242- QueueLimit = 100
243- } ) ;
247+ return RateLimitPartition . GetSlidingWindowLimiter ( $ "ip-{ ip } ", _ =>
248+ new SlidingWindowRateLimiterOptions
249+ {
250+ PermitLimit = 1000 ,
251+ Window = TimeSpan . FromMinutes ( 1 ) ,
252+ SegmentsPerWindow = 6 ,
253+ QueueProcessingOrder = QueueProcessingOrder . OldestFirst ,
254+ QueueLimit = 100
255+ } ) ;
244256 }
245-
257+
246258 if ( user . HasClaim ( claim => claim is { Type : ClaimTypes . Role , Value : "Admin" or "System" } ) )
247259 return RateLimitPartition . GetNoLimiter ( "privileged-nolimit" ) ;
248260
@@ -290,7 +302,8 @@ await context.HttpContext.Response.WriteAsync("Too Many Requests. Please try aga
290302 return services ;
291303 }
292304
293- public static IServiceCollection AddOpenShockSignalR ( this IServiceCollection services , ConfigurationOptions redisConfig )
305+ public static IServiceCollection AddOpenShockSignalR ( this IServiceCollection services ,
306+ ConfigurationOptions redisConfig )
294307 {
295308 services . AddSignalR ( )
296309 . AddOpenShockStackExchangeRedis ( options => { options . Configuration = redisConfig ; } )
@@ -299,7 +312,7 @@ public static IServiceCollection AddOpenShockSignalR(this IServiceCollection ser
299312 options . PayloadSerializerOptions . PropertyNameCaseInsensitive = true ;
300313 options . PayloadSerializerOptions . Converters . Add ( new SemVersionJsonConverter ( ) ) ;
301314 } ) ;
302-
315+
303316 return services ;
304317 }
305318}
0 commit comments