114 lines
3.8 KiB
C#
114 lines
3.8 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 Exam
|
|
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();
|
|
|
|
// Router
|
|
// API Get
|
|
app.MapGet("/", () => Results.Redirect("/swagger"));
|
|
app.MapGet("/api/GetRecvDataArray", Router.API.GetRecvDataArray);
|
|
app.MapPut("/api/SendString", Router.API.SendString);
|
|
// API Put
|
|
app.MapPut("/api/SendAddrPackage", Router.API.SendAddrPackage);
|
|
app.MapPut("/api/SendDataPackage", Router.API.SendDataPackage);
|
|
// API Jtag Put
|
|
app.MapPut("/api/jtag/RunCommand", Router.API.Jtag.RunCommand);
|
|
app.MapPut("/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();
|
|
}
|
|
|