DigitalManufacturingHostModule.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using Procurement.EntityFrameworkCore;
  2. using Microsoft.AspNetCore.Authentication.JwtBearer;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.AspNetCore.Cors;
  5. using Microsoft.AspNetCore.DataProtection;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Hosting;
  10. using Microsoft.OpenApi.Models;
  11. using StackExchange.Redis;
  12. using System;
  13. using System.IO;
  14. using System.Linq;
  15. using Volo.Abp;
  16. using Volo.Abp.AspNetCore.MultiTenancy;
  17. using Volo.Abp.AspNetCore.Mvc;
  18. using Volo.Abp.AspNetCore.Serilog;
  19. using Volo.Abp.Autofac;
  20. using Volo.Abp.Caching;
  21. using Volo.Abp.Data;
  22. using Volo.Abp.EntityFrameworkCore.MySQL;
  23. using Volo.Abp.Localization;
  24. using Volo.Abp.Modularity;
  25. using Volo.Abp.MultiTenancy;
  26. using Volo.Abp.Threading;
  27. using Volo.Abp.VirtualFileSystem;
  28. namespace Procurement
  29. {
  30. [DependsOn(
  31. typeof(AbpAutofacModule),
  32. typeof(AbpEntityFrameworkCoreMySQLModule),
  33. typeof(DigitalManufacturingApplicationModule),
  34. typeof(DigitalManufacturingEntityFrameworkCoreModule),
  35. typeof(AbpAspNetCoreMultiTenancyModule),
  36. typeof(AbpAspNetCoreSerilogModule),
  37. typeof(AbpAspNetCoreMvcModule)
  38. )]
  39. public class ProcurementHostModule : AbpModule
  40. {
  41. private const string DefaultCorsPolicyName = "Default";
  42. public override void ConfigureServices(ServiceConfigurationContext context)
  43. {
  44. var configuration = context.Services.GetConfiguration();
  45. var hostingEnvironment = context.Services.GetHostingEnvironment();
  46. ConfigureConventionalControllers();
  47. ConfigureMultiTenancy();
  48. ConfigureAuthentication(context, configuration);
  49. ConfigureLocalization();
  50. ConfigureCache(configuration);
  51. ConfigureVirtualFileSystem(context);
  52. ConfigureCors(context, configuration);
  53. //ConfigureRedis(context, configuration, hostingEnvironment);
  54. ConfigureSwaggerServices(context);
  55. }
  56. private void ConfigureConventionalControllers()
  57. {
  58. Configure<AbpAspNetCoreMvcOptions>(options =>
  59. {
  60. options.ConventionalControllers.Create(typeof(DigitalManufacturingApplicationModule).Assembly, opts =>
  61. {
  62. opts.RootPath = "digitalmanufacturing";
  63. });
  64. });
  65. }
  66. private void ConfigureMultiTenancy()
  67. {
  68. Configure<AbpMultiTenancyOptions>(options =>
  69. {
  70. options.IsEnabled = true;
  71. });
  72. }
  73. private void ConfigureCache(IConfiguration configuration)
  74. {
  75. Configure<AbpDistributedCacheOptions>(options =>
  76. {
  77. options.KeyPrefix = "DigitalManufacturing:";
  78. });
  79. }
  80. private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
  81. {
  82. context.Services.AddCors(options =>
  83. {
  84. options.AddPolicy(DefaultCorsPolicyName, builder =>
  85. {
  86. builder
  87. .WithOrigins(
  88. configuration["App:CorsOrigins"]
  89. .Split(",", StringSplitOptions.RemoveEmptyEntries)
  90. .Select(o => o.RemovePostFix("/"))
  91. .ToArray()
  92. )
  93. .WithAbpExposedHeaders()
  94. .SetIsOriginAllowedToAllowWildcardSubdomains()
  95. .AllowAnyHeader()
  96. .AllowAnyMethod()
  97. .AllowCredentials();
  98. });
  99. });
  100. }
  101. private void ConfigureVirtualFileSystem(ServiceConfigurationContext context)
  102. {
  103. var hostingEnvironment = context.Services.GetHostingEnvironment();
  104. if (hostingEnvironment.IsDevelopment())
  105. {
  106. Configure<AbpVirtualFileSystemOptions>(options =>
  107. {
  108. options.FileSets.ReplaceEmbeddedByPhysical<DigitalManufacturingDomainModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}DigitalManufacturing.Domain"));
  109. options.FileSets.ReplaceEmbeddedByPhysical<DigitalManufacturingApplicationContractsModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}DigitalManufacturing.Application.Contracts"));
  110. options.FileSets.ReplaceEmbeddedByPhysical<DigitalManufacturingApplicationModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}DigitalManufacturing.Application"));
  111. });
  112. }
  113. }
  114. private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
  115. {
  116. context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  117. .AddJwtBearer(options =>
  118. {
  119. options.Authority = configuration["AuthServer:Authority"];
  120. options.RequireHttpsMetadata = false;
  121. options.Audience = "BusinessService";
  122. });
  123. }
  124. private static void ConfigureSwaggerServices(ServiceConfigurationContext context)
  125. {
  126. context.Services.AddSwaggerGen(
  127. options =>
  128. {
  129. options.SwaggerDoc("v1", new OpenApiInfo { Title = "DigitalManufacturing Service API", Version = "v1" });
  130. options.DocInclusionPredicate((docName, description) => true);
  131. });
  132. }
  133. private void ConfigureLocalization()
  134. {
  135. Configure<AbpLocalizationOptions>(options =>
  136. {
  137. options.Languages.Add(new LanguageInfo("cs", "cs", "Čeština"));
  138. options.Languages.Add(new LanguageInfo("en", "en", "English"));
  139. options.Languages.Add(new LanguageInfo("pt-BR", "pt-BR", "Português"));
  140. options.Languages.Add(new LanguageInfo("tr", "tr", "Türkçe"));
  141. options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
  142. options.Languages.Add(new LanguageInfo("zh-Hant", "zh-Hant", "繁體中文"));
  143. });
  144. }
  145. private void ConfigureRedis(
  146. ServiceConfigurationContext context,
  147. IConfiguration configuration,
  148. IWebHostEnvironment hostingEnvironment)
  149. {
  150. context.Services.AddStackExchangeRedisCache(options =>
  151. {
  152. options.Configuration = configuration["Redis:Configuration"];
  153. });
  154. if (!hostingEnvironment.IsDevelopment())
  155. {
  156. var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
  157. context.Services
  158. .AddDataProtection()
  159. .PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys");
  160. }
  161. }
  162. public override void OnApplicationInitialization(ApplicationInitializationContext context)
  163. {
  164. var app = context.GetApplicationBuilder();
  165. app.UseCorrelationId();
  166. app.UseStaticFiles();
  167. app.UseRouting();
  168. app.UseCors(DefaultCorsPolicyName);
  169. app.UseAuthentication();
  170. app.UseAbpClaimsMap();
  171. app.UseMultiTenancy();
  172. app.UseAbpRequestLocalization();
  173. app.UseSwagger();
  174. app.UseSwaggerUI(options =>
  175. {
  176. options.SwaggerEndpoint("/swagger/v1/swagger.json", "DigitalManufacturing Service API");
  177. });
  178. app.UseAuditing();
  179. app.UseAbpSerilogEnrichers();
  180. app.UseConfiguredEndpoints();
  181. AsyncHelper.RunSync(async () =>
  182. {
  183. using (var scope = context.ServiceProvider.CreateScope())
  184. {
  185. await scope.ServiceProvider
  186. .GetRequiredService<IDataSeeder>()
  187. .SeedAsync();
  188. }
  189. });
  190. }
  191. }
  192. }