dotnet-digital-ops/DigitalOps.Api/Program.cs

127 lines
3.4 KiB
C#
Raw Normal View History

2025-01-02 15:10:52 -05:00
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<ForwardedHeadersOptions>(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<AppModule>();
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<string>("ConnectionString");
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
dataSourceBuilder.MapEnum<OrganizationRole>("organization_role");
var dataSource = dataSourceBuilder.Build();
builder.Services.AddDbContextPool<MainDbContext>(options =>
{
options.UseNpgsql(dataSource, o =>
{
// todo: ef 9.0+
// o.MapEnum<RouteFileType>("route_file_type");
});
if (builder.Configuration.GetValue<bool>("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<string> {"https://hoppscotch.io"};
if (builder.Environment.IsDevelopment())
{
origins.Add("http://localhost:8100");
}
options.WithOrigins(origins.ToArray());
options.AllowCredentials();
options.AllowAnyHeader();
options.AllowAnyMethod();
});
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();