33using Microsoft . Extensions . Configuration ;
44using Microsoft . Extensions . DependencyInjection ;
55using Prometheus ;
6- using System . Reflection ;
76using TaskHub . Observability . Logger . Serilog ;
87using TaskHub . Observability . Metrics . Implementation . Bootstrap ;
98using TaskHub . Observability . OpenTelemetry ;
109using TaskHub . Observability . Traces . Implementation ;
1110using TaskHub . Shared . Authorization . Identity . Bootstrap ;
12- using TaskHub . Shared . Bootstraper . Options ;
1311using TaskHub . Shared . Bootstraper . Settings ;
1412using TaskHub . Shared . Bootstraper . Setup ;
1513using TaskHub . Shared . Commands . Bus ;
2119
2220namespace TaskHub . Shared . Bootstraper ;
2321
24- public class FullHostBuilder ( string [ ] args )
22+ public class FullHostBuilder ( string [ ] args ) : BasicHostBuilder ( args )
2523{
26- private Action < WebApplication > _runAct = _ => { } ;
27- private Action < WebApplicationBuilder > _initAct = _ => { } ;
24+ public async Task Start < TContext > ( ) where TContext : DbContext
25+ {
26+ Builder . Services . AddAppDbContext < TContext > ( Builder . Configuration , o => o . ConnectionString = "Default" ) ;
27+ AppStart ( ) ;
28+ await Task . CompletedTask ;
29+ }
2830
29- public FullHostBuilder Add ( Action < WebApplicationBuilder > act )
31+ public async Task Start < TContext > ( Action < WebApplicationBuilder > act ) where TContext : DbContext
3032 {
31- _initAct = act ;
32- return this ;
33+ Builder . Services . AddAppDbContext < TContext > ( Builder . Configuration , o => o . ConnectionString = "Default" ) ;
34+ AppStart ( ) ;
35+ act ( Builder ) ;
3336 }
3437
35- public FullHostBuilder Use ( Action < WebApplication > act )
38+ protected override void AppStart ( )
3639 {
37- _runAct = act ;
38- return this ;
40+ base . AppStart ( ) ;
41+ var assms = AppDomain . CurrentDomain . GetAssemblies ( )
42+ . Where ( a => ! a . IsDynamic && a . GetName ( ) . Name ! . StartsWith ( "TaskHub." ) )
43+ . SelectMany ( a => a . GetTypes ( ) ) . ToArray ( ) ;
44+
45+ Builder . Services . AddAppOpenTelemetry ( o => Builder . Configuration . GetSection ( "OpenTelemetry" ) . Bind ( o ) ) ;
46+ Builder . Services . AddAppAuthorization ( o => Builder . Configuration . GetSection ( "Jwt" ) . Bind ( o ) ) ;
47+ Builder . Services . AddAppVersioning ( o => Builder . Configuration . GetSection ( "Versioning" ) . Bind ( o ) ) ;
48+ Builder . Services . AddAppRedis ( o => Builder . Configuration . GetSection ( "Redis" ) . Bind ( o ) ) ;
49+ Builder . Services . AddFixedRateLimiter ( o => Builder . Configuration . GetSection ( "RateLimiter" ) . Bind ( o ) ) ;
50+ Builder . Services . AddAppSwagger ( o => Builder . Configuration . GetSection ( "Swagger" ) . Bind ( o ) ) ;
51+ Builder . Services . AddAppTraces ( ServiceName ) ;
52+ Builder . Services . AddAppMetrics ( Builder . Configuration ) ;
53+ Builder . Services . AddAuthorizationService ( ) ;
54+ Builder . Services . AddAppCommandsBus ( assms ) ;
55+ Builder . Services . AddAppUnitOfWork ( assms ) ;
56+ Builder . Services . AddAppRepositories ( assms ) ;
57+ Builder . Host . AddAppSerilog ( ServiceName ) ;
3958 }
4059
41- public async Task StartWith < TContext > ( Action < HostOptions > action ) where TContext : DbContext
60+ public async Task Run < TContext > ( ) where TContext : DbContext
4261 {
43- var options = new HostOptions ( ) ;
44- action ( options ) ;
45- var host = new BasicHostBuilder ( args ) ;
62+ var app = await BuildApp < TContext > ( ) ;
63+ await app . RunAsync ( ) ;
64+ }
4665
47- SwaggerOptions ? swagger = null ;
48- var serviceName = string . Empty ;
49- host . Init ( builder =>
50- {
51- var assms = Directory . GetFiles ( AppContext . BaseDirectory , "*.dll" ) . Select ( Assembly . LoadFrom ) . SelectMany ( a => a . GetTypes ( ) ) ;
52- serviceName = builder . Configuration . GetSection ( "OpenTelemetry:ServiceName" ) . Get < string > ( ) ?? string . Empty ;
53- swagger = builder . Configuration . GetSection ( "Swagger" ) . Get < SwaggerOptions > ( ) ?? new ( ) ;
54- builder . Services . AddAppDbContext < TContext > ( builder . Configuration , o => o . ConnectionString = options . ConnectionString ) ;
55- builder . Services . AddAppOpenTelemetry ( o => builder . Configuration . GetSection ( "OpenTelemetry" ) . Bind ( o ) ) ;
56- builder . Services . AddAppAuthorization ( o => builder . Configuration . GetSection ( "Jwt" ) . Bind ( o ) ) ;
57- builder . Services . AddAppVersioning ( o => builder . Configuration . GetSection ( "Versioning" ) . Bind ( o ) ) ;
58- builder . Services . AddAppRedis ( o => builder . Configuration . GetSection ( "Redis" ) . Bind ( 0 ) ) ;
59- builder . Services . AddFixedRateLimiter ( o => builder . Configuration . GetSection ( "RateLimiter" ) . Bind ( o ) ) ;
60- builder . Services . AddAppSwagger ( o => builder . Configuration . GetSection ( "Swagger" ) . Bind ( o ) ) ;
61- builder . Services . AddAppTraces ( serviceName ) ;
62- builder . Services . AddAppMetrics ( builder . Configuration ) ;
63- builder . Services . AddAppSerilog ( builder . Configuration ) ;
64- builder . AddAuthorizationService ( ) ;
65- builder . Services . AddAppCommandsBus ( assms ) ;
66- builder . Services . AddAppUnitOfWork ( assms ) ;
67- builder . Services . AddAppRepositories ( assms ) ;
68- assms = null ;
69- _initAct ( builder ) ;
70- } ) ;
66+ public async Task Run < TContext > ( Func < WebApplication , Task > action ) where TContext : DbContext
67+ {
68+ var app = await BuildApp < TContext > ( ) ;
69+ await action ( app ) ;
70+ await app . RunAsync ( ) ;
71+ }
7172
72- await host . Run ( async app =>
73+ private async Task < WebApplication > BuildApp < TContext > ( ) where TContext : DbContext
74+ {
75+ var app = BuildApp ( ) ;
76+
77+ app . UseHttpMetrics ( options =>
7378 {
74- app . UseHttpMetrics ( options =>
75- {
76- options . ReduceStatusCodeCardinality ( ) ;
77- options . AddCustomLabel ( "service" , ctx => serviceName ) ;
78- } ) ;
79- app . MapMetrics ( ) . AllowAnonymous ( ) ;
80- if ( swagger != null ) app . UseAppSwaggerUI ( o => o = swagger ) ;
81- await app . Services . MigrateAsync < TContext > ( ) ;
82- _runAct ( app ) ;
79+ options . ReduceStatusCodeCardinality ( ) ;
80+ options . AddCustomLabel ( "service" , ctx => ServiceName ) ;
8381 } ) ;
82+
83+ app . MapMetrics ( ) . AllowAnonymous ( ) ;
84+ app . UseAppSwaggerUI ( o => Builder . Configuration . GetSection ( "Swagger" ) . Bind ( o ) ) ;
85+ return app ;
8486 }
85- }
87+ }
0 commit comments