Program.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using AipGateway.API.Service.Repositories;
  2. using AipGateway.API.Service.Services;
  3. using AipGateway.API.Service.Filters;
  4. using Microsoft.AspNetCore.HttpOverrides;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.OpenApi.Models;
  7. using Serilog;
  8. using AipGateway.Data;
  9. using AipGateway.Data.Entities;
  10. using Microsoft.EntityFrameworkCore;
  11. using AipGateway.API.Service.Configurations;
  12. // https://lab.cliel.com/entry/ASPNET-Core-9-%EA%B3%A0%EA%B8%89-Web-Service-%EA%B8%B0%EB%8A%A5
  13. //Log.Logger = new Serilog.LoggerConfiguration()
  14. // //.WriteTo.Console()
  15. // .WriteTo.AzureApp()
  16. // .CreateBootstrapLogger();
  17. internal class Program
  18. {
  19. [Obsolete]
  20. private static void Main(string[] args)
  21. {
  22. try
  23. {
  24. Console.WriteLine("Start Azure Information RESTFull API Service... [{0}]", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
  25. //Log.Logger = new LoggerConfiguration()
  26. // //.ReadFrom.Configuration(configuration)
  27. // .CreateLogger();
  28. var builder = WebApplication.CreateBuilder(args);
  29. ConfigureSettings(builder.Host, builder.Environment);
  30. AddAppSettings(builder.Services, builder.Configuration);
  31. ConfigureServices(builder.Services, builder.Configuration);
  32. LinkedServerRepository linkedServerRepository = new LinkedServerRepository();
  33. builder.Services.AddSingleton<ILinkedServerRepository>(linkedServerRepository);
  34. builder.Services.AddSingleton<ILinkedServerService>(new LinkedServerService(linkedServerRepository));
  35. builder.Services.AddSingleton<IAipFileManagerService>(new AipFileManagerService());
  36. builder.Services.AddControllers().AddNewtonsoftJson();
  37. // Add services to the container.
  38. //builder.Services.AddControllersWithViews();
  39. builder.Services.AddSingleton<Microsoft.AspNetCore.Http.IHttpContextAccessor, Microsoft.AspNetCore.Http.HttpContextAccessor>();
  40. builder.Services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  41. builder.Services.Configure<MvcOptions>(opts => {
  42. opts.RespectBrowserAcceptHeader = true;
  43. opts.ReturnHttpNotAcceptable = true;
  44. });
  45. //builder.Services.AddSwaggerGen(c => {
  46. // c.SwaggerDoc("v1", new OpenApiInfo { Title = "Azure Information Protection RESTFull API Service Gateway", Version = "v1" });
  47. //});
  48. builder.Services.Configure<MvcNewtonsoftJsonOptions>(opts => {
  49. opts.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
  50. });
  51. builder.Services.Configure<ForwardedHeadersOptions>(options =>
  52. {
  53. options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
  54. options.KnownNetworks.Clear();
  55. options.KnownProxies.Clear();
  56. });
  57. builder.WebHost.ConfigureKestrel((context, serverOptions) =>
  58. {
  59. serverOptions.ListenAnyIP(5000);
  60. });
  61. //https://learn.microsoft.com/ko-kr/aspnet/core/fundamentals/servers/kestrel/options?view=aspnetcore-8.0
  62. //builder.WebHost.ConfigureKestrel((context, serverOptions) =>
  63. //{
  64. //serverOptions.Limits.MaxRequestBodySize = 100_000_000;
  65. //serverOptions.Limits.MaxConcurrentConnections = 100;
  66. //serverOptions.Limits.MaxConcurrentUpgradedConnections = 100;
  67. //serverOptions.Listen(IPAddress.Any, 8000, listenOptions =>
  68. //{
  69. //listenOptions.UseHttps("testCert.pfx", "testPassword");
  70. //listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
  71. //});
  72. //});
  73. var app = builder.Build();
  74. if (app.Environment.IsDevelopment())
  75. {
  76. app.UseSwagger();
  77. app.UseSwaggerUI();
  78. }
  79. else
  80. {
  81. app.UseExceptionHandler("/Home/Error");
  82. app.UseHsts();
  83. }
  84. app.UseHttpsRedirection();
  85. app.UseStaticFiles();
  86. app.UseRouting();
  87. app.UseAuthorization();
  88. //app.MapControllerRoute(
  89. // name: "default",
  90. // pattern: "{controller=Home}/{action=Index}/{id?}");
  91. app.MapControllers();
  92. //app.UseForwardedHeaders(new ForwardedHeadersOptions
  93. //{
  94. // ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
  95. //});
  96. //app.UseForwardedHeaders(new ForwardedHeadersOptions
  97. //{
  98. // ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto, //ForwardedHeaders.All,
  99. //RequireHeaderSymmetry = false,
  100. //ForwardLimit = null,
  101. //KnownNetworks = { new System.Net.IPNetwork(IPAddress.Parse("::ffff:172.17.0.1"), 104) }
  102. //});
  103. //app.UseMiddleware<MyWebApp.TestMiddleware>();
  104. app.UseSwagger();
  105. app.UseSwaggerUI(options => {
  106. options.SwaggerEndpoint("/swagger/v1/swagger.json", "AipAPI");
  107. });
  108. app.Run();
  109. }
  110. catch (Exception ex) when (ex.GetType().Name is not "StopTheHostException"
  111. && ex.GetType().Name is not "HostAbortedException")
  112. {
  113. Log.Fatal(ex, "Unhandled exception");
  114. }
  115. finally
  116. {
  117. Log.Information("Shut down complete");
  118. Log.CloseAndFlush();
  119. }
  120. }
  121. private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
  122. {
  123. //services.AddRazorPages();
  124. services.AddControllers(options =>
  125. {
  126. options.Filters.Add<ExceptionFilter>();
  127. options.Filters.Add<AipApiAsyncActionFilter>();
  128. options.Filters.Add<AipApiActionFilter>();
  129. });
  130. // If you want to use the Action Filter in Controller or Action Level
  131. // You need to register the Action-Filter-Class first
  132. //services.AddTransient<ActionFilterEx1>();
  133. //services.AddTransient<ActionFilterEx2>();
  134. //services.AddTransient<ControllerActionFilter>();
  135. //services.AddTransient<ValidateActionFilter>();
  136. SwaggerConfiguration(services, configuration);
  137. ApplicationContextConfiguration(services, configuration);
  138. //JwtTokenConfiguration(services, configuration);
  139. //AddApplicationServices(services);
  140. }
  141. private static void ApplicationContextConfiguration(IServiceCollection services, IConfiguration configuration)
  142. {
  143. services.AddDbContext<AipDbContext>(options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
  144. //services
  145. // .AddDefaultIdentity<ApplicationUser>(IdentityOptionsProvider.GetIdentityOptions)
  146. // .AddRoles<ApplicationRole>()
  147. // .AddEntityFrameworkStores<ApplicationContext>();
  148. }
  149. private static void AddApplicationServices(IServiceCollection services)
  150. {
  151. //services.AddScoped<IAccountService, AccountService>();
  152. //services.AddScoped<IJwtTokenManager, JwtTokenManager>();
  153. //services.AddScoped<IEmailSenderService, EmailSenderService>();
  154. //services.AddScoped<IRoleService, RoleService>();
  155. //services.AddScoped<IUserService, UserService>();
  156. }
  157. private static void SwaggerConfiguration(IServiceCollection services, IConfiguration configuration)
  158. {
  159. SwaggerSettings swaggerSettings = new();
  160. configuration.GetSection(nameof(SwaggerSettings)).Bind(swaggerSettings);
  161. services.AddSwaggerGen(options =>
  162. {
  163. options.SwaggerDoc(swaggerSettings.Version, new OpenApiInfo
  164. {
  165. Title = swaggerSettings.Title,
  166. Version = swaggerSettings.Version,
  167. });
  168. });
  169. }
  170. private static void ConfigureSettings(IHostBuilder hostBuilder, IHostEnvironment environment)
  171. {
  172. hostBuilder.ConfigureAppConfiguration(config =>
  173. {
  174. config
  175. .SetBasePath(Directory.GetCurrentDirectory())
  176. .AddJsonFile(
  177. path: "appsettings.json",
  178. optional: false,
  179. reloadOnChange: true)
  180. .AddJsonFile(
  181. path: $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
  182. optional: true,
  183. reloadOnChange: true)
  184. .AddEnvironmentVariables();
  185. });
  186. }
  187. private static void AddAppSettings(IServiceCollection services, IConfiguration configuration)
  188. {
  189. //services.Configure<JwtTokenSettings>(configuration.GetSection(nameof(JwtTokenSettings)));
  190. //services.Configure<EmailSenderSettings>(configuration.GetSection(nameof(EmailSenderSettings)));
  191. //services.Configure<RequestLocalizationSettings>(configuration.GetSection(nameof(RequestLocalizationSettings)));
  192. if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development") {
  193. services.Configure<SwaggerSettings>(configuration.GetSection(nameof(SwaggerSettings)));
  194. }
  195. }
  196. }