FPGA_WebLab/server/Program.cs

118 lines
4.1 KiB
C#

using System.Reflection;
using Microsoft.OpenApi.Models;
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);
// Add services to the container.
builder.Services.AddControllersWithViews();
// NLog: Setup NLog for Dependency injection
builder.Logging.ClearProviders();
builder.Host.UseNLog();
builder.Services.AddEndpointsApiExplorer();
// 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.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "FPGA Web Lab API",
Description = "Use FPGA in the cloud",
Version = "v1"
});
// Generate Doc and Example
options.IncludeXmlComments(Assembly.GetExecutingAssembly());
// var executingAssembly = Assembly.GetExecutingAssembly();
// var xmlFilename = $"{executingAssembly.GetName().Name}.xml";
// options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
// var referencedProjectsXmlDocPaths =
// executingAssembly.GetReferencedAssemblies()
// .Where(assembly => assembly.Name != null && assembly.Name.StartsWith("server", StringComparison.InvariantCultureIgnoreCase))
// .Select(assembly => Path.Combine(AppContext.BaseDirectory, $"{assembly.Name}.xml"))
// .Where(path => File.Exists(path));
// foreach (var xmlDocPath in referencedProjectsXmlDocPaths) options.IncludeXmlComments(xmlDocPath);
});
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseExceptionHandler("/Home/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();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// if (app.Environment.IsDevelopment())
// {
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "FPAG WebLab API V1");
});
// }
// Setup Program
MsgBus.Init();
// if (app.Environment.IsDevelopment()) MsgBus.UDPServer.EnableDebugMode = true;
MsgBus.UDPServer.EnableDebugMode = true;
// Router
// API Get
app.MapGet("/", () => Results.Redirect("/swagger"));
app.MapGet("/api/GetRecvDataArray", Router.API.GetRecvDataArray);
// API Post
app.MapPost("/api/SendString", Router.API.SendString);
app.MapPost("/api/SendBytes", Router.API.SendBytes);
app.MapPost("/api/SendAddrPackage", Router.API.SendAddrPackage);
app.MapPost("/api/SendDataPackage", Router.API.SendDataPackage);
// API Jtag
app.MapPost("/api/jtag/RunCommand", Router.API.Jtag.RunCommand);
app.MapGet("/api/jtag/GetIDCode", Router.API.Jtag.GetDeviceIDCode);
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();
}