Program.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using Aip.Service;
  2. using Aip.Service.Aip.Serivces;
  3. using Aip.Service.Configurations;
  4. using Aip.Service.Infrastructures;
  5. using Aip.Service.Repositories;
  6. using Aip.Service.Services;
  7. using Aip.Service.Services.Interfaces;
  8. using Microsoft.AspNetCore.HttpOverrides;
  9. using Serilog;
  10. using Serilog.Extensions.Logging;
  11. using System.Net;
  12. using Microsoft.OpenApi.Models;
  13. using Microsoft.Extensions.DependencyInjection;
  14. using Aip.Service.Client;
  15. using Aip.Service.Middlewares;
  16. internal class Program
  17. {
  18. private static async Task Main(string[] args)
  19. {
  20. try
  21. {
  22. var configuration = new ConfigurationBuilder()
  23. .SetBasePath(Directory.GetCurrentDirectory())
  24. .AddJsonFile(
  25. path: "appsettings.json",
  26. optional: false,
  27. reloadOnChange: true)
  28. .AddJsonFile(
  29. path: $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
  30. optional: true,
  31. reloadOnChange: true)
  32. .Build();
  33. var serillogLogger = new Serilog.LoggerConfiguration()
  34. .ReadFrom.Configuration(configuration)
  35. .MinimumLevel.Information()
  36. .Enrich.FromLogContext()
  37. .CreateLogger();
  38. Log.Logger = serillogLogger.ForContext<Program>();
  39. int licenseAccounts = LicenseManager.LocalValidateLicense();
  40. if (licenseAccounts <= 0)
  41. {
  42. if (licenseAccounts == 0)
  43. {
  44. Log.Fatal("프로그램 라이센스에 허가된 계정 정보가 없습니다..");
  45. }
  46. else
  47. {
  48. Log.Fatal("프로그램 라이센스 정보를 확인하지 못했습니다.");
  49. }
  50. return;
  51. }
  52. var builder = WebApplication.CreateBuilder(args);
  53. builder.Logging.ClearProviders();
  54. builder.Host.UseSerilog((context, services, configuration) => configuration
  55. .ReadFrom.Configuration(context.Configuration)
  56. .ReadFrom.Services(services)
  57. .Enrich.FromLogContext(), true);
  58. ConfigureSettings(builder.Host, builder.Environment); // 환경설정 파일 설정
  59. if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != null)
  60. {
  61. Log.Information("Start Azure Information RESTFull API Service... License Accounts: {0} EA. [{1}]",
  62. licenseAccounts, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
  63. }
  64. else
  65. {
  66. Log.Information("Start Azure Information RESTFull API Service...License Accounts: {0} EA.", licenseAccounts);
  67. }
  68. Startup.SetSystemConfig();
  69. ILogger<AipDbRepository> aipDbRepositoryLogger = new SerilogLoggerFactory(serillogLogger).CreateLogger<AipDbRepository>();
  70. ILogger<ApiAuthService> apiAuthServiceLogger = new SerilogLoggerFactory(serillogLogger).CreateLogger<ApiAuthService>();
  71. ILogger<AipConfigService> apiConfigServiceLogger = new SerilogLoggerFactory(serillogLogger).CreateLogger<AipConfigService>();
  72. DatabaseFactory databaseFactory = new DatabaseFactory(builder.Configuration);
  73. AipDbRepository aipDbRepository = new AipDbRepository(aipDbRepositoryLogger, builder.Configuration, databaseFactory);
  74. AipSettings aipSettings = await aipDbRepository.LoadAipConfig();
  75. ApiAuthService apiAuthService = new ApiAuthService(apiAuthServiceLogger, aipDbRepository);
  76. apiAuthService.LoadAuthInformation();
  77. AipFileService aipFileService = new AipFileService(serillogLogger, aipSettings.GetConfig());
  78. AipConfigService aipConfigService = new AipConfigService(apiConfigServiceLogger, aipDbRepository, aipSettings, aipFileService);
  79. aipConfigService.DownloadAipFileInformations();
  80. builder.Services.AddSwaggerGen(c => {
  81. c.SwaggerDoc("v1", new OpenApiInfo { Title = "Azure Information Protection RESTFull API Service Gateway", Version = "v1" });
  82. //c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
  83. });
  84. builder.Services.Configure<ForwardedHeadersOptions>(options =>
  85. {
  86. options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
  87. options.KnownNetworks.Clear();
  88. options.KnownProxies.Clear();
  89. });
  90. Log.Information("Start AIP RESTFull Service...Binding Port: {0}.", aipSettings.Port);
  91. //https://learn.microsoft.com/ko-kr/aspnet/core/fundamentals/servers/kestrel/options?view=aspnetcore-8.0
  92. builder.WebHost.ConfigureKestrel((context, serverOptions) =>
  93. {
  94. serverOptions.Limits.MaxRequestBodySize = 100_000_000; // [RequestSizeLimit(100_000_000)] --> Controller 위에 선언, IHttpMaxRequestBodySizeFeature
  95. serverOptions.Limits.MaxConcurrentConnections = 500;
  96. serverOptions.Limits.MaxConcurrentUpgradedConnections = 500;
  97. //serverOptions.Limits.Http2.InitialConnectionWindowSize = 131_072;
  98. serverOptions.AllowSynchronousIO = false;
  99. serverOptions.Listen(IPAddress.Any, aipSettings.Port, listenOptions =>
  100. {
  101. //listenOptions.UseHttps("testCert.pfx", "testPassword");
  102. //listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
  103. });
  104. });
  105. builder.Services.AddSingleton<AipSettings>(aipSettings);
  106. builder.Services.AddSingleton<IDatabaseFactory>(databaseFactory);
  107. builder.Services.AddSingleton<IAipDbRepository>(aipDbRepository);
  108. builder.Services.AddSingleton<IApiAuthService>(apiAuthService);
  109. builder.Services.AddSingleton<AipFileService>(aipFileService);
  110. builder.Services.AddSingleton<IApiConfigService>(aipConfigService);
  111. builder.Services.AddSingleton<IAipDbLoggingService, AipDbLoggingService>();
  112. builder.Services.AddScoped<IApiAipService, ApiAipService>();
  113. builder.Services.AddScoped<IApiDbService, ApiDbService>();
  114. builder.Services.AddScoped<IApiFileService, ApiFileService>();
  115. builder.Services.AddScoped<IApiStreamService, ApiStreamService>();
  116. builder.Services.AddHttpClient("setLabel", c =>
  117. {
  118. }).AddTypedClient(c => Refit.RestService.For<ISetLabelClient>(c));
  119. builder.Services.AddControllers();
  120. builder.Services.AddCors(p => p.AddPolicy("corsapp", builder =>
  121. {
  122. //builder.WithOrigins("*", "http://localhost:3011", "https://localhost:3011", "https://localhost:7261").AllowAnyMethod().AllowAnyHeader();
  123. }));
  124. var app = builder.Build();
  125. app.UseSerilogRequestLogging();
  126. app.UseMiddleware<RequestResponseLogging>();
  127. app.UseRouting();
  128. app.UseCors(builder => builder
  129. .AllowAnyOrigin()
  130. .AllowAnyMethod()
  131. .AllowAnyHeader());
  132. app.UseCors("corsapp");
  133. app.UseAuthorization();
  134. //if (app.Environment.IsDevelopment())
  135. {
  136. app.UseSwagger();
  137. app.UseSwaggerUI(options =>
  138. {
  139. options.SwaggerEndpoint("/swagger/v1/swagger.json", "AIP Gateway API v1");
  140. options.RoutePrefix = "swagger-ui";
  141. });
  142. }
  143. app.MapControllers();
  144. app.Run();
  145. Log.Information("Azure Information RESTFull API Service...Stopped cleanly.");
  146. }
  147. catch (Exception ex)
  148. {
  149. Log.Fatal(ex, "Azure Information RESTFull API Service...An unhandled exception occurred during bootstrapping.");
  150. }
  151. finally
  152. {
  153. Log.CloseAndFlush();
  154. }
  155. }
  156. private static void ConfigureSettings(IHostBuilder hostBuilder, IHostEnvironment environment)
  157. {
  158. hostBuilder.ConfigureAppConfiguration(config =>
  159. {
  160. config
  161. .SetBasePath(Directory.GetCurrentDirectory())
  162. .AddJsonFile(
  163. path: "appsettings.json",
  164. optional: false,
  165. reloadOnChange: true)
  166. .AddJsonFile(
  167. path: $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
  168. optional: true,
  169. reloadOnChange: true)
  170. .AddEnvironmentVariables();
  171. });
  172. }
  173. }