133 lines
3.8 KiB
C#
133 lines
3.8 KiB
C#
using System.Text;
|
|
using line_gestao_api.Data;
|
|
using line_gestao_api.Models;
|
|
using line_gestao_api.Services;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddControllers();
|
|
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
|
{
|
|
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
|
options.KnownIPNetworks.Clear();
|
|
options.KnownProxies.Clear();
|
|
});
|
|
|
|
builder.Services.Configure<FormOptions>(o =>
|
|
{
|
|
o.MultipartBodyLengthLimit = 50_000_000;
|
|
});
|
|
|
|
var corsOrigins = builder.Configuration
|
|
.GetSection("Cors:AllowedOrigins")
|
|
.Get<string[]>()?
|
|
.Where(o => !string.IsNullOrWhiteSpace(o))
|
|
.Select(o => o.Trim())
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
.ToArray()
|
|
?? [];
|
|
|
|
if (corsOrigins.Length == 0)
|
|
{
|
|
corsOrigins = ["http://localhost:4200"];
|
|
}
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("Front", p =>
|
|
p.WithOrigins(corsOrigins)
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
);
|
|
});
|
|
|
|
builder.Services.AddDbContext<AppDbContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("Default"))
|
|
);
|
|
|
|
builder.Services.AddHttpContextAccessor();
|
|
builder.Services.AddScoped<ITenantProvider, TenantProvider>();
|
|
builder.Services.AddScoped<IAuditLogBuilder, AuditLogBuilder>();
|
|
builder.Services.AddScoped<IVigenciaNotificationSyncService, VigenciaNotificationSyncService>();
|
|
builder.Services.AddScoped<ParcelamentosImportService>();
|
|
builder.Services.AddScoped<GeralDashboardInsightsService>();
|
|
builder.Services.AddScoped<SpreadsheetImportAuditService>();
|
|
|
|
builder.Services.AddIdentityCore<ApplicationUser>(options =>
|
|
{
|
|
options.Password.RequiredLength = 6;
|
|
options.User.RequireUniqueEmail = false;
|
|
})
|
|
.AddRoles<IdentityRole<Guid>>()
|
|
.AddEntityFrameworkStores<AppDbContext>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
var jwtKey = builder.Configuration["Jwt:Key"];
|
|
if (string.IsNullOrWhiteSpace(jwtKey))
|
|
{
|
|
throw new InvalidOperationException("Configuration 'Jwt:Key' is required.");
|
|
}
|
|
|
|
var issuer = builder.Configuration["Jwt:Issuer"];
|
|
var audience = builder.Configuration["Jwt:Audience"];
|
|
|
|
builder.Services
|
|
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = issuer,
|
|
ValidAudience = audience,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
|
|
};
|
|
});
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
builder.Services.Configure<NotificationOptions>(builder.Configuration.GetSection("Notifications"));
|
|
builder.Services.AddHostedService<VigenciaNotificationBackgroundService>();
|
|
|
|
builder.Services.Configure<SeedOptions>(builder.Configuration.GetSection("Seed"));
|
|
|
|
var app = builder.Build();
|
|
app.UseForwardedHeaders();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
var useHttpsRedirection = builder.Configuration.GetValue("App:UseHttpsRedirection", !app.Environment.IsDevelopment());
|
|
if (useHttpsRedirection)
|
|
{
|
|
app.UseHttpsRedirection();
|
|
}
|
|
|
|
app.UseCors("Front");
|
|
|
|
app.UseAuthentication();
|
|
app.UseMiddleware<TenantMiddleware>();
|
|
app.UseAuthorization();
|
|
|
|
await SeedData.EnsureSeedDataAsync(app.Services);
|
|
|
|
app.MapControllers();
|
|
app.MapGet("/health", () => Results.Ok(new { status = "ok" }));
|
|
|
|
app.Run();
|