#region ========================================================================= USING ===================================================================================== using ErrorOr; using FastEndpoints; using Lumina.Application.Common.DependencyInjection; using Lumina.DataAccess.Common.DependencyInjection; using Lumina.DataAccess.Core.UoW; using Lumina.Domain.Common.DependencyInjection; using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services; using Lumina.Infrastructure.Common.DependencyInjection; using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Progress; using Lumina.Presentation.Api.Common.DependencyInjection; using Lumina.Presentation.Api.Common.Middlewares; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging; using Scalar.AspNetCore; using Serilog; using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Threading.Tasks; #endregion namespace Lumina.Presentation.Api; /// /// Application entry point, contains the composition root module, wires up all dependencies of the application. /// [ExcludeFromCodeCoverage] public class Program { /// /// Application entry point. /// /// Optional command line arguments. public static async Task Main(string[] args) { WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.BindSharedConfiguration(builder.Configuration); builder.Services.BindPresentationApiLayerConfiguration(builder.Configuration); builder.Services.AddPresentationApiLayerServices(builder.Configuration); builder.Services.AddApplicationLayerServices(); builder.Services.AddInfrastructureLayerServices(); builder.Services.AddDataAccessLayerServices(); builder.Services.AddDomainLayerServices(); // determine log path based on environment string logPath; if (Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true") { logPath = Environment.GetEnvironmentVariable("LOG_PATH") ?? "/logs"; // use docker volume path builder.WebHost.ConfigureKestrel(options => { options.ListenAnyIP(5214); // HTTP only; also, the port is also the same port exposed in the Dockerfile }); } else logPath = Path.Combine(AppContext.BaseDirectory, "logs"); // use local binary path Directory.CreateDirectory(logPath); if (!logPath.EndsWith(Path.DirectorySeparatorChar.ToString())) logPath = logPath += Path.DirectorySeparatorChar; // set environment variable for Serilog configuration Environment.SetEnvironmentVariable("LOG_PATH", logPath); builder.Host.UseSerilog((context, services, configuration) => { configuration.ReadFrom.Configuration(context.Configuration) .ReadFrom.Services(services) .Enrich.FromLogContext(); }); WebApplication app = builder.Build(); app.UseSerilogRequestLogging(); // apply any pending migrations using (IServiceScope scope = app.Services.CreateScope()) { IServiceProvider services = scope.ServiceProvider; try { LuminaDbContext context = services.GetRequiredService(); await context.Database.MigrateAsync(); // enable the Write-Ahead Logging journal mode and a busy timeout, so that the Entity Framework connection and the dedicated connections // used by the bulk data access operations of Dapper can access the database concurrently, without database locking errors SqliteConnection sqliteConnection = new(context.Database.GetDbConnection().ConnectionString); sqliteConnection.Open(); using SqliteCommand sqliteCommand = sqliteConnection.CreateCommand(); sqliteCommand.CommandText = "PRAGMA journal_mode=WAL; PRAGMA busy_timeout=30000;"; sqliteCommand.ExecuteNonQuery(); sqliteConnection.Close(); } catch (Exception ex) { ILogger logger = services.GetRequiredService>(); logger.LogCritical(ex, "Failed to run database migrations"); } } app.UseRouting(); app.UseCors("SecurePolicy"); //app.UseExceptionHandler("/error"); // uses a middleware which re-executes the request to the error path //app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); app.UseRateLimiter(); app.UseFastEndpoints(config => { config.Endpoints.RoutePrefix = "api"; config.Versioning.Prefix = "v"; config.Versioning.DefaultVersion = 1; config.Versioning.PrependToRoute = true; config.Endpoints.ShortNames = true; }); // add API documentation (OpenApi/Scalar) app.MapOpenApi(); app.UseOpenApi(openApiDocumentMiddlewareSettings => openApiDocumentMiddlewareSettings.Path = "/openapi/{documentName}.json"); // this is needed for API versioning to work correctly with Scalar app.MapScalarApiReference(scalarOptions => { scalarOptions.WithTitle("Lumina API") .WithTheme(ScalarTheme.BluePlanet) .WithDefaultHttpClient(ScalarTarget.CSharp, ScalarClient.HttpClient) //.WithFavicon() // TODO: enable when favicon is added .WithDotNetFlag(true); }); // add the middleware that fires domain events and ensures eventual transactional consistency, but NOT for long-polling/WebSockets stuff like SignalR, which would keep the db locked app.UseWhen(ctx => !ctx.Request.Path.StartsWithSegments("/scanProgressHub"), app => app.UseMiddleware()); // create a directory relative to the application's startup directory, and use it to store static files that are served at the /media route on the API string mediaRootDirectoryPathSetting = app.Configuration.GetValue("MediaSettings:RootDirectory") ?? string.Empty; string mediaRootPath = Path.Combine(AppContext.BaseDirectory, mediaRootDirectoryPathSetting); if (!Directory.Exists(mediaRootPath)) Directory.CreateDirectory(mediaRootPath); // ensure the default file system structure is present using (IServiceScope scope = app.Services.CreateScope()) { IServiceProvider services = scope.ServiceProvider; IFileSystemStructureSeedService context = services.GetRequiredService(); ErrorOr setDefaultDirectoriesResult = context.SetDefaultDirectories(mediaRootPath); if (setDefaultDirectoriesResult.IsError) throw new InvalidOperationException("Could not create default file system directories structure: " + setDefaultDirectoriesResult.FirstError.Description); } app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(mediaRootPath), RequestPath = "/media" }); app.MapHub("/scanProgressHub"); await app.RunAsync().ConfigureAwait(false); } }