2024-05-28 15:29:18 +02:00
|
|
|
using Foxnouns.Backend;
|
2024-05-27 15:53:54 +02:00
|
|
|
using Foxnouns.Backend.Database;
|
|
|
|
using Serilog;
|
|
|
|
using Foxnouns.Backend.Extensions;
|
2024-07-08 19:03:04 +02:00
|
|
|
using Foxnouns.Backend.Middleware;
|
2024-06-12 03:47:20 +02:00
|
|
|
using Foxnouns.Backend.Services;
|
2024-07-08 19:03:04 +02:00
|
|
|
using Hangfire;
|
|
|
|
using Hangfire.Redis.StackExchange;
|
2024-05-28 15:29:18 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-07-08 19:03:04 +02:00
|
|
|
using Minio;
|
2024-05-28 17:09:50 +02:00
|
|
|
using Newtonsoft.Json;
|
2024-05-27 15:53:54 +02:00
|
|
|
using Newtonsoft.Json.Serialization;
|
2024-07-08 19:03:04 +02:00
|
|
|
using Sentry.Extensibility;
|
|
|
|
using Sentry.Hangfire;
|
2024-05-27 15:53:54 +02:00
|
|
|
|
2024-06-04 17:43:48 +02:00
|
|
|
// Read version information from .version in the repository root
|
|
|
|
await BuildInfo.ReadBuildInfo();
|
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
var config = builder.AddConfiguration();
|
|
|
|
|
2024-05-28 17:09:50 +02:00
|
|
|
builder.AddSerilog();
|
2024-05-27 15:53:54 +02:00
|
|
|
|
2024-07-08 19:03:04 +02:00
|
|
|
builder.WebHost.UseSentry(opts =>
|
|
|
|
{
|
|
|
|
opts.Dsn = config.Logging.SentryUrl;
|
|
|
|
opts.TracesSampleRate = config.Logging.SentryTracesSampleRate;
|
|
|
|
opts.MaxRequestBodySize = RequestSize.Small;
|
|
|
|
});
|
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
builder.Services
|
|
|
|
.AddControllers()
|
|
|
|
.AddNewtonsoftJson(options =>
|
|
|
|
options.SerializerSettings.ContractResolver = new DefaultContractResolver
|
|
|
|
{
|
|
|
|
NamingStrategy = new SnakeCaseNamingStrategy()
|
2024-05-28 15:29:18 +02:00
|
|
|
})
|
|
|
|
.ConfigureApiBehaviorOptions(options =>
|
|
|
|
{
|
|
|
|
options.InvalidModelStateResponseFactory = actionContext => new BadRequestObjectResult(
|
|
|
|
new ApiError.BadRequest("Bad request", actionContext.ModelState).ToJson()
|
|
|
|
);
|
|
|
|
});
|
2024-05-27 15:53:54 +02:00
|
|
|
|
2024-05-28 17:09:50 +02:00
|
|
|
// Set the default converter to snake case as we use it in a couple places.
|
|
|
|
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
|
|
|
|
{
|
|
|
|
ContractResolver = new DefaultContractResolver
|
|
|
|
{
|
|
|
|
NamingStrategy = new SnakeCaseNamingStrategy()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
builder.Services
|
|
|
|
.AddDbContext<DatabaseContext>()
|
2024-05-28 15:29:18 +02:00
|
|
|
.AddCustomServices()
|
|
|
|
.AddCustomMiddleware()
|
2024-05-27 15:53:54 +02:00
|
|
|
.AddEndpointsApiExplorer()
|
2024-07-08 19:03:04 +02:00
|
|
|
.AddSwaggerGen()
|
|
|
|
.AddMinio(c =>
|
|
|
|
c.WithEndpoint(config.Storage.Endpoint)
|
|
|
|
.WithCredentials(config.Storage.AccessKey, config.Storage.SecretKey)
|
|
|
|
.Build());
|
|
|
|
|
|
|
|
builder.Services.AddHangfire(c => c.UseSentry().UseRedisStorage(config.Jobs.Redis, new RedisStorageOptions
|
|
|
|
{
|
|
|
|
Prefix = "foxnouns_"
|
|
|
|
}))
|
|
|
|
.AddHangfireServer(options => { options.WorkerCount = config.Jobs.Workers; });
|
2024-05-27 15:53:54 +02:00
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
2024-05-28 15:29:18 +02:00
|
|
|
await app.Initialize(args);
|
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
app.UseSerilogRequestLogging();
|
|
|
|
app.UseRouting();
|
2024-07-08 19:03:04 +02:00
|
|
|
// Not all environments will want tracing (from experience, it's expensive to use in production, even with a low sample rate),
|
|
|
|
// so it's locked behind a config option.
|
|
|
|
if (config.Logging.SentryTracing) app.UseSentryTracing();
|
2024-05-27 15:53:54 +02:00
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
app.UseCors();
|
2024-05-28 15:29:18 +02:00
|
|
|
app.UseCustomMiddleware();
|
2024-05-27 15:53:54 +02:00
|
|
|
app.MapControllers();
|
|
|
|
|
2024-07-08 19:03:04 +02:00
|
|
|
app.UseHangfireDashboard("/hangfire", new DashboardOptions
|
|
|
|
{
|
|
|
|
AppPath = null,
|
|
|
|
AsyncAuthorization = [new HangfireDashboardAuthorizationFilter(app.Services)]
|
|
|
|
});
|
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
app.Urls.Clear();
|
|
|
|
app.Urls.Add(config.Address);
|
|
|
|
|
2024-06-12 03:47:20 +02:00
|
|
|
// Fire off the periodic tasks loop in the background
|
|
|
|
_ = new Timer(_ =>
|
|
|
|
{
|
|
|
|
var __ = RunPeriodicTasksAsync();
|
|
|
|
}, null, TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(1));
|
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
app.Run();
|
2024-06-12 03:47:20 +02:00
|
|
|
Log.CloseAndFlush();
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
async Task RunPeriodicTasksAsync()
|
|
|
|
{
|
|
|
|
await using var scope = app.Services.CreateAsyncScope();
|
|
|
|
var logger = scope.ServiceProvider.GetRequiredService<ILogger>();
|
|
|
|
logger.Debug("Running periodic tasks");
|
2024-05-27 15:53:54 +02:00
|
|
|
|
2024-06-12 03:47:20 +02:00
|
|
|
var keyCacheSvc = scope.ServiceProvider.GetRequiredService<KeyCacheService>();
|
|
|
|
await keyCacheSvc.DeleteExpiredKeysAsync();
|
|
|
|
}
|