Innovenergy_trunk/csharp/App/Backend/Program.cs

106 lines
3.2 KiB
C#
Raw Normal View History

using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
using System.Threading.Channels;
using Hellang.Middleware.ProblemDetails;
2023-03-23 12:28:55 +00:00
using InnovEnergy.App.Backend.Database;
using InnovEnergy.App.Backend.Websockets;
2023-07-13 07:40:04 +00:00
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
2023-09-15 11:34:28 +00:00
using InnovEnergy.Lib.Utils;
using RabbitMQ.Client;
2023-03-08 12:20:33 +00:00
namespace InnovEnergy.App.Backend;
2023-02-24 12:59:56 +00:00
public static class Program
{
2023-02-24 12:59:56 +00:00
public static void Main(String[] args)
2023-02-24 11:58:47 +00:00
{
2023-03-20 07:33:44 +00:00
//Db.CreateFakeRelations();
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);
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();
2023-09-15 11:34:28 +00:00
2023-03-20 09:20:56 +00:00
builder.Services.AddControllers();
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();
//This handles our Exceptions
2023-09-15 11:34:28 +00:00
setup.Map<Exceptions>(exception => new ProblemDetails
{
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-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-09-15 11:34:28 +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-24 11:58:47 +00:00
app.UseSwagger();
2023-03-20 09:20:56 +00:00
app.UseSwaggerUI();
}
2023-07-13 07:40:04 +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();
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-03-20 09:20:56 +00:00
private static OpenApiInfo OpenApiInfo { get; } = new OpenApiInfo
{
2023-03-20 09:20:56 +00:00
Title = "InnovEnergy Backend API",
Version = "v1"
};
2023-03-20 09:20:56 +00:00
}
2023-03-15 13:38:06 +00:00
2023-02-24 11:58:47 +00:00
2023-03-20 09:20:56 +00:00
// var x = new CorsPolicy
// {
// Headers = { "*" },
// Origins = { "*" },
// Methods = { "*" }
// };