FPGA_WebLab/server/Program.cs

114 lines
3.3 KiB
C#

using Microsoft.AspNetCore.Http.Features;
using Newtonsoft.Json;
using NLog;
using NLog.Web;
// Early init of NLog to allow startup and exception logging, before host is built
var logger = NLog.LogManager.Setup()
.LoadConfigurationFromAppSettings()
.GetCurrentClassLogger();
logger.Debug("Init Main...");
try
{
var builder = WebApplication.CreateBuilder(args);
// Services Settings
// Add services to the container.
builder.Services.AddControllersWithViews();
// NLog: Setup NLog for Dependency injection
builder.Logging.ClearProviders();
builder.Host.UseNLog();
builder.Services.AddEndpointsApiExplorer();
builder.Services.Configure<FormOptions>(options =>
{
options.MultipartBodyLengthLimit = 32 * 1024 * 1024;
});
// Add Json.Net Serializer
builder.Services.AddControllersWithViews().AddNewtonsoftJson(options =>
{
// Configure Newtonsoft.Json options here
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
// Add Swagger
builder.Services.AddControllers();
builder.Services.AddOpenApiDocument(options =>
{
options.PostProcess = document =>
{
document.Info = new NSwag.OpenApiInfo
{
Version = "v1",
Title = "FPGA Web Lab API",
Description = "Use FPGA in the cloud",
// TermsOfService = "https://example.com/terms",
// Contact = new NSwag.OpenApiContact
// {
// Name = "Example Contact",
// Url = "https://example.com/contact"
// },
// License = new NSwag.OpenApiLicense
// {
// Name = "Example License",
// Url = "https://example.com/license"
// }
};
};
});
// Application Settings
var app = builder.Build();
// Configure the HTTP request pipeline.
// app.UseExceptionHandler(new ExceptionHandlerOptions()
// {
// AllowStatusCode404Response = true,
// ExceptionHandlingPath = "/error"
// });
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
// if (app.Environment.IsDevelopment())
// {
app.UseOpenApi();
app.UseSwaggerUi();
// }
// Setup Program
MsgBus.Init();
// Router
// API Get
app.MapGet("/", () => Results.Redirect("/swagger"));
app.MapControllers();
app.Run("http://localhost:5000");
}
catch (Exception exception)
{
// NLog: catch setup errors
logger.Error(exception, "Stopped program because of exception");
throw;
}
finally
{
// Close UDP Server
logger.Info("Program is Closing now...");
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
NLog.LogManager.Shutdown();
// Close Program
MsgBus.Exit();
}