Innovenergy_trunk/csharp/App/Backend/Program.cs

65 lines
1.9 KiB
C#
Raw Normal View History

using System.Reactive.Linq;
2023-03-08 12:20:33 +00:00
using InnovEnergy.App.Backend.Database;
using Microsoft.OpenApi.Models;
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-15 13:38:06 +00:00
Db.CreateFakeRelations();
2023-02-24 11:58:47 +00:00
var builder = WebApplication.CreateBuilder(args);
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-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-24 11:58:47 +00:00
var app = builder.Build();
2023-02-24 11:58:47 +00:00
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
2023-02-24 11:58:47 +00:00
app.UseSwagger();
app.UseSwaggerUI(cfg => cfg.EnableFilter());
}
2023-02-24 11:58:47 +00:00
app.UseCors();
app.UseHttpsRedirection();
app.UseAuthorization();
app.Use(SetSessionUser);
app.MapControllers();
2023-02-24 11:58:47 +00:00
app.Run();
}
2023-03-15 13:38:06 +00:00
2023-02-24 11:58:47 +00:00
private static async Task SetSessionUser(HttpContext ctx, RequestDelegate next)
{
2023-02-24 11:58:47 +00:00
var headers = ctx.Request.Headers;
2023-03-15 13:38:06 +00:00
var hasToken = headers.TryGetValue("auth", out var token) ;
2023-02-24 11:58:47 +00:00
if (hasToken)
{
2023-03-15 13:38:06 +00:00
var session = Db.GetSession(token);
if (session is not null)
2023-03-16 07:49:01 +00:00
ctx.Items["Session"] = session;
2023-02-24 11:58:47 +00:00
}
await next(ctx);
}
}