FileStorageHostModule.cs 7.8 KB

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