using System.Text.Json.Serialization; using DigitalOps.Api; using DigitalOps.Dal; using DigitalOps.Dal.DbEntity; using FluentValidation.AspNetCore; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.EntityFrameworkCore; using Npgsql; using OpenHarbor.CQRS; using OpenHarbor.CQRS.AspNetCore.Mvc; using OpenHarbor.CQRS.DynamicQuery.AspNetCore; using PoweredSoft.Data; using PoweredSoft.Data.EntityFrameworkCore; using PoweredSoft.DynamicQuery; using PoweredSoft.Module.Abstractions; var builder = WebApplication.CreateBuilder(args); builder.Services.Configure(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost; options.KnownNetworks.Clear(); options.KnownProxies.Clear(); options.ForwardLimit = 2; }); builder.Services.AddHttpContextAccessor(); builder.Services.AddCors(); builder.Services.AddPoweredSoftDataServices(); builder.Services.AddPoweredSoftEntityFrameworkCoreDataServices(); builder.Services.AddPoweredSoftDynamicQuery(); builder.Services.AddDefaultCommandDiscovery(); builder.Services.AddDefaultQueryDiscovery(); builder.Services .AddFluentValidation(); builder.Services.AddModule(); var mvcBuilder = builder.Services .AddControllers() .AddJsonOptions(jsonOptions => { jsonOptions.JsonSerializerOptions.Converters.Insert(0, new JsonStringEnumConverter()); }); mvcBuilder .AddOpenHarborCommands(); mvcBuilder .AddOpenHarborQueries() .AddOpenHarborDynamicQueries(); // Add services to the container. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => { options.Authority = builder.Configuration["JwtBearer:Authority"]; // check how to set up AudienceValidator to whitelist sites using it options.TokenValidationParameters.ValidateAudience = false; }); builder.Services.AddAuthorization(); var connectionString = builder.Configuration.GetSection("Database").GetValue("ConnectionString"); var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString); dataSourceBuilder.MapEnum("organization_role"); var dataSource = dataSourceBuilder.Build(); builder.Services.AddDbContextPool(options => { options.UseNpgsql(dataSource, o => { // todo: ef 9.0+ // o.MapEnum("route_file_type"); }); if (builder.Configuration.GetValue("Debug")) { AppContext.SetSwitch("Npgsql.EnableConnectionStringLogging", true); options .EnableSensitiveDataLogging() .EnableDetailedErrors(); } }); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseForwardedHeaders(); app.UseHttpsRedirection(); app.UseCors(options => { var origins = new List {"https://hoppscotch.io"}; if (builder.Environment.IsDevelopment()) { origins.Add("http://localhost:4200"); } options.WithOrigins(origins.ToArray()); options.AllowCredentials(); options.AllowAnyHeader(); options.AllowAnyMethod(); }); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();