Program.cs 8.0 KB

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