Initial commit
This commit is contained in:
70
src/Apollinare.API/Program.cs
Normal file
70
src/Apollinare.API/Program.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Apollinare.API.Hubs;
|
||||
using Apollinare.API.Services;
|
||||
using Apollinare.API.Services.Reports;
|
||||
using Apollinare.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Database
|
||||
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
|
||||
?? "Data Source=apollinare.db";
|
||||
builder.Services.AddDbContext<AppollinareDbContext>(options =>
|
||||
options.UseSqlite(connectionString));
|
||||
|
||||
// Services
|
||||
builder.Services.AddScoped<EventoCostiService>();
|
||||
builder.Services.AddScoped<DemoDataService>();
|
||||
builder.Services.AddScoped<ReportGeneratorService>();
|
||||
builder.Services.AddSingleton<DataNotificationService>();
|
||||
|
||||
// SignalR
|
||||
builder.Services.AddSignalR()
|
||||
.AddJsonProtocol(options =>
|
||||
{
|
||||
options.PayloadSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
|
||||
options.PayloadSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
||||
});
|
||||
|
||||
// CORS - Allow credentials for SignalR
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowFrontend", policy =>
|
||||
{
|
||||
policy.SetIsOriginAllowed(origin => new Uri(origin).Host == "localhost")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowCredentials();
|
||||
});
|
||||
});
|
||||
|
||||
// Controllers with JSON options
|
||||
builder.Services.AddControllers()
|
||||
.AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
|
||||
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
||||
});
|
||||
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Initialize database
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
using var scope = app.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppollinareDbContext>();
|
||||
db.Database.EnsureCreated();
|
||||
DbSeeder.Seed(db);
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.UseCors("AllowFrontend");
|
||||
app.UseWebSockets();
|
||||
app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
app.MapHub<DataHub>("/hubs/data");
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user