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

143 lines
5.1 KiB
C#
Raw Normal View History

2020-07-15 12:33:44 +00:00
using System.Text.Json.Serialization;
using ASC.Api.Core.Auth;
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;
using Autofac;
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;
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
2020-07-03 11:47:27 +00:00
namespace ASC.Api.Core
2020-06-23 10:48:36 +00:00
{
public abstract class BaseStartup
2020-06-23 10:48:36 +00:00
{
public IConfiguration Configuration { get; }
public IHostEnvironment HostEnvironment { get; }
2020-07-15 12:33:44 +00:00
public virtual string[] LogParams { get; }
public virtual JsonConverter[] Converters { get; }
public virtual bool AddControllers { get; } = true;
2020-10-18 16:34:30 +00:00
public virtual bool ConfirmAddScheme { get; } = false;
protected DIHelper DIHelper { get; }
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
{
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
2020-10-18 16:34:30 +00:00
DIHelper.Configure(services);
2020-07-15 12:33:44 +00:00
if (AddControllers)
2020-07-06 07:56:17 +00:00
{
services.AddControllers()
.AddXmlSerializerFormatters()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.WriteIndented = false;
options.JsonSerializerOptions.IgnoreNullValues = true;
2020-07-15 12:33:44 +00:00
options.JsonSerializerOptions.Converters.Add(new ApiDateTimeConverter());
if (Converters != null)
{
foreach (var c in Converters)
{
options.JsonSerializerOptions.Converters.Add(c);
}
}
2020-07-06 07:56:17 +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<>));
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 =>
{
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)));
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 => { });
}
2020-07-15 12:33:44 +00:00
if (LogParams != null)
{
2020-10-29 08:13:51 +00:00
LogNLogExtension.ConfigureLog(DIHelper, LogParams);
}
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();
app.UseAuthentication();
app.UseAuthorization();
app.UseCultureMiddleware();
app.UseDisposeMiddleware();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapCustom();
});
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.Register(Configuration);
2020-06-23 10:48:36 +00:00
}
}
2020-07-03 11:47:27 +00:00
}