2024-08-02 14:15:12 +00:00
|
|
|
using System.Diagnostics;
|
2024-04-30 12:07:50 +00:00
|
|
|
using Flurl.Http;
|
2023-04-13 12:30:01 +00:00
|
|
|
using Hellang.Middleware.ProblemDetails;
|
2023-03-23 12:28:55 +00:00
|
|
|
using InnovEnergy.App.Backend.Database;
|
2023-11-08 10:59:15 +00:00
|
|
|
using InnovEnergy.App.Backend.Websockets;
|
2023-07-13 07:40:04 +00:00
|
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
2023-04-13 12:30:01 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-02-21 08:58:21 +00:00
|
|
|
using Microsoft.OpenApi.Models;
|
2023-09-15 11:34:28 +00:00
|
|
|
using InnovEnergy.Lib.Utils;
|
2024-04-30 12:07:50 +00:00
|
|
|
|
2023-03-08 12:20:33 +00:00
|
|
|
namespace InnovEnergy.App.Backend;
|
2023-02-21 08:58:21 +00:00
|
|
|
|
2023-02-24 12:59:56 +00:00
|
|
|
public static class Program
|
2023-02-21 08:58:21 +00:00
|
|
|
{
|
2024-04-30 12:07:50 +00:00
|
|
|
public static async Task Main(String[] args)
|
2023-02-24 11:58:47 +00:00
|
|
|
{
|
2024-11-28 13:43:47 +00:00
|
|
|
//First, we initialize the database. This is an empty constructor of the Db class that will be called.
|
|
|
|
//In addition, we initialize WatchDog in order to restart the backend service in case of failure.
|
|
|
|
//Finally, we start all the backend services. We call the InitializeEnvironment function of RabbitMqManager to create the queue (factory/connection)
|
|
|
|
//Then, we generate a consumer that binds to the queue. This is a separate async Task so it must not be awaited (it acts as a separate thread).
|
|
|
|
//Finally, we call the MonitorSalimaxInstallationTable and MonitorSalidomoInstallationTable from the WebsocketManager class.
|
|
|
|
//Those methods will build in-memory data structures to track the connected frontends and update them regarding the offline installations.
|
|
|
|
|
2023-11-14 11:30:57 +00:00
|
|
|
Watchdog.NotifyReady();
|
2023-03-23 12:28:55 +00:00
|
|
|
Db.Init();
|
2023-07-13 07:40:04 +00:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
2023-11-29 20:28:11 +00:00
|
|
|
|
2023-11-27 15:43:42 +00:00
|
|
|
RabbitMqManager.InitializeEnvironment();
|
|
|
|
RabbitMqManager.StartRabbitMqConsumer();
|
2024-08-12 07:48:16 +00:00
|
|
|
WebsocketManager.MonitorSalimaxInstallationTable();
|
|
|
|
WebsocketManager.MonitorSalidomoInstallationTable();
|
2024-04-30 12:07:50 +00:00
|
|
|
|
2024-08-02 14:15:12 +00:00
|
|
|
|
2023-03-20 09:20:56 +00:00
|
|
|
builder.Services.AddControllers();
|
2023-04-13 12:30:01 +00:00
|
|
|
builder.Services.AddProblemDetails(setup =>
|
|
|
|
{
|
|
|
|
//This includes the stacktrace in Development Env
|
2023-10-26 12:09:38 +00:00
|
|
|
setup.IncludeExceptionDetails = (_, _) => builder.Environment.IsDevelopment() || builder.Environment.IsStaging();
|
2023-04-13 12:30:01 +00:00
|
|
|
|
|
|
|
//This handles our Exceptions
|
2023-09-15 11:34:28 +00:00
|
|
|
setup.Map<Exceptions>(exception => new ProblemDetails
|
2023-04-13 12:30:01 +00:00
|
|
|
{
|
|
|
|
Detail = exception.Detail,
|
|
|
|
Status = exception.Status,
|
|
|
|
Type = exception.Type,
|
|
|
|
Instance = exception.Instance
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2023-02-24 12:59:56 +00:00
|
|
|
builder.Services.AddSwaggerGen(c =>
|
2023-02-24 11:58:47 +00:00
|
|
|
{
|
2023-03-20 09:20:56 +00:00
|
|
|
c.SwaggerDoc("v1", OpenApiInfo);
|
2023-02-24 12:59:56 +00:00
|
|
|
c.UseAllOfToExtendReferenceSchemas();
|
2023-03-20 07:33:44 +00:00
|
|
|
c.SupportNonNullableReferenceTypes();
|
2023-02-24 11:58:47 +00:00
|
|
|
});
|
2023-02-21 08:58:21 +00:00
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
var app = builder.Build();
|
2023-09-15 11:34:28 +00:00
|
|
|
|
|
|
|
app.Use(async (context, next) =>
|
|
|
|
{
|
|
|
|
context.Request.WriteLine();
|
|
|
|
|
|
|
|
await next(context);
|
|
|
|
});
|
2023-11-08 10:59:15 +00:00
|
|
|
|
2023-09-15 11:34:28 +00:00
|
|
|
|
2023-11-08 10:59:15 +00:00
|
|
|
app.UseWebSockets();
|
2023-07-13 07:40:04 +00:00
|
|
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
|
|
|
{
|
|
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
|
|
|
});
|
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
if (app.Environment.IsDevelopment())
|
2023-02-21 08:58:21 +00:00
|
|
|
{
|
2023-02-24 11:58:47 +00:00
|
|
|
app.UseSwagger();
|
2023-03-20 09:20:56 +00:00
|
|
|
app.UseSwaggerUI();
|
2023-02-21 08:58:21 +00:00
|
|
|
}
|
2023-07-13 07:40:04 +00:00
|
|
|
|
2023-03-20 14:17:52 +00:00
|
|
|
app.UseCors(p => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()) ;
|
2023-09-15 11:34:28 +00:00
|
|
|
//app.UseHttpsRedirection();
|
2023-02-24 11:58:47 +00:00
|
|
|
app.MapControllers();
|
2023-04-13 12:30:01 +00:00
|
|
|
app.UseProblemDetails();
|
2023-07-13 07:40:04 +00:00
|
|
|
|
2023-09-15 11:34:28 +00:00
|
|
|
app.Run();
|
2023-02-24 11:58:47 +00:00
|
|
|
}
|
2023-02-21 08:58:21 +00:00
|
|
|
|
2023-03-20 09:20:56 +00:00
|
|
|
private static OpenApiInfo OpenApiInfo { get; } = new OpenApiInfo
|
2023-02-21 08:58:21 +00:00
|
|
|
{
|
2024-11-28 13:43:47 +00:00
|
|
|
Title = "Innesco Backend API",
|
2023-03-20 09:20:56 +00:00
|
|
|
Version = "v1"
|
|
|
|
};
|
2023-02-21 08:58:21 +00:00
|
|
|
|
2023-11-22 15:49:47 +00:00
|
|
|
}
|