DocSpace-buildtools/common/ASC.Api.Core/Core/BaseStartup.cs

182 lines
7.0 KiB
C#
Raw Normal View History

2022-01-26 13:04:22 +00:00
using JsonConverter = System.Text.Json.Serialization.JsonConverter;
2020-07-03 11:47:27 +00:00
namespace ASC.Api.Core
2021-08-08 12:56:54 +00:00
{
public abstract class BaseStartup
2021-09-08 10:16:58 +00:00
{
public IConfiguration Configuration { get; }
public IHostEnvironment HostEnvironment { get; }
2020-07-15 12:33:44 +00:00
public virtual JsonConverter[] Converters { get; }
2021-04-20 07:55:52 +00:00
public virtual bool AddControllersAsServices { get; } = false;
2020-10-18 16:34:30 +00:00
public virtual bool ConfirmAddScheme { get; } = false;
2021-04-20 07:55:52 +00:00
public virtual bool AddAndUseSession { get; } = false;
2020-10-18 16:34:30 +00:00
protected DIHelper DIHelper { get; }
protected bool LoadProducts { get; set; } = true;
protected bool LoadConsumers { get; } = true;
public BaseStartup(IConfiguration configuration, IHostEnvironment hostEnvironment)
{
Configuration = configuration;
2020-10-18 16:34:30 +00:00
HostEnvironment = hostEnvironment;
DIHelper = new DIHelper();
2021-10-28 13:07:14 +00:00
if (bool.TryParse(Configuration["core:products"], out var loadProducts))
{
LoadProducts = loadProducts;
}
2020-07-15 12:33:44 +00:00
}
2021-08-08 12:56:54 +00:00
public virtual void ConfigureServices(IServiceCollection services)
2020-06-23 10:48:36 +00:00
{
2021-06-16 13:54:36 +00:00
services.AddCustomHealthCheck(Configuration);
2020-07-15 12:33:44 +00:00
services.AddHttpContextAccessor();
2021-01-12 17:51:14 +00:00
services.AddMemoryCache();
2020-07-15 12:33:44 +00:00
2021-09-08 10:16:58 +00:00
if (AddAndUseSession)
2021-04-20 07:55:52 +00:00
services.AddSession();
2020-10-18 16:34:30 +00:00
DIHelper.Configure(services);
2020-07-15 12:33:44 +00:00
2021-09-03 14:02:52 +00:00
Action<JsonOptions> jsonOptions = options =>
{
options.JsonSerializerOptions.WriteIndented = false;
2021-10-01 10:26:36 +00:00
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
2021-06-22 16:55:47 +00:00
options.JsonSerializerOptions.Converters.Add(new ApiDateTimeConverter());
2020-07-15 12:33:44 +00:00
2021-06-22 16:55:47 +00:00
if (Converters != null)
2021-04-20 07:55:52 +00:00
{
2021-06-22 16:55:47 +00:00
foreach (var c in Converters)
2021-04-20 07:55:52 +00:00
{
2021-06-22 16:55:47 +00:00
options.JsonSerializerOptions.Converters.Add(c);
2021-04-20 07:55:52 +00:00
}
}
};
2021-09-03 14:02:52 +00:00
services.AddControllers()
.AddXmlSerializerFormatters()
2021-09-03 14:02:52 +00:00
.AddJsonOptions(jsonOptions);
services.AddSingleton(jsonOptions);
2020-07-15 12:33:44 +00:00
2020-10-21 11:52:44 +00:00
DIHelper.TryAdd<DisposeMiddleware>();
2020-10-19 15:53:15 +00:00
DIHelper.TryAdd<CultureMiddleware>();
DIHelper.TryAdd<IpSecurityFilter>();
DIHelper.TryAdd<PaymentFilter>();
DIHelper.TryAdd<ProductSecurityFilter>();
DIHelper.TryAdd<TenantStatusFilter>();
2020-10-22 17:57:18 +00:00
DIHelper.TryAdd<ConfirmAuthHandler>();
2021-09-08 10:16:58 +00:00
DIHelper.TryAdd<CookieAuthHandler>();
2021-09-03 14:02:52 +00:00
DIHelper.TryAdd<WebhooksGlobalFilterAttribute>();
2020-10-19 19:04:07 +00:00
var redisConfiguration = Configuration.GetSection("Redis").Get<RedisConfiguration>();
var kafkaConfiguration = Configuration.GetSection("kafka").Get<KafkaSettings>();
if (kafkaConfiguration != null)
{
DIHelper.TryAdd(typeof(IEventBus<>), typeof(EventBusKafka<>));
}
else if (redisConfiguration != null)
{
DIHelper.TryAdd(typeof(IEventBus<>), typeof(EventBusRedis<>));
services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConfiguration);
}
else
{
DIHelper.TryAdd(typeof(IEventBus<>), typeof(EventBusMemoryCache<>));
}
2021-08-19 09:11:26 +00:00
DIHelper.TryAdd(typeof(IWebhookPublisher), typeof(WebhookPublisher));
2021-07-16 08:56:14 +00:00
if (LoadProducts)
{
DIHelper.RegisterProducts(Configuration, HostEnvironment.ContentRootPath);
}
2020-07-15 12:33:44 +00:00
2020-06-23 10:48:36 +00:00
var builder = services.AddMvcCore(config =>
{
config.Conventions.Add(new ControllerNameAttributeConvention());
2020-06-23 10:48:36 +00:00
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
2020-06-23 10:48:36 +00:00
config.Filters.Add(new AuthorizeFilter(policy));
config.Filters.Add(new TypeFilterAttribute(typeof(TenantStatusFilter)));
config.Filters.Add(new TypeFilterAttribute(typeof(PaymentFilter)));
config.Filters.Add(new TypeFilterAttribute(typeof(IpSecurityFilter)));
config.Filters.Add(new TypeFilterAttribute(typeof(ProductSecurityFilter)));
config.Filters.Add(new CustomResponseFilterAttribute());
config.Filters.Add(new CustomExceptionFilterAttribute());
config.Filters.Add(new TypeFilterAttribute(typeof(FormatFilter)));
2021-09-03 14:02:52 +00:00
config.Filters.Add(new TypeFilterAttribute(typeof(WebhooksGlobalFilterAttribute)));
2020-06-23 10:48:36 +00:00
config.OutputFormatters.RemoveType<XmlSerializerOutputFormatter>();
config.OutputFormatters.Add(new XmlOutputFormatter());
2020-07-15 12:33:44 +00:00
});
2020-10-19 19:04:07 +00:00
2020-07-15 12:33:44 +00:00
var authBuilder = services.AddAuthentication("cookie")
.AddScheme<AuthenticationSchemeOptions, CookieAuthHandler>("cookie", a => { });
2020-07-03 15:57:59 +00:00
2020-07-15 12:33:44 +00:00
if (ConfirmAddScheme)
{
authBuilder.AddScheme<AuthenticationSchemeOptions, ConfirmAuthHandler>("confirm", a => { });
}
2021-05-05 14:09:05 +00:00
services.AddAutoMapper(Assembly.GetAssembly(typeof(MappingProfile)));
2020-07-15 12:33:44 +00:00
}
public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
2020-06-23 10:48:36 +00:00
{
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
2020-07-15 12:33:44 +00:00
});
2020-06-23 10:48:36 +00:00
app.UseRouting();
2021-04-20 07:55:52 +00:00
if (AddAndUseSession)
app.UseSession();
2020-06-23 10:48:36 +00:00
app.UseAuthentication();
app.UseAuthorization();
app.UseCultureMiddleware();
app.UseDisposeMiddleware();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapCustom();
2021-06-16 13:54:36 +00:00
endpoints.MapHealthChecks("/health", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
endpoints.MapHealthChecks("/liveness", new HealthCheckOptions
{
Predicate = r => r.Name.Contains("self")
});
2020-06-23 10:48:36 +00:00
});
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.Register(Configuration, LoadProducts, LoadConsumers);
2020-06-23 10:48:36 +00:00
}
2021-08-08 12:56:54 +00:00
}
public static class LogNLogConfigureExtenstion
{
public static IHostBuilder ConfigureNLogLogging(this IHostBuilder hostBuilder)
{
return hostBuilder.ConfigureLogging((hostBuildexContext, r) =>
{
2021-09-23 17:58:39 +00:00
_ = new ConfigureLogNLog(hostBuildexContext.Configuration, new ConfigurationExtension(hostBuildexContext.Configuration), hostBuildexContext.HostingEnvironment);
2021-08-08 12:56:54 +00:00
r.AddNLog(LogManager.Configuration);
});
}
2020-06-23 10:48:36 +00:00
}
2020-07-03 11:47:27 +00:00
}