60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
|
using Innovenergy.Backend.Database;
|
||
|
using Microsoft.OpenApi.Models;
|
||
|
|
||
|
namespace Innovenergy.Backend;
|
||
|
|
||
|
public static class Program
|
||
|
{
|
||
|
public static void Main(String[] args)
|
||
|
{
|
||
|
using (var db = Db.Connect())
|
||
|
db.CreateFakeRelations();
|
||
|
|
||
|
var builder = WebApplication.CreateBuilder(args);
|
||
|
|
||
|
builder.Services.AddControllers(); // TODO: remove magic, specify controllers explicitly
|
||
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||
|
|
||
|
builder.Services.AddHttpContextAccessor();
|
||
|
builder.Services.AddEndpointsApiExplorer();
|
||
|
builder.Services.AddCors(o => o.AddDefaultPolicy(p => p.WithOrigins("*").AllowAnyHeader().AllowAnyMethod()));
|
||
|
builder.Services.AddSwaggerGen(c =>
|
||
|
{
|
||
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "InnovEnergy Backend API", Version = "v1" });
|
||
|
c.UseAllOfToExtendReferenceSchemas();
|
||
|
c.OperationFilter<HeaderFilter>(); //Todo testing throw me out
|
||
|
});
|
||
|
|
||
|
|
||
|
var app = builder.Build();
|
||
|
|
||
|
// Configure the HTTP request pipeline.
|
||
|
if (app.Environment.IsDevelopment())
|
||
|
{
|
||
|
app.UseSwagger();
|
||
|
app.UseSwaggerUI(cfg => cfg.EnableFilter());
|
||
|
}
|
||
|
|
||
|
app.UseCors();
|
||
|
app.UseHttpsRedirection();
|
||
|
app.UseAuthorization();
|
||
|
app.Use(SetSessionUser);
|
||
|
app.MapControllers();
|
||
|
|
||
|
app.Run();
|
||
|
}
|
||
|
|
||
|
private static async Task SetSessionUser(HttpContext ctx, RequestDelegate next)
|
||
|
{
|
||
|
var headers = ctx.Request.Headers;
|
||
|
var hasToken = headers.TryGetValue("auth", out var token);
|
||
|
|
||
|
if (hasToken)
|
||
|
{
|
||
|
using var db = Db.Connect();
|
||
|
ctx.Items["User"] = db.GetUserByToken(token.ToString());
|
||
|
}
|
||
|
|
||
|
await next(ctx);
|
||
|
}
|
||
|
}
|