68 lines
2.1 KiB
C#
68 lines
2.1 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using PizzaExpress.Data;
|
|
|
|
namespace PizzaExpress
|
|
{
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
var allowedOrigins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>();
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy(name: MyAllowSpecificOrigins,
|
|
policy =>
|
|
{
|
|
policy.WithOrigins(allowedOrigins)
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
builder.Services.AddControllers()
|
|
.AddXmlSerializerFormatters();
|
|
|
|
// Creazione del contesto con DB SQLite
|
|
builder.Services.AddDbContext<PizzaContext>(opt =>
|
|
opt.UseSqlite("Data Source=pizza.db"));
|
|
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
var app = builder.Build();
|
|
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var ctx = scope.ServiceProvider.GetRequiredService<PizzaContext>();
|
|
// Crea il DB e le tabelle basate sul modello se non esistono (no migrations)
|
|
ctx.Database.EnsureCreated();
|
|
SeedData.Initialize(ctx);
|
|
}
|
|
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
|
|
app.UseHttpsRedirection();
|
|
app.UseCors(MyAllowSpecificOrigins);
|
|
app.UseAuthorization();
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
app.Run();
|
|
}
|
|
}
|
|
}
|