Innovenergy_trunk/csharp/App/Backend/Program.cs

106 lines
3.2 KiB
C#

using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
using System.Threading.Channels;
using Hellang.Middleware.ProblemDetails;
using InnovEnergy.App.Backend.Database;
using InnovEnergy.App.Backend.Websockets;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
using InnovEnergy.Lib.Utils;
using RabbitMQ.Client;
namespace InnovEnergy.App.Backend;
public static class Program
{
public static void Main(String[] args)
{
//Db.CreateFakeRelations();
Watchdog.NotifyReady();
Db.Init();
var builder = WebApplication.CreateBuilder(args);
string vpnServerIp = "194.182.190.208";
//string vpnServerIp = "127.0.0.1";
WebsocketManager.Factory = new ConnectionFactory { HostName = vpnServerIp};
WebsocketManager.Connection = WebsocketManager.Factory.CreateConnection();
WebsocketManager.Channel = WebsocketManager.Connection.CreateModel();
Console.WriteLine("Middleware subscribed to RabbitMQ queue, ready for receiving messages");
WebsocketManager.Channel.QueueDeclare(queue: "statusQueue", durable: true, exclusive: false, autoDelete: false, arguments: null);
WebsocketManager.StartRabbitMqConsumer();
Console.WriteLine("Queue declared");
WebsocketManager.InformInstallationsToSubscribeToRabbitMq();
builder.Services.AddControllers();
builder.Services.AddProblemDetails(setup =>
{
//This includes the stacktrace in Development Env
setup.IncludeExceptionDetails = (_, _) => builder.Environment.IsDevelopment() || builder.Environment.IsStaging();
//This handles our Exceptions
setup.Map<Exceptions>(exception => new ProblemDetails
{
Detail = exception.Detail,
Status = exception.Status,
Type = exception.Type,
Instance = exception.Instance
});
});
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", OpenApiInfo);
c.UseAllOfToExtendReferenceSchemas();
c.SupportNonNullableReferenceTypes();
});
var app = builder.Build();
app.Use(async (context, next) =>
{
context.Request.WriteLine();
await next(context);
});
app.UseWebSockets();
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseCors(p => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()) ;
//app.UseHttpsRedirection();
app.MapControllers();
app.UseProblemDetails();
app.Run();
}
private static OpenApiInfo OpenApiInfo { get; } = new OpenApiInfo
{
Title = "InnovEnergy Backend API",
Version = "v1"
};
}
// var x = new CorsPolicy
// {
// Headers = { "*" },
// Origins = { "*" },
// Methods = { "*" }
// };