using System.Reflection; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; using Myhm.Data; using Myhm.Models; using Myhm.Util.SwaggerTheme; 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.AddDbContextFactory(options => options.UseNpgsql(builder.Configuration.GetConnectionString("MyhmContext")) ); // Sentry if (config?.UseSentry == true) { builder.WebHost.UseSentry(); } // Add services to the container. builder.Services.AddHttpLogging(o => { }); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(opts => { opts.SwaggerDoc( "v1", new OpenApiInfo { Title = "Myhm API", Version = "v1", Description = "", } ); 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(); var assembly = Assembly.GetExecutingAssembly(); foreach (var resource in assembly.GetManifestResourceNames()) { app.Logger.LogDebug("Resource: {Resource}", resource); } if (config?.UseSwagger == true) { app.UseSwagger(); app.UseSwaggerUI(CustomStyle.CustomModern); } 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.UseAntiforgery(); // if (app.Environment.IsDevelopment()) // { // app.Logger.LogInformation( // "Development mode detected, printing configuration: {Configuration}", // config?.ToJson() // ); // } app.Run();