123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- using Aip.Service;
- using Aip.Service.Aip.Serivces;
- using Aip.Service.Configurations;
- using Aip.Service.Infrastructures;
- using Aip.Service.Repositories;
- using Aip.Service.Services;
- using Aip.Service.Services.Interfaces;
- using Microsoft.AspNetCore.HttpOverrides;
- using Serilog;
- using Serilog.Extensions.Logging;
- using System.Net;
- using Microsoft.OpenApi.Models;
- using Microsoft.Extensions.DependencyInjection;
- using Aip.Service.Client;
- using Aip.Service.Middlewares;
- internal class Program
- {
- private static async Task Main(string[] args)
- {
- try
- {
- var configuration = new ConfigurationBuilder()
- .SetBasePath(Directory.GetCurrentDirectory())
- .AddJsonFile(
- path: "appsettings.json",
- optional: false,
- reloadOnChange: true)
- .AddJsonFile(
- path: $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
- optional: true,
- reloadOnChange: true)
- .Build();
- var serillogLogger = new Serilog.LoggerConfiguration()
- .ReadFrom.Configuration(configuration)
- .MinimumLevel.Information()
- .Enrich.FromLogContext()
- .CreateLogger();
- Log.Logger = serillogLogger.ForContext<Program>();
- int licenseAccounts = LicenseManager.LocalValidateLicense();
- if (licenseAccounts <= 0)
- {
- if (licenseAccounts == 0)
- {
- Log.Fatal("프로그램 라이센스에 허가된 계정 정보가 없습니다..");
- }
- else
- {
- Log.Fatal("프로그램 라이센스 정보를 확인하지 못했습니다.");
- }
- return;
- }
- var builder = WebApplication.CreateBuilder(args);
- builder.Logging.ClearProviders();
- builder.Host.UseSerilog((context, services, configuration) => configuration
- .ReadFrom.Configuration(context.Configuration)
- .ReadFrom.Services(services)
- .Enrich.FromLogContext(), true);
- ConfigureSettings(builder.Host, builder.Environment); // 환경설정 파일 설정
- if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != null)
- {
- Log.Information("Start Azure Information RESTFull API Service... License Accounts: {0} EA. [{1}]",
- licenseAccounts, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
- }
- else
- {
- Log.Information("Start Azure Information RESTFull API Service...License Accounts: {0} EA.", licenseAccounts);
- }
- Startup.SetSystemConfig();
- ILogger<AipDbRepository> aipDbRepositoryLogger = new SerilogLoggerFactory(serillogLogger).CreateLogger<AipDbRepository>();
- ILogger<ApiAuthService> apiAuthServiceLogger = new SerilogLoggerFactory(serillogLogger).CreateLogger<ApiAuthService>();
- ILogger<AipConfigService> apiConfigServiceLogger = new SerilogLoggerFactory(serillogLogger).CreateLogger<AipConfigService>();
- DatabaseFactory databaseFactory = new DatabaseFactory(builder.Configuration);
- AipDbRepository aipDbRepository = new AipDbRepository(aipDbRepositoryLogger, builder.Configuration, databaseFactory);
- AipSettings aipSettings = await aipDbRepository.LoadAipConfig();
- ApiAuthService apiAuthService = new ApiAuthService(apiAuthServiceLogger, aipDbRepository);
- apiAuthService.LoadAuthInformation();
- AipFileService aipFileService = new AipFileService(serillogLogger, aipSettings.GetConfig());
- AipConfigService aipConfigService = new AipConfigService(apiConfigServiceLogger, aipDbRepository, aipSettings, aipFileService);
- aipConfigService.DownloadAipFileInformations();
- builder.Services.AddSwaggerGen(c => {
- c.SwaggerDoc("v1", new OpenApiInfo { Title = "Azure Information Protection RESTFull API Service Gateway", Version = "v1" });
- //c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
- });
- builder.Services.Configure<ForwardedHeadersOptions>(options =>
- {
- options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
- options.KnownNetworks.Clear();
- options.KnownProxies.Clear();
- });
- Log.Information("Start AIP RESTFull Service...Binding Port: {0}.", aipSettings.Port);
- //https://learn.microsoft.com/ko-kr/aspnet/core/fundamentals/servers/kestrel/options?view=aspnetcore-8.0
- builder.WebHost.ConfigureKestrel((context, serverOptions) =>
- {
- serverOptions.Limits.MaxRequestBodySize = 100_000_000; // [RequestSizeLimit(100_000_000)] --> Controller 위에 선언, IHttpMaxRequestBodySizeFeature
- serverOptions.Limits.MaxConcurrentConnections = 500;
- serverOptions.Limits.MaxConcurrentUpgradedConnections = 500;
- //serverOptions.Limits.Http2.InitialConnectionWindowSize = 131_072;
- serverOptions.AllowSynchronousIO = false;
- serverOptions.Listen(IPAddress.Any, aipSettings.Port, listenOptions =>
- {
- //listenOptions.UseHttps("testCert.pfx", "testPassword");
- //listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
- });
- });
- builder.Services.AddSingleton<AipSettings>(aipSettings);
- builder.Services.AddSingleton<IDatabaseFactory>(databaseFactory);
- builder.Services.AddSingleton<IAipDbRepository>(aipDbRepository);
- builder.Services.AddSingleton<IApiAuthService>(apiAuthService);
- builder.Services.AddSingleton<AipFileService>(aipFileService);
- builder.Services.AddSingleton<IApiConfigService>(aipConfigService);
- builder.Services.AddSingleton<IAipDbLoggingService, AipDbLoggingService>();
- builder.Services.AddScoped<IApiAipService, ApiAipService>();
- builder.Services.AddScoped<IApiDbService, ApiDbService>();
- builder.Services.AddScoped<IApiFileService, ApiFileService>();
- builder.Services.AddScoped<IApiStreamService, ApiStreamService>();
- builder.Services.AddHttpClient("setLabel", c =>
- {
- }).AddTypedClient(c => Refit.RestService.For<ISetLabelClient>(c));
- builder.Services.AddControllers();
- builder.Services.AddCors(p => p.AddPolicy("corsapp", builder =>
- {
- //builder.WithOrigins("*", "http://localhost:3011", "https://localhost:3011", "https://localhost:7261").AllowAnyMethod().AllowAnyHeader();
- }));
- var app = builder.Build();
- app.UseSerilogRequestLogging();
- app.UseMiddleware<RequestResponseLogging>();
- app.UseRouting();
- app.UseCors(builder => builder
- .AllowAnyOrigin()
- .AllowAnyMethod()
- .AllowAnyHeader());
- app.UseCors("corsapp");
- app.UseAuthorization();
- //if (app.Environment.IsDevelopment())
- {
- app.UseSwagger();
- app.UseSwaggerUI(options =>
- {
- options.SwaggerEndpoint("/swagger/v1/swagger.json", "AIP Gateway API v1");
- options.RoutePrefix = "swagger-ui";
- });
- }
- app.MapControllers();
- app.Run();
- Log.Information("Azure Information RESTFull API Service...Stopped cleanly.");
- }
- catch (Exception ex)
- {
- Log.Fatal(ex, "Azure Information RESTFull API Service...An unhandled exception occurred during bootstrapping.");
- }
- finally
- {
- Log.CloseAndFlush();
- }
- }
- private static void ConfigureSettings(IHostBuilder hostBuilder, IHostEnvironment environment)
- {
- hostBuilder.ConfigureAppConfiguration(config =>
- {
- config
- .SetBasePath(Directory.GetCurrentDirectory())
- .AddJsonFile(
- path: "appsettings.json",
- optional: false,
- reloadOnChange: true)
- .AddJsonFile(
- path: $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
- optional: true,
- reloadOnChange: true)
- .AddEnvironmentVariables();
- });
- }
- }
|