Configured CORS support with a default policy that allows requests from any origin. Added a TODO to restrict allowed origins using values from appsettings.json in the future.
53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using DbFirst.Application.Catalogs;
|
|
using DbFirst.Domain.Repositories;
|
|
using DbFirst.Infrastructure;
|
|
using DbFirst.Infrastructure.Repositories;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
//TODO: create and add exception handling middleware
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
// TODO: allow listed origins configured in appsettings.json
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(policy =>
|
|
{
|
|
policy.AllowAnyOrigin()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod();
|
|
});
|
|
});
|
|
|
|
// TODO: Create extension method for this in Infrastructure layer in case of using in multiple projects
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
// TODO: Create extension method for this in Application layer in case of using in multiple projects
|
|
builder.Services.AddAutoMapper(typeof(CatalogProfile).Assembly, typeof(ApplicationDbContext).Assembly);
|
|
|
|
builder.Services.AddScoped<ICatalogRepository, CatalogRepository>();
|
|
builder.Services.AddScoped<ICatalogService, CatalogService>();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
app.UseCors();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|