2022-03-15 18:00:53 +00:00
|
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
|
//
|
|
|
|
|
// This program is a free software product.
|
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
|
|
|
|
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
|
|
|
|
// any third-party rights.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
//
|
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
|
//
|
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
|
//
|
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
|
//
|
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
|
|
|
|
|
|
|
|
using JsonConverter = System.Text.Json.Serialization.JsonConverter;
|
2021-12-21 16:16:34 +00:00
|
|
|
|
|
2022-02-03 13:19:48 +00:00
|
|
|
|
namespace ASC.Api.Core;
|
2021-12-21 16:16:34 +00:00
|
|
|
|
|
2022-02-03 13:19:48 +00:00
|
|
|
|
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; }
|
2022-03-17 14:33:16 +00:00
|
|
|
|
public virtual bool AddControllersAsServices { get; }
|
|
|
|
|
public virtual bool ConfirmAddScheme { get; }
|
|
|
|
|
public virtual bool AddAndUseSession { get; }
|
2022-03-03 11:04:17 +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;
|
|
|
|
|
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-02-07 16:03:16 +00:00
|
|
|
|
{
|
2022-03-03 11:04:17 +00:00
|
|
|
|
services.AddSession();
|
2022-02-07 16:03:16 +00:00
|
|
|
|
}
|
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)
|
2021-07-27 09:43:01 +00:00
|
|
|
|
{
|
2022-03-03 11:04:17 +00:00
|
|
|
|
foreach (var c in Converters)
|
|
|
|
|
{
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(c);
|
2022-01-13 11:24:14 +00:00
|
|
|
|
}
|
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>();
|
|
|
|
|
|
2022-04-01 16:12:53 +00:00
|
|
|
|
services.AddDistributedCache(Configuration);
|
2022-03-09 09:10:30 +00:00
|
|
|
|
services.AddEventBus(Configuration);
|
2022-03-26 13:31:13 +00:00
|
|
|
|
services.AddDistributedTaskQueue();
|
2022-04-01 16:12:53 +00:00
|
|
|
|
services.AddCacheNotify(Configuration);
|
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-22 12:10:05 +00:00
|
|
|
|
services.AddOptions();
|
|
|
|
|
|
2022-03-03 11:04:17 +00:00
|
|
|
|
services.AddMvcCore(config =>
|
|
|
|
|
{
|
|
|
|
|
config.Conventions.Add(new ControllerNameAttributeConvention());
|
2022-01-13 11:24:14 +00:00
|
|
|
|
|
2022-03-03 11:04:17 +00:00
|
|
|
|
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
|
2022-01-27 12:20:51 +00:00
|
|
|
|
|
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-01-13 11:24:14 +00:00
|
|
|
|
|
2022-03-03 11:04:17 +00:00
|
|
|
|
config.OutputFormatters.RemoveType<XmlSerializerOutputFormatter>();
|
|
|
|
|
config.OutputFormatters.Add(new XmlOutputFormatter());
|
|
|
|
|
});
|
2021-01-26 09:29:50 +00:00
|
|
|
|
|
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-18 08:51:33 +00:00
|
|
|
|
services.AddAutoMapper(typeof(MappingProfile));
|
2022-03-03 11:04:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
2022-02-07 16:03:16 +00:00
|
|
|
|
{
|
2022-03-03 11:04:17 +00:00
|
|
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
|
|
if (AddAndUseSession)
|
|
|
|
|
{
|
|
|
|
|
app.UseSession();
|
2021-01-26 09:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-03 11:04:17 +00:00
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.UseCultureMiddleware();
|
|
|
|
|
|
|
|
|
|
app.UseDisposeMiddleware();
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
2021-01-26 09:29:50 +00:00
|
|
|
|
{
|
2022-03-03 11:04:17 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2022-02-03 13:19:48 +00:00
|
|
|
|
}
|
2021-08-08 12:56:54 +00:00
|
|
|
|
|
2022-02-03 13:19:48 +00:00
|
|
|
|
public static class LogNLogConfigureExtenstion
|
|
|
|
|
{
|
2022-03-17 06:55:08 +00:00
|
|
|
|
private static LoggingConfiguration GetXmlLoggingConfiguration(IHostEnvironment hostEnvironment,IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var loggerConfiguration = new XmlLoggingConfiguration(CrossPlatform.PathCombine(configuration["pathToConf"], "nlog.config"));
|
|
|
|
|
|
|
|
|
|
var settings = new ConfigurationExtension(configuration).GetSetting<NLogSettings>("log");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(settings.Name))
|
|
|
|
|
{
|
|
|
|
|
loggerConfiguration.Variables["name"] = settings.Name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(settings.Dir))
|
|
|
|
|
{
|
|
|
|
|
loggerConfiguration.Variables["dir"] = CrossPlatform.PathCombine(hostEnvironment.ContentRootPath, settings.Dir)
|
|
|
|
|
.TrimEnd('/').TrimEnd('\\') + Path.DirectorySeparatorChar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return loggerConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
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-17 06:55:08 +00:00
|
|
|
|
r.Services.ConfigureOptions<ConfigureLogNLog>();
|
|
|
|
|
r.Services.AddSingleton(resolver => (IOptionsMonitor<ILog>)resolver.GetRequiredService<IOptionsMonitor<LogNLog>>());
|
|
|
|
|
r.Services.AddSingleton(resolver => (ILog)resolver.GetRequiredService<IOptionsMonitor<LogNLog>>().CurrentValue);
|
2022-03-17 20:09:45 +00:00
|
|
|
|
r.Services.AddSingleton(typeof(ILog<>), typeof(Common.Logging.LogFactory<>));
|
2022-03-17 06:55:08 +00:00
|
|
|
|
|
|
|
|
|
LogManager.ThrowConfigExceptions = false;
|
|
|
|
|
|
|
|
|
|
r.AddNLog(GetXmlLoggingConfiguration(hostBuildexContext.HostingEnvironment, hostBuildexContext.Configuration));
|
2022-03-03 11:04:17 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2020-07-03 11:47:27 +00:00
|
|
|
|
}
|