DocSpace-buildtools/common/services/ASC.ApiSystem/Startup.cs

124 lines
4.8 KiB
C#
Raw Normal View History

/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL 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 more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 <EFBFBD> 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 <EFBFBD> 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
2020-02-18 13:48:38 +00:00
2020-10-29 14:30:08 +00:00
using ASC.ApiSystem.Controllers;
using StackExchange.Redis.Extensions.Newtonsoft;
2020-02-18 13:48:38 +00:00
namespace ASC.ApiSystem
{
2021-06-22 16:09:24 +00:00
public class Startup : BaseWorkerStartup
2020-02-18 13:48:38 +00:00
{
public IHostEnvironment HostEnvironment { get; }
2021-06-22 16:09:24 +00:00
public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment) : base(configuration)
2020-02-18 13:48:38 +00:00
{
HostEnvironment = hostEnvironment;
}
// This method gets called by the runtime. Use this method to add services to the container.
2021-06-22 16:09:24 +00:00
public override void ConfigureServices(IServiceCollection services)
{
2021-06-22 16:09:24 +00:00
base.ConfigureServices(services);
var diHelper = new DIHelper(services);
2020-10-12 19:39:23 +00:00
services.AddHttpContextAccessor();
2021-01-13 09:41:29 +00:00
services.AddMemoryCache();
2020-02-18 13:48:38 +00:00
2020-10-12 19:39:23 +00:00
services.AddControllers()
2020-07-09 08:08:42 +00:00
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.WriteIndented = false;
2021-10-01 10:26:36 +00:00
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
2020-07-09 08:08:42 +00:00
});
2020-02-18 13:48:38 +00:00
2020-10-29 14:30:08 +00:00
services.AddMemoryCache();
var redisConfiguration = Configuration.GetSection("Redis").Get<RedisConfiguration>();
var kafkaConfiguration = Configuration.GetSection("kafka").Get<KafkaSettings>();
if (kafkaConfiguration != null)
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
}
else if (redisConfiguration != null)
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>));
}
else
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>));
}
2020-10-29 14:30:08 +00:00
diHelper.TryAdd<AuthHandler>();
diHelper.TryAdd<CalDavController>();
diHelper.TryAdd<PortalController>();
diHelper.TryAdd<SettingsController>();
diHelper.TryAdd<TariffController>();
2020-02-18 13:48:38 +00:00
2020-10-12 19:39:23 +00:00
services.AddAuthentication()
.AddScheme<AuthenticationSchemeOptions, AuthHandler>("auth.allowskip", _ => { })
.AddScheme<AuthenticationSchemeOptions, AuthHandler>("auth.allowskip.registerportal", _ => { });
services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(Configuration.GetSection("Redis").Get<RedisConfiguration>());
2020-02-18 13:48:38 +00:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2021-06-22 16:09:24 +00:00
public override void Configure(IApplicationBuilder app)
{
base.Configure(app);
2020-02-18 13:48:38 +00:00
2020-10-12 19:39:23 +00:00
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
2020-02-18 13:48:38 +00:00
2020-10-12 19:39:23 +00:00
app.UseCors(builder =>
2020-02-18 13:48:38 +00:00
builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
2020-10-12 19:39:23 +00:00
app.UseRouting();
2020-02-18 13:48:38 +00:00
2020-10-12 19:39:23 +00:00
app.UseAuthentication();
2020-02-18 13:48:38 +00:00
2020-10-12 19:39:23 +00:00
app.UseAuthorization();
2020-02-18 13:48:38 +00:00
2020-10-12 19:39:23 +00:00
app.UseEndpoints(endpoints =>
2020-02-18 13:48:38 +00:00
{
2020-10-12 19:39:23 +00:00
endpoints.MapControllers();
2020-02-18 13:48:38 +00:00
});
}
2020-11-02 16:27:08 +00:00
public void ConfigureContainer(ContainerBuilder builder)
{
2021-08-11 18:32:43 +00:00
builder.Register(Configuration, false, false);
2020-02-18 13:48:38 +00:00
}
}
}