summary refs log tree commit diff
path: root/extra/admin-api/Spacebar.AdminApi/Program.cs
blob: f3e977cbe52dba46a63b8a881838f46d7e783169 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http.Timeouts;
using Microsoft.EntityFrameworkCore;
using Spacebar.AdminApi.Middleware;
using Spacebar.AdminApi.Services;
using Spacebar.Db.Contexts;
using Spacebar.RabbitMqUtilities;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers(options => {
    options.MaxValidationDepth = null;
    // options.MaxIAsyncEnumerableBufferLimit = 1;
}).AddJsonOptions(options => {
    options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
    options.JsonSerializerOptions.WriteIndented = true;
    // options.JsonSerializerOptions.DefaultBufferSize = ;
}).AddMvcOptions(o=> {
    o.SuppressOutputFormatterBuffering = true;
});

// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddDbContextPool<SpacebarDbContext>(options => {
    options
        .UseNpgsql(builder.Configuration.GetConnectionString("Spacebar"))
        .EnableDetailedErrors();
});
builder.Services.AddScoped<AuthenticationService>();
builder.Services.AddScoped<Configuration>();
builder.Services.AddSingleton<RabbitMQConfiguration>();
builder.Services.AddSingleton<RabbitMQService>();

builder.Services.AddRequestTimeouts(x => {
    x.DefaultPolicy = new RequestTimeoutPolicy {
        Timeout = TimeSpan.FromMinutes(10),
        WriteTimeoutResponse = async context => {
            context.Response.StatusCode = 504;
            context.Response.ContentType = "application/json";
            await context.Response.StartAsync();
            await context.Response.WriteAsJsonAsync(new { error = "Unknown error" });
            await context.Response.CompleteAsync();
        }
    };
});
// builder.Services.AddCors(options => {
//     options.AddPolicy(
//         "Open",
//         policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
// });

var app = builder.Build();
app.Use((context, next) => {
    context.Response.Headers["Access-Control-Allow-Origin"] = "*";
    context.Response.Headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS";
    context.Response.Headers["Access-Control-Allow-Headers"] = "*, Authorization";
    if (context.Request.Method == "OPTIONS") {
        context.Response.StatusCode = 200;
        return Task.CompletedTask;
    }

    return next();
});
app.UsePathBase("/_spacebar/admin");
// app.UseCors("Open");

// Configure the HTTP request pipeline.
app.MapOpenApi();

app.UseMiddleware<AuthenticationMiddleware>();
app.UseAuthorization();

app.MapControllers();

app.Run();