2021-09-03 14:02:52 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
2021-05-05 14:09:05 +00:00
|
|
|
|
using System.Text.Json.Serialization;
|
2020-07-15 12:33:44 +00:00
|
|
|
|
|
|
|
|
|
using ASC.Api.Core.Auth;
|
2021-04-02 15:46:00 +00:00
|
|
|
|
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;
|
2021-01-26 09:29:50 +00:00
|
|
|
|
using ASC.Common.DependencyInjection;
|
2021-08-08 12:56:54 +00:00
|
|
|
|
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;
|
2021-01-26 09:29:50 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2021-08-08 12:56:54 +00:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
|
|
|
|
using NLog;
|
|
|
|
|
using NLog.Extensions.Logging;
|
|
|
|
|
|
2021-12-21 16:16:34 +00:00
|
|
|
|
using StackExchange.Redis.Extensions.Core.Configuration;
|
2022-01-27 12:20:51 +00:00
|
|
|
|
using StackExchange.Redis.Extensions.Newtonsoft;
|
2021-12-21 16:16:34 +00:00
|
|
|
|
|
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
|
|
|
|
{
|
2020-07-07 20:00:39 +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; }
|
2021-10-25 11:29:36 +00:00
|
|
|
|
protected bool LoadProducts { get; set; } = true;
|
2021-06-24 08:15:20 +00:00
|
|
|
|
protected bool LoadConsumers { get; } = true;
|
2020-07-07 20:00:39 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2022-01-13 11:24:14 +00:00
|
|
|
|
}
|
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();
|
2022-01-25 10:47:36 +00:00
|
|
|
|
services.AddHttpClient();
|
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 =>
|
2021-07-27 09:43:01 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
2022-01-13 11:24:14 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2021-09-03 14:02:52 +00:00
|
|
|
|
|
2021-07-27 09:43:01 +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
|
|
|
|
|
2022-01-13 11:24:14 +00:00
|
|
|
|
var redisConfiguration = Configuration.GetSection("Redis").Get<RedisConfiguration>();
|
2022-01-27 12:20:51 +00:00
|
|
|
|
var kafkaConfiguration = Configuration.GetSection("kafka").Get<KafkaSettings>();
|
2022-01-13 11:24:14 +00:00
|
|
|
|
|
2022-01-27 12:20:51 +00:00
|
|
|
|
if (kafkaConfiguration != null)
|
|
|
|
|
{
|
|
|
|
|
DIHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
|
|
|
|
|
}
|
|
|
|
|
else if (redisConfiguration != null)
|
2022-01-13 11:24:14 +00:00
|
|
|
|
{
|
|
|
|
|
DIHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>));
|
2022-01-27 12:20:51 +00:00
|
|
|
|
|
|
|
|
|
services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConfiguration);
|
2022-01-13 11:24:14 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DIHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-19 09:11:26 +00:00
|
|
|
|
DIHelper.TryAdd(typeof(IWebhookPublisher), typeof(WebhookPublisher));
|
2021-01-26 09:29:50 +00:00
|
|
|
|
|
2021-07-16 08:56:14 +00:00
|
|
|
|
if (LoadProducts)
|
|
|
|
|
{
|
|
|
|
|
DIHelper.RegisterProducts(Configuration, HostEnvironment.ContentRootPath);
|
|
|
|
|
}
|
2020-07-15 12:33:44 +00:00
|
|
|
|
|
2022-01-21 13:15:15 +00:00
|
|
|
|
services.AddMvcCore(config =>
|
2020-06-23 10:48:36 +00:00
|
|
|
|
{
|
2021-04-02 15:46:00 +00:00
|
|
|
|
config.Conventions.Add(new ControllerNameAttributeConvention());
|
|
|
|
|
|
2020-06-23 10:48:36 +00:00
|
|
|
|
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
|
2021-04-02 15:46:00 +00:00
|
|
|
|
|
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 => { });
|
2020-07-07 20:00:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 14:09:05 +00:00
|
|
|
|
services.AddAutoMapper(Assembly.GetAssembly(typeof(MappingProfile)));
|
2020-07-15 12:33:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 20:00:39 +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
|
|
|
|
});
|
2021-01-26 09:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ConfigureContainer(ContainerBuilder builder)
|
|
|
|
|
{
|
2021-06-24 08:15:20 +00:00
|
|
|
|
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
|
|
|
|
}
|