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

188 lines
6.7 KiB
C#
Raw Normal View History

2021-05-05 14:09:05 +00:00
using System.Reflection;
using System.Text.Json.Serialization;
2020-07-15 12:33:44 +00:00
using ASC.Api.Core.Auth;
using ASC.Api.Core.Convention;
2020-07-15 12:33:44 +00:00
using ASC.Api.Core.Core;
2020-06-23 10:48:36 +00:00
using ASC.Api.Core.Middleware;
2020-07-15 12:33:44 +00:00
using ASC.Common;
2020-10-19 19:04:07 +00:00
using ASC.Common.Caching;
using ASC.Common.DependencyInjection;
using ASC.Common.Logging;
2021-05-05 14:09:05 +00:00
using ASC.Common.Mapping;
2021-08-08 12:56:54 +00:00
using ASC.Common.Utils;
2021-08-26 18:43:41 +00:00
using ASC.Webhooks.Core;
using Autofac;
2020-07-15 12:33:44 +00:00
2021-06-16 13:54:36 +00:00
using HealthChecks.UI.Client;
2020-07-15 12:33:44 +00:00
using Microsoft.AspNetCore.Authentication;
2020-06-23 10:48:36 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
2021-06-16 13:54:36 +00:00
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
2020-07-15 12:33:44 +00:00
using Microsoft.AspNetCore.Hosting;
2020-06-23 10:48:36 +00:00
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Mvc.Formatters;
2020-07-15 12:33:44 +00:00
using Microsoft.Extensions.Configuration;
2020-06-23 10:48:36 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2020-06-23 10:48:36 +00:00
2021-08-08 12:56:54 +00:00
using NLog;
using NLog.Extensions.Logging;
2020-07-03 11:47:27 +00:00
namespace ASC.Api.Core
2020-06-23 10:48:36 +00:00
{
public abstract class BaseStartup
{
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; } = 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();
2020-07-15 12:33:44 +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-04-20 07:55:52 +00:00
if(AddAndUseSession)
services.AddSession();
2020-10-18 16:34:30 +00:00
DIHelper.Configure(services);
2020-07-15 12:33:44 +00:00
services.AddControllers()
.AddXmlSerializerFormatters()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.WriteIndented = false;
options.JsonSerializerOptions.IgnoreNullValues = true;
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)
2020-07-06 07:56:17 +00:00
{
2021-06-22 16:55:47 +00:00
foreach (var c in Converters)
2020-07-15 12:33:44 +00:00
{
2021-06-22 16:55:47 +00:00
options.JsonSerializerOptions.Converters.Add(c);
2020-07-15 12:33:44 +00:00
}
2021-06-22 16:55:47 +00:00
}
});
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>();
2020-10-19 19:04:07 +00:00
2020-10-21 11:52:44 +00:00
DIHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
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());
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
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)));
2020-06-23 10:48:36 +00:00
config.Filters.Add(new CustomResponseFilterAttribute());
config.Filters.Add(new CustomExceptionFilterAttribute());
config.Filters.Add(new TypeFilterAttribute(typeof(FormatFilter)));
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.UseWebhooksMiddleware();
2020-06-23 10:48:36 +00:00
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) =>
{
_ = new ConfigureLogNLog(hostBuildexContext.Configuration, new ConfigurationExtension(hostBuildexContext.Configuration));
r.AddNLog(LogManager.Configuration);
});
}
2020-06-23 10:48:36 +00:00
}
2020-07-03 11:47:27 +00:00
}