Startup.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.IO.Abstractions;
  3. using System.Security.Principal;
  4. using BO.AppServer.Business.Services;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.AspNetCore.Mvc.Authorization;
  9. using Microsoft.Extensions.Configuration;
  10. using Microsoft.Extensions.DependencyInjection;
  11. using Microsoft.Extensions.Hosting;
  12. using BO.AppServer.Data.Entity;
  13. using BO.AppServer.Data.Repository;
  14. using BO.AppServer.Metadata.Configuration;
  15. using BO.AppServer.Web.Data;
  16. using Microsoft.AspNetCore.Http;
  17. using Microsoft.AspNetCore.Identity;
  18. using Quadarax.Foundation.Core.Data.Domain;
  19. using Quadarax.Foundation.Core.Data.Interface.Domain;
  20. namespace AppServer
  21. {
  22. public class Startup
  23. {
  24. private ConfigurationService _srvConfiguration;
  25. private IWebHostEnvironment _env;
  26. public Startup(IConfiguration configuration)
  27. {
  28. Configuration = configuration;
  29. }
  30. public IConfiguration Configuration { get; }
  31. // This method gets called by the runtime. Use this method to add services to the container.
  32. public void ConfigureServices(IServiceCollection services)
  33. {
  34. //services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
  35. // .AddAzureAD(options => Configuration.Bind("AzureAd", options));
  36. // EF enable
  37. services.AddDbContext<BOContext>();
  38. services.AddDbContext<IFDbContext>();
  39. // Identity framework enable
  40. services.AddIdentity<IdentityUser, IdentityRole>()
  41. .AddEntityFrameworkStores<IFDbContext>()
  42. .AddDefaultTokenProviders();
  43. //Use file abstractions
  44. services.AddSingleton<IFileSystem,FileSystem>();
  45. // Inject IPrincipal to get current user in all levels
  46. services.AddHttpContextAccessor();
  47. services.AddTransient<IPrincipal>(provider => provider.GetService<IHttpContextAccessor>().HttpContext?.User);
  48. // Bo Services
  49. var cfg = new Configuration();
  50. Configuration.GetSection("Bo").Bind(cfg);
  51. services.Configure<Configuration>(Configuration.GetSection("Bo"));
  52. services.AddScoped<AccessService>();
  53. services.AddSingleton<ConfigurationService>();
  54. services.AddScoped<UserService>();
  55. services.AddScoped<StatisticsService>();
  56. services.AddScoped<CatalogueService>();
  57. services.AddScoped<WorkspaceService>();
  58. services.AddScoped<StorageService>();
  59. services.AddScoped<DocumentService>();
  60. // Bo Repos
  61. services.AddScoped<DataDomain, BOContext>();
  62. services.AddScoped<IDomainContextResolver<DataDomain>, DomainContextResolver<DataDomain>>();
  63. services.AddScoped<IUnitOfWork<DataDomain>, UnitOfWorkDataDomain>();
  64. services.AddScoped<UserRepo>();
  65. services.AddScoped<WorkspaceRepo>();
  66. services.AddScoped<WorkspaceBillingRepo>();
  67. services.AddScoped<StructureRepo>();
  68. services.AddScoped<BillingPlanRepo>();
  69. services.AddScoped<MimeTypeRepo>();
  70. services.AddScoped<StatisticRepo>();
  71. services.AddScoped<WorkspaceRepo>();
  72. services.AddScoped<MetadocumentRepo>();
  73. services.AddScoped<ArtifactRepo>();
  74. // Session framework
  75. services.AddDistributedMemoryCache();
  76. services.AddSession(options =>
  77. {
  78. options.IdleTimeout = TimeSpan.Parse(cfg.System.SessionIdleTimeout);
  79. });
  80. services.AddControllersWithViews(options =>
  81. {
  82. var policy = new AuthorizationPolicyBuilder()
  83. .RequireAuthenticatedUser()
  84. .Build();
  85. options.Filters.Add(new AuthorizeFilter(policy));
  86. });
  87. services.AddRazorPages();
  88. }
  89. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  90. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, IServiceProvider service)
  91. {
  92. _env = env;
  93. if (env.IsDevelopment())
  94. {
  95. app.UseDeveloperExceptionPage();
  96. }
  97. else
  98. {
  99. app.UseExceptionHandler("/Home/Error");
  100. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  101. app.UseHsts();
  102. }
  103. //May create IF database structure
  104. using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
  105. {
  106. var context = serviceScope.ServiceProvider.GetRequiredService<IFDbContext>();
  107. context.Database.EnsureCreated();
  108. }
  109. _srvConfiguration = (ConfigurationService)service.GetService(typeof(ConfigurationService));
  110. lifetime.ApplicationStarted.Register(OnApplicationStarted);
  111. if (_srvConfiguration.Configuration.System.UseSSL)
  112. app.UseHttpsRedirection();
  113. app.UseStaticFiles();
  114. app.UseRouting();
  115. // Security support
  116. app.UseAuthentication();
  117. app.UseAuthorization();
  118. // Session support
  119. app.UseSession();
  120. app.UseEndpoints(endpoints =>
  121. {
  122. endpoints.MapControllerRoute(
  123. name: "default",
  124. pattern: "{controller=Home}/{action=Index}/{id?}");
  125. endpoints.MapRazorPages();
  126. });
  127. }
  128. private void OnApplicationStarted()
  129. {
  130. _srvConfiguration.Install();
  131. if (_env.IsDevelopment())
  132. {
  133. _srvConfiguration.InstallDev();
  134. }
  135. _srvConfiguration.LogInfo("Application server is fully initialized.");
  136. }
  137. }
  138. }