168 lines
4.7 KiB
C#
168 lines
4.7 KiB
C#
|
using CH.Api;
|
||
|
using CH.Dal;
|
||
|
using FluentValidation.AspNetCore;
|
||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||
|
using Microsoft.AspNetCore.HttpOverrides;
|
||
|
using Microsoft.AspNetCore.Routing;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using Microsoft.OpenApi.Models;
|
||
|
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;
|
||
|
using System.Text.Json.Serialization;
|
||
|
|
||
|
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>();
|
||
|
builder.Services.AddDefaultCommandDiscovery();
|
||
|
builder.Services.AddDefaultQueryDiscovery();
|
||
|
if (builder.Configuration.GetValue<bool>("Swagger:Enable"))
|
||
|
{
|
||
|
builder.Services.AddEndpointsApiExplorer();
|
||
|
builder.Services.AddSwaggerGen(options =>
|
||
|
{
|
||
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Plan B Route Service Api", Version = "0.1.0" });
|
||
|
|
||
|
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
|
||
|
{
|
||
|
Type = SecuritySchemeType.OAuth2,
|
||
|
Flows = new OpenApiOAuthFlows
|
||
|
{
|
||
|
AuthorizationCode = new OpenApiOAuthFlow
|
||
|
{
|
||
|
AuthorizationUrl = new Uri(builder.Configuration["Swagger:AuthorizationUrl"]),
|
||
|
TokenUrl = new Uri(builder.Configuration["Swagger:TokenUrl"]),
|
||
|
Scopes = new Dictionary<string, string>
|
||
|
{
|
||
|
{ "email", "Email" }
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
options.AddSecurityRequirement(new OpenApiSecurityRequirement {
|
||
|
{
|
||
|
new OpenApiSecurityScheme {
|
||
|
Reference = new OpenApiReference {
|
||
|
Type = ReferenceType.SecurityScheme,
|
||
|
Id = "oauth2"
|
||
|
},
|
||
|
},
|
||
|
new[] { "email" }
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
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();
|
||
|
builder.Services.AddHttpClient();
|
||
|
builder.Services.AddMemoryCache();
|
||
|
|
||
|
|
||
|
var mvcBuilder = builder.Services
|
||
|
.AddControllers()
|
||
|
.AddJsonOptions(jsonOptions =>
|
||
|
{
|
||
|
jsonOptions.JsonSerializerOptions.Converters.Insert(0, new JsonStringEnumConverter());
|
||
|
});
|
||
|
|
||
|
mvcBuilder
|
||
|
.AddOpenHarborCommands();
|
||
|
|
||
|
mvcBuilder
|
||
|
.AddOpenHarborQueries()
|
||
|
.AddOpenHarborDynamicQueries();
|
||
|
var connectionString = builder.Configuration.GetSection("Database").GetValue<string>("ConnectionString");
|
||
|
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
|
||
|
|
||
|
var dataSource = dataSourceBuilder.Build();
|
||
|
|
||
|
builder.Services.AddDbContextPool<CHDbContext>(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();
|
||
|
|
||
|
app.UseForwardedHeaders();
|
||
|
|
||
|
if (builder.Configuration.GetValue<bool>("Swagger:Enable"))
|
||
|
{
|
||
|
app.UseSwagger();
|
||
|
app.UseSwaggerUI(options =>
|
||
|
{
|
||
|
options.OAuthClientId(builder.Configuration["Swagger:ClientId"]);
|
||
|
options.OAuthClientSecret(builder.Configuration["Swagger:Secret"]);
|
||
|
options.OAuthUsePkce();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
if (builder.Environment.IsDevelopment() == false)
|
||
|
{
|
||
|
app.UseHttpsRedirection();
|
||
|
}
|
||
|
app.UseCors(options =>
|
||
|
{
|
||
|
var origins = new List<string> { "capacitor://localhost", "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();
|