This commit is contained in:
98
test-aspnetcore/src/Program.cs
Executable file
98
test-aspnetcore/src/Program.cs
Executable file
@@ -0,0 +1,98 @@
|
||||
global using Microsoft.EntityFrameworkCore;
|
||||
global using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
global using Microsoft.AspNetCore.Http.HttpResults;
|
||||
global using System.Security.Claims;
|
||||
global using FluentValidation;
|
||||
|
||||
using Microsoft.AspNetCore.Cors.Infrastructure;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Text;
|
||||
using TestAspNetCore.Infra;
|
||||
using TestAspNetCore.Utils;
|
||||
using TestAspNetCore.Core.Jobs;
|
||||
using TestAspNetCore.Core.Users;
|
||||
using TestAspNetCore.Core.Vehicles;
|
||||
using TestAspNetCore.Core.WeatherForecasts;
|
||||
|
||||
namespace TestAspNetCore;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
AddServices(builder);
|
||||
|
||||
var app = builder.Build();
|
||||
Configure(app);
|
||||
|
||||
app.Run();
|
||||
}
|
||||
|
||||
public static void AddServices(WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddDbContext<AppDbContext>(
|
||||
options => options.UseSqlite("DataSource=app.db"));
|
||||
|
||||
builder.Services.AddOpenApi();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(
|
||||
"CorsPolicy",
|
||||
builder => builder
|
||||
.AllowAnyOrigin()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
);
|
||||
});
|
||||
|
||||
builder.Services.AddValidatorsFromAssembly(typeof(Program).Assembly);
|
||||
}
|
||||
|
||||
public static void Configure(WebApplication app)
|
||||
{
|
||||
app.MapOpenApi();
|
||||
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
app.UseDeveloperExceptionPage();
|
||||
app.UseCors("CorsPolicy");
|
||||
app.UseHttpsRedirection();
|
||||
app.UseRouting();
|
||||
|
||||
var apis = app.MapGroup("")
|
||||
.AddEndpointFilter<RequestLoggingFilter>();
|
||||
|
||||
var securityScheme = new OpenApiSecurityScheme()
|
||||
{
|
||||
Type = SecuritySchemeType.Http,
|
||||
Name = JwtBearerDefaults.AuthenticationScheme,
|
||||
Scheme = JwtBearerDefaults.AuthenticationScheme,
|
||||
Reference = new()
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = JwtBearerDefaults.AuthenticationScheme
|
||||
}
|
||||
};
|
||||
apis.WithOpenApi(x => new()
|
||||
{
|
||||
Security = [new() { [securityScheme] = [] }]
|
||||
});
|
||||
|
||||
JobApi.MapEndpoints(apis);
|
||||
UserApi.MapEndpoints(apis);
|
||||
VehicleApi.MapEndpoints(apis);
|
||||
WeatherForecastApi.MapEndpoints(apis);
|
||||
|
||||
using var scope = app.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
db.Database.EnsureCreated();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user