diff --git a/FictionArchive.Service.ReportingService/Program.cs b/FictionArchive.Service.ReportingService/Program.cs index 6d98235..4f43e86 100644 --- a/FictionArchive.Service.ReportingService/Program.cs +++ b/FictionArchive.Service.ReportingService/Program.cs @@ -1,3 +1,10 @@ +using FictionArchive.Common.Extensions; +using FictionArchive.Service.ReportingService.Consumers; +using FictionArchive.Service.ReportingService.Services; +using FictionArchive.Service.ReportingService.GraphQL; +using FictionArchive.Service.Shared; +using FictionArchive.Service.Shared.Extensions; + namespace FictionArchive.Service.ReportingService; public class Program @@ -5,8 +12,68 @@ public class Program public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); + + var isSchemaExport = SchemaExportDetector.IsSchemaExportMode(args); + + builder.AddLocalAppsettings(); + + builder.Services.AddHealthChecks(); + + #region MassTransit + + if (!isSchemaExport) + { + builder.Services.AddFictionArchiveMassTransit( + builder.Configuration, + x => + { + x.AddConsumer(); + }); + } + + #endregion + + #region GraphQL + + builder.Services.AddGraphQLServer() + .AddQueryConventions() + .AddTypeExtension(typeof(JobQueries)) + .ApplySaneDefaults() + .AddAuthorization(); + + #endregion + + #region Database + + builder.Services.RegisterDbContext( + builder.Configuration.GetConnectionString("DefaultConnection"), + skipInfrastructure: isSchemaExport); + + #endregion + + // Authentication & Authorization + builder.Services.AddOidcAuthentication(builder.Configuration); + builder.Services.AddFictionArchiveAuthorization(); + var app = builder.Build(); - app.MapGet("/healthz", () => "ok"); - app.Run(); + + // Update database (skip in schema export mode) + if (!isSchemaExport) + { + using var scope = app.Services.CreateScope(); + var dbContext = scope.ServiceProvider.GetRequiredService(); + dbContext.UpdateDatabase(); + } + + app.UseHttpsRedirection(); + + app.MapHealthChecks("/healthz"); + + app.UseAuthentication(); + app.UseAuthorization(); + + app.MapGraphQL(); + + app.RunWithGraphQLCommands(args); } }