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

188 lines
6.7 KiB
C#
Raw Normal View History

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