oliverbooth.dev/OliverBooth/Program.cs

86 lines
2.8 KiB
C#
Raw Normal View History

2024-02-29 18:10:04 +00:00
using System.Security.Authentication;
2024-02-25 15:40:58 +00:00
using Asp.Versioning;
using AspNetCore.ReCaptcha;
2024-02-29 18:10:04 +00:00
using FluentFTP;
using FluentFTP.Logging;
using OliverBooth.Common.Extensions;
2023-08-06 15:56:08 +01:00
using OliverBooth.Services;
2023-08-12 21:06:48 +01:00
using Serilog;
2024-02-29 18:10:04 +00:00
using Serilog.Extensions.Logging;
2023-08-12 21:06:48 +01:00
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("logs/latest.log", rollingInterval: RollingInterval.Day)
#if DEBUG
.MinimumLevel.Debug()
#endif
2023-08-12 21:06:48 +01:00
.CreateLogger();
2023-08-06 15:56:08 +01:00
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddTomlFile("data/config.toml", true, true);
2023-08-06 15:56:08 +01:00
builder.Logging.ClearProviders();
2023-08-12 21:06:48 +01:00
builder.Logging.AddSerilog();
2023-08-12 20:40:46 +01:00
2024-02-25 15:40:58 +00:00
builder.Services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1);
options.ReportApiVersions = true;
options.ApiVersionReader = new UrlSegmentApiVersionReader();
});
builder.Services.AddCommonServices();
2024-02-23 03:23:57 +00:00
builder.Services.AddHttpClient();
2024-02-29 18:10:04 +00:00
builder.Services.AddTransient<IAsyncFtpClient, AsyncFtpClient>(provider =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
string? host = configuration["Cdn:Ftp:Host"];
string? username = configuration["Cdn:Ftp:Username"];
string? password = configuration["Cdn:Ftp:Password"];
if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
{
throw new AuthenticationException("Configuration value missing for CDN FTP.");
}
var client = new AsyncFtpClient(host, username, password);
var loggerFactory = new SerilogLoggerFactory(Log.Logger);
client.Logger = new FtpLogAdapter(loggerFactory.CreateLogger("FTP"));
return client;
});
builder.Services.AddSingleton<ICdnService, CdnService>();
2024-02-23 03:23:57 +00:00
builder.Services.AddSingleton<IMastodonService, MastodonService>();
2023-08-06 15:55:12 +01:00
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
2023-08-05 21:01:47 +01:00
builder.Services.AddControllersWithViews();
2024-02-27 19:49:42 +00:00
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddServerSideBlazor().AddInteractiveServerComponents();
builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.Services.AddReCaptcha(builder.Configuration.GetSection("ReCaptcha"));
2023-05-26 19:05:44 +01:00
if (builder.Environment.IsProduction())
{
builder.WebHost.AddCertificateFromEnvironment(2845, 5049);
}
2023-08-10 23:33:15 +01:00
WebApplication app = builder.Build();
2023-05-26 19:05:44 +01:00
if (!app.Environment.IsDevelopment())
{
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.UseHttpsRedirection();
2023-09-25 20:06:05 +01:00
app.UseStatusCodePagesWithRedirects("/error/{0}");
2023-05-26 19:05:44 +01:00
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
2023-08-05 20:58:03 +01:00
2023-08-05 21:01:47 +01:00
app.MapControllers();
2023-05-26 19:05:44 +01:00
app.MapRazorPages();
2024-02-27 19:49:42 +00:00
app.MapBlazorHub();
2023-05-26 19:05:44 +01:00
app.Run();