Implement API security baseline (sys_user, JWT, audit fields, transactions, unique constraints) and Web real login with 401 handling. Document setup and development workflows in root, WmsLite.Api, and WmsLite.Web README files. Co-authored-by: Cursor <cursoragent@cursor.com>
65 lines
2.3 KiB
C#
65 lines
2.3 KiB
C#
using Scalar.AspNetCore;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using WmsLite.Api.Common;
|
|
using WmsLite.Api.Infrastructure;
|
|
using WmsLite.Api.Infrastructure.HttpLogging;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
if (builder.Environment.IsDevelopment())
|
|
{
|
|
builder.Configuration.AddJsonFile("appsettings.Development.local.json", optional: true, reloadOnChange: true);
|
|
}
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddOpenApi();
|
|
builder.Services.AddControllers()
|
|
.AddJsonOptions(options =>
|
|
{
|
|
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase, allowIntegerValues: true));
|
|
options.JsonSerializerOptions.Converters.Add(new JsonLongAsStringConverter());
|
|
options.JsonSerializerOptions.Converters.Add(new JsonNullableLongAsStringConverter());
|
|
})
|
|
.ConfigureApiBehaviorOptions(options =>
|
|
{
|
|
options.InvalidModelStateResponseFactory = context =>
|
|
{
|
|
var errors = context.ModelState
|
|
.Where(x => x.Value?.Errors.Count > 0)
|
|
.ToDictionary(
|
|
x => x.Key,
|
|
x => x.Value!.Errors.Select(e => string.IsNullOrWhiteSpace(e.ErrorMessage) ? "Invalid value." : e.ErrorMessage).ToArray());
|
|
|
|
return new BadRequestObjectResult(BusinessResult.BadRequest("Validation failed.", errors));
|
|
};
|
|
});
|
|
builder.Services.AddHttpContextAccessor();
|
|
builder.Services.Configure<HttpLoggingOptions>(builder.Configuration.GetSection(HttpLoggingOptions.SectionName));
|
|
builder.Services.AddWmsLiteAuth(builder.Configuration);
|
|
builder.Services.AddSqlSugarSetup(builder.Configuration);
|
|
builder.Services.AddWmsLiteServices();
|
|
builder.Services.AddWmsLiteMigrations(builder.Configuration);
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
app.UseSqlSugarCodeFirst();
|
|
app.UseWmsLiteMigrations();
|
|
app.UseHttpsRedirection();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.UseMiddleware<GlobalExceptionMiddleware>();
|
|
app.UseMiddleware<HttpRequestResponseLoggingMiddleware>();
|
|
app.MapOpenApi();
|
|
app.MapScalarApiReference(options =>
|
|
{
|
|
options.Title = "WmsLite API";
|
|
options.Theme = ScalarTheme.BluePlanet;
|
|
});
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|