2023-03-08 12:20:33 +00:00
|
|
|
using InnovEnergy.App.Backend.Database;
|
2023-02-21 08:58:21 +00:00
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
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
|
|
|
{
|
2023-02-24 12:59:56 +00:00
|
|
|
public static void Main(String[] args)
|
2023-02-24 11:58:47 +00:00
|
|
|
{
|
|
|
|
using (var db = Db.Connect())
|
|
|
|
db.CreateFakeRelations();
|
2023-02-21 08:58:21 +00:00
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
2023-02-21 08:58:21 +00:00
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
builder.Services.AddControllers(); // TODO: remove magic, specify controllers explicitly
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
2023-02-21 08:58:21 +00:00
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddCors(o => o.AddDefaultPolicy(p => p.WithOrigins("*").AllowAnyHeader().AllowAnyMethod()));
|
2023-02-24 12:59:56 +00:00
|
|
|
builder.Services.AddSwaggerGen(c =>
|
2023-02-24 11:58:47 +00:00
|
|
|
{
|
2023-02-24 12:59:56 +00:00
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "InnovEnergy Backend API", Version = "v1" });
|
|
|
|
c.UseAllOfToExtendReferenceSchemas();
|
|
|
|
c.OperationFilter<HeaderFilter>(); //Todo testing throw me out
|
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-02-21 08:58:21 +00:00
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
if (app.Environment.IsDevelopment())
|
2023-02-21 08:58:21 +00:00
|
|
|
{
|
2023-02-24 11:58:47 +00:00
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI(cfg => cfg.EnableFilter());
|
2023-02-21 08:58:21 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
app.UseCors();
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.Use(SetSessionUser);
|
|
|
|
app.MapControllers();
|
2023-02-21 08:58:21 +00:00
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
app.Run();
|
|
|
|
}
|
2023-02-21 08:58:21 +00:00
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
private static async Task SetSessionUser(HttpContext ctx, RequestDelegate next)
|
2023-02-21 08:58:21 +00:00
|
|
|
{
|
2023-02-24 11:58:47 +00:00
|
|
|
var headers = ctx.Request.Headers;
|
|
|
|
var hasToken = headers.TryGetValue("auth", out var token);
|
2023-02-21 08:58:21 +00:00
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
if (hasToken)
|
2023-02-21 08:58:21 +00:00
|
|
|
{
|
2023-02-24 11:58:47 +00:00
|
|
|
using var db = Db.Connect();
|
|
|
|
ctx.Items["User"] = db.GetUserByToken(token.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
await next(ctx);
|
2023-02-21 08:58:21 +00:00
|
|
|
}
|
|
|
|
}
|