2024-03-15 21:35:24 -05:00
|
|
|
//using AAIntegration.SimmonsBank.API.Authorization;
|
|
|
|
using AAIntegration.SimmonsBank.API.Config;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
using AAIntegration.SimmonsBank.API.Services;
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
using System.Text.Json.Serialization;
|
2024-03-08 21:18:30 -06:00
|
|
|
using AAIntegration.SimmonsBank.API.Configs;
|
2024-03-15 21:17:05 -05:00
|
|
|
using AAIntegration.SimmonsBank.API.Handlers;
|
2024-03-15 21:35:24 -05:00
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
using System.Text;
|
|
|
|
//using Microsoft.Build.Framework;
|
2024-03-15 21:17:05 -05:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2024-03-08 21:18:30 -06:00
|
|
|
|
|
|
|
internal class Program
|
|
|
|
{
|
|
|
|
private static void Main(string[] args)
|
|
|
|
{
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
// Add services to the container.
|
2024-03-15 21:35:24 -05:00
|
|
|
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
|
|
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
{
|
|
|
|
options.AddPolicy("ClientPermission", policy =>
|
|
|
|
{
|
|
|
|
policy.AllowAnyHeader()
|
|
|
|
.AllowAnyMethod()
|
|
|
|
.SetIsOriginAllowed(_ => true)
|
|
|
|
.AllowCredentials();
|
|
|
|
});
|
|
|
|
|
|
|
|
options.AddPolicy(name: MyAllowSpecificOrigins, policy =>
|
|
|
|
{
|
|
|
|
policy.WithOrigins(builder.Configuration.GetSection("ActiveAllocator:AllowedHosts").Get<string[]>());
|
|
|
|
});
|
|
|
|
});
|
2024-03-08 21:18:30 -06:00
|
|
|
|
|
|
|
// Authentication
|
|
|
|
builder.Services.AddAuthentication()
|
|
|
|
.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>(ApiKeyAuthenticationOptions.DefaultScheme, null);
|
|
|
|
|
2024-03-15 21:17:05 -05:00
|
|
|
// Authorization
|
|
|
|
builder.Services.AddAuthorization(options => {
|
|
|
|
AuthorizationPolicyBuilder defaultAuthorizationPolicyBuilder = new AuthorizationPolicyBuilder(
|
|
|
|
ApiKeyAuthenticationOptions.DefaultScheme
|
|
|
|
);
|
|
|
|
|
|
|
|
defaultAuthorizationPolicyBuilder = defaultAuthorizationPolicyBuilder.RequireAuthenticatedUser();
|
|
|
|
options.DefaultPolicy = defaultAuthorizationPolicyBuilder.Build();
|
|
|
|
});
|
|
|
|
|
2024-03-15 21:35:24 -05:00
|
|
|
builder.Services.AddControllersWithViews().AddJsonOptions(x =>
|
|
|
|
{
|
|
|
|
// serialize enums as strings in api responses (e.g. Role)
|
|
|
|
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
2024-03-08 21:18:30 -06:00
|
|
|
|
2024-03-15 21:35:24 -05:00
|
|
|
// ignore omitted parameters on models to enable optional params (e.g. User update)
|
|
|
|
x.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
|
|
|
});
|
|
|
|
|
2024-03-08 21:18:30 -06:00
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
2024-03-15 21:35:24 -05:00
|
|
|
builder.Services.AddSwaggerGen(c =>
|
|
|
|
{
|
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "AA API", Version = "v1" });
|
|
|
|
});
|
2024-03-08 21:18:30 -06:00
|
|
|
|
2024-03-15 21:35:24 -05:00
|
|
|
// Configure strongly typed settings object
|
2024-03-15 21:17:05 -05:00
|
|
|
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
|
2024-03-15 21:35:24 -05:00
|
|
|
builder.Services.Configure<EnvelopeFundConfig>(builder.Configuration.GetSection("EnvelopeFund"));
|
|
|
|
builder.Services.Configure<DatabaseConfig>(builder.Configuration.GetSection("ActiveAllocator:Database"));
|
2024-03-15 21:17:05 -05:00
|
|
|
|
2024-03-15 21:35:24 -05:00
|
|
|
DatabaseConfig dbConfig = builder.Configuration.GetSection("ActiveAllocator:Database").Get<DatabaseConfig>();
|
2024-03-15 21:17:05 -05:00
|
|
|
|
2024-03-15 21:35:24 -05:00
|
|
|
builder.Services.AddAutoMapper(typeof(Program));
|
2024-03-15 21:17:05 -05:00
|
|
|
builder.Services.AddDbContext<DataContext>(opt =>
|
|
|
|
opt.UseNpgsql(dbConfig.GetConnectionString()));
|
|
|
|
|
|
|
|
builder.Services.AddScoped<IUserService, UserService>();
|
2024-03-15 21:35:24 -05:00
|
|
|
builder.Services.AddScoped<IAccountService, AccountService>();
|
|
|
|
builder.Services.AddScoped<ITransactionService, TransactionService>();
|
2024-03-15 21:17:05 -05:00
|
|
|
builder.Services.AddScoped<ICacheService, CacheService>();
|
2024-03-15 21:35:24 -05:00
|
|
|
builder.Services.AddScoped<IVersionService, VersionService>();
|
2024-03-15 21:17:05 -05:00
|
|
|
|
|
|
|
builder.Services.AddScoped<ApiKeyAuthenticationHandler>();
|
|
|
|
|
2024-03-08 21:18:30 -06:00
|
|
|
var app = builder.Build();
|
|
|
|
|
2024-03-15 21:35:24 -05:00
|
|
|
// Apply Database Migrations - This is NOT recommended for multi-node deployment!!!
|
|
|
|
using var scope = app.Services.CreateScope();
|
|
|
|
using var dbContext = scope.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
dbContext.Database.Migrate();
|
|
|
|
|
2024-03-08 21:18:30 -06:00
|
|
|
// Configure the HTTP request pipeline.
|
2024-03-15 21:35:24 -05:00
|
|
|
if (!app.Environment.IsDevelopment())
|
2024-03-08 21:18:30 -06:00
|
|
|
{
|
2024-03-15 21:35:24 -05:00
|
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
|
|
app.UseHsts();
|
2024-03-08 21:18:30 -06:00
|
|
|
}
|
|
|
|
|
2024-03-15 21:35:24 -05:00
|
|
|
app.UseCors("ClientPermission");
|
2024-03-08 21:18:30 -06:00
|
|
|
app.UseHttpsRedirection();
|
2024-03-15 21:35:24 -05:00
|
|
|
app.UseStaticFiles();
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseCors(MyAllowSpecificOrigins);
|
2024-03-08 21:18:30 -06:00
|
|
|
|
2024-03-15 21:35:24 -05:00
|
|
|
app.UseAuthentication();
|
2024-03-08 21:18:30 -06:00
|
|
|
app.UseAuthorization();
|
|
|
|
|
2024-03-15 21:35:24 -05:00
|
|
|
// global error handler
|
|
|
|
app.UseMiddleware<ErrorHandlerMiddleware>();
|
|
|
|
|
|
|
|
app.MapControllerRoute(
|
|
|
|
name: "default",
|
|
|
|
pattern: "{controller}/{action=Index}/{id?}");
|
|
|
|
|
|
|
|
app.MapFallbackToFile("index.html");
|
2024-03-08 21:18:30 -06:00
|
|
|
|
|
|
|
app.Run();
|
|
|
|
}
|
2024-03-15 21:35:24 -05:00
|
|
|
}
|