-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
111 lines (103 loc) · 4.47 KB
/
Copy pathProgram.cs
File metadata and controls
111 lines (103 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using backend.Configurations;
using FooBooRealTime_back_dotnet.Configuration;
using FooBooRealTime_back_dotnet.Controllers.SignalR;
using FooBooRealTime_back_dotnet.Data;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using QuizApp.Configurations;
using Serilog;
namespace FooBooRealTime_back_dotnet
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenAnyIP(5001, listenOption => // use https only
{
listenOption.UseHttps();
});
options.ConfigureHttpsDefaults(httpsOptions =>
{
httpsOptions.AllowAnyClientCertificate();
});
});
Log.Logger = new LoggerConfiguration()
.ConfigureLoggerService(builder.Configuration)
.CreateLogger();
builder.Services.ConfigureRegisteredServices();
builder.Services.ConfigureDependancies(builder.Configuration);
builder.Services.ConfigureCors();
builder.Services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
}).AddJsonOptions(options =>
{
options.JsonSerializerOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.Never;
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Logging.AddConsole();
builder.Logging.SetMinimumLevel(LogLevel.Debug);
builder.Host.UseSerilog();
try
{
Log.Information("Starting up the server...");
var app = builder.Build();
// migrate data of DBContext
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<FooBooDbContext>();
context.Database.Migrate(); // Apply migrations
}
catch (Exception ex)
{
// Log errors during migration
app.Logger.LogError(ex, "An error occurred while migrating the database.");
}
}
// ensure Swagger API not available in production.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseSerilogRequestLogging(); // Logs all requests and responses
app.UseHttpsRedirection(); // Redirects HTTP to HTTPS
app.UseRouting(); // Matches routes to endpoints
app.UseCors("Allow3001"); // Applies CORS policy (choose one)
app.UseAuthentication(); // Validates credentials and sets User
app.UseAuthorization(); // Enforces access control policies
// Map routes and hubs
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<GameHub>("hub/game",options =>
{
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets |
Microsoft.AspNetCore.Http.Connections.HttpTransportType.ServerSentEvents |
Microsoft.AspNetCore.Http.Connections.HttpTransportType.LongPolling;
});
});
Log.Information("Server successfully started");
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
}
}
}