-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
44 lines (34 loc) · 1.04 KB
/
Program.cs
File metadata and controls
44 lines (34 loc) · 1.04 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
using LabReportAPI.Services;
using LabReportAPI.Models;
var builder = WebApplication.CreateBuilder(args);
// Services
builder.Services.AddControllers();
builder.Services.AddHostedService<TcpListenerService>();
builder.Services.Configure<LabSettings>(builder.Configuration.GetSection("LabSettings"));
builder.Services.AddSingleton<ILogService, LogService>();
var app = builder.Build();
// Static files
app.UseDefaultFiles();
app.UseStaticFiles();
// Routing
app.UseRouting();
app.MapControllers();
// Friendly fallback for unknown routes (non-API)
app.Use(async (context, next) =>
{
var path = context.Request.Path.Value ?? "";
if (!path.StartsWith("/api") && !System.IO.Path.HasExtension(path))
{
context.Response.ContentType = "text/html";
context.Response.StatusCode = 404;
await context.Response.SendFileAsync("wwwroot/error.html");
return;
}
await next();
});
// Shutdown log
app.Lifetime.ApplicationStopping.Register(() =>
{
Console.WriteLine("🔄 Application is stopping...");
});
app.Run();