mirror of
https://github.com/Caraxi/mare.server.git
synced 2025-12-12 15:07:23 +01:00
124 lines
4.7 KiB
C#
124 lines
4.7 KiB
C#
using System;
|
|
using MareSynchronos.API;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using MareSynchronosServer.Hubs;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Http.Connections;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using AspNetCoreRateLimit;
|
|
using Ben.Diagnostics;
|
|
using MareSynchronosShared.Authentication;
|
|
using MareSynchronosShared.Data;
|
|
using MareSynchronosShared.Protos;
|
|
|
|
namespace MareSynchronosServer
|
|
{
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddHttpContextAccessor();
|
|
|
|
services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
|
|
services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));
|
|
|
|
services.AddMemoryCache();
|
|
services.AddInMemoryRateLimiting();
|
|
|
|
services.AddSingleton<SystemInfoService, SystemInfoService>();
|
|
services.AddSingleton<IUserIdProvider, IdBasedUserIdProvider>();
|
|
services.AddTransient(_ => Configuration);
|
|
|
|
services.AddGrpcClient<AuthService.AuthServiceClient>(c =>
|
|
{
|
|
c.Address = new Uri(Configuration.GetValue<string>("ServiceAddress"));
|
|
});
|
|
services.AddGrpcClient<MetricsService.MetricsServiceClient>(c =>
|
|
{
|
|
c.Address = new Uri(Configuration.GetValue<string>("ServiceAddress"));
|
|
});
|
|
|
|
services.AddDbContextPool<MareDbContext>(options =>
|
|
{
|
|
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"), builder =>
|
|
{
|
|
builder.MigrationsHistoryTable("_efmigrationshistory", "public");
|
|
}).UseSnakeCaseNamingConvention();
|
|
options.EnableThreadSafetyChecks(false);
|
|
}, Configuration.GetValue("DbContextPoolSize", 1024));
|
|
|
|
services.AddHostedService(provider => provider.GetService<SystemInfoService>());
|
|
|
|
services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultScheme = SecretKeyGrpcAuthenticationHandler.AuthScheme;
|
|
})
|
|
.AddScheme<AuthenticationSchemeOptions, SecretKeyGrpcAuthenticationHandler>(SecretKeyGrpcAuthenticationHandler.AuthScheme, options => { });
|
|
services.AddAuthorization(options => options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build());
|
|
|
|
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
|
|
|
|
services.AddSignalR(hubOptions =>
|
|
{
|
|
hubOptions.MaximumReceiveMessageSize = long.MaxValue;
|
|
hubOptions.EnableDetailedErrors = true;
|
|
hubOptions.MaximumParallelInvocationsPerClient = 10;
|
|
hubOptions.StreamBufferCapacity = 200;
|
|
hubOptions.AddFilter<SignalRLimitFilter>();
|
|
});
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
app.UseBlockingDetection();
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
app.UseMigrationsEndPoint();
|
|
}
|
|
else
|
|
{
|
|
app.UseExceptionHandler("/Error");
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseIpRateLimiting();
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseWebSockets();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapHub<MareHub>(Api.Path, options =>
|
|
{
|
|
options.ApplicationMaxBufferSize = 5242880;
|
|
options.TransportMaxBufferSize = 5242880;
|
|
options.Transports = HttpTransportType.WebSockets;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|