using Microsoft.EntityFrameworkCore; using System.Reflection; using Microsoft.OpenApi.Models; using ZenithInfo.Components; using ZenithInfo.Models; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Components.QuickGrid.EntityFrameworkAdapter; using ZenithInfo.Data; var builder = WebApplication.CreateBuilder(args); // Configuration var config = builder.Configuration.GetSection("Configuration").Get(); builder.Services.Configure(builder.Configuration.GetSection("Configuration")); if (config?.GlobalToken == "CHANGE-ME") { throw new InvalidOperationException( "Global token is not a valid value, please set the `CONFIGURATION__GLOBALTOKEN` environment variable to a random value. `openssl rand -hex 64` is a good way to generate a random value." ); } // Initialize Database builder.Services.AddQuickGridEntityFrameworkAdapter(); builder.Services.AddDbContextFactory(options => options.UseNpgsql(builder.Configuration.GetConnectionString("ZenithInfoContext"))); // Sentry if (config?.UseSentry == true) { builder.WebHost.UseSentry(); } // Add services to the container. builder.Services.AddRazorComponents().AddInteractiveServerComponents(); builder.Services.AddHttpLogging(o => { }); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(opts => { opts.SwaggerDoc("v1", new OpenApiInfo { Title = "Zenith Info API", Version = "v1" }); opts.AddSecurityDefinition( "Bearer", new OpenApiSecurityScheme { Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, Scheme = "Bearer", } ); opts.AddSecurityRequirement( new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer", }, Scheme = "oauth2", Name = "Bearer", In = ParameterLocation.Header, }, new List() }, } ); var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; opts.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFile)); }); var app = builder.Build(); if (config?.UseSwagger == true) { app.UseSwagger(); app.UseSwaggerUI(); } if (config?.LogRequests == true) { app.UseHttpLogging(); } // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error", createScopeForErrors: true); // 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(); app.UseStaticFiles(); app.UseAntiforgery(); app.MapRazorComponents().AddInteractiveServerRenderMode(); if (config?.DatabaseType == DatabaseTypes.Memory) { app.Logger.LogWarning( "`Memory` database type is in use. Data will be lost when the application is restarted. If you are not in a development environment, you should use `PostgresSQL` instead." ); } app.Run();