summary refs log tree commit diff
path: root/extra/admin-api/Spacebar.UApi/Program.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-02-13 06:12:01 +0100
committerRory& <root@rory.gay>2026-02-13 06:12:18 +0100
commite5b7977f2ae07b42e3be2dbbf79c450b8176ed19 (patch)
tree19a45641ef5b2b4b1c85b0f6700b4a1592bc3a48 /extra/admin-api/Spacebar.UApi/Program.cs
parentdie (diff)
downloadserver-ts-e5b7977f2ae07b42e3be2dbbf79c450b8176ed19.tar.xz
cs: create uapi project
Diffstat (limited to 'extra/admin-api/Spacebar.UApi/Program.cs')
-rw-r--r--extra/admin-api/Spacebar.UApi/Program.cs83
1 files changed, 83 insertions, 0 deletions
diff --git a/extra/admin-api/Spacebar.UApi/Program.cs b/extra/admin-api/Spacebar.UApi/Program.cs
new file mode 100644

index 00000000..4695b7ab --- /dev/null +++ b/extra/admin-api/Spacebar.UApi/Program.cs
@@ -0,0 +1,83 @@ +using System.Text.Json.Serialization; +using ArcaneLibs; +using Microsoft.EntityFrameworkCore; +using Spacebar.Interop.Authentication; +using Spacebar.Interop.Authentication.AspNetCore; +using Spacebar.Models.Db.Contexts; + +var builder = WebApplication.CreateBuilder(args); +if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("APPSETTINGS_PATH"))) + builder.Configuration.AddJsonFile(Environment.GetEnvironmentVariable("APPSETTINGS_PATH")!); + +// 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.MaxDepth = 100; + // 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.AddSingleton<SpacebarAuthenticationConfiguration>(); +builder.Services.AddScoped<SpacebarAuthenticationService>(); +builder.Services.AddScoped<SpacebarAspNetAuthenticationService>(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) { + app.MapOpenApi(); +} + +app.UseAuthorization(); + +app.MapControllers(); +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(); +}); + +// fallback to proxy in case we dont have a specific endpoint... +// TODO config +app.MapFallback("{*_}", async context => { + var client = new StreamingHttpClient(); + var requestMessage = new HttpRequestMessage( + new HttpMethod(context.Request.Method), + "http://api.old.server.spacebar.chat" + context.Request.Path + context.Request.QueryString + ) { + Content = new StreamContent(context.Request.Body) + }; + Console.WriteLine(requestMessage.RequestUri); + + foreach (var header in context.Request.Headers) + if (header.Key is not ("Accept-Encoding" or "Host")) + requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()); + + var responseMessage = await client.SendUnhandledAsync(requestMessage, CancellationToken.None); + context.Response.StatusCode = (int)responseMessage.StatusCode; + + foreach (var header in responseMessage.Headers) context.Response.Headers[header.Key] = header.Value.ToArray(); + foreach (var header in responseMessage.Content.Headers) context.Response.Headers[header.Key] = header.Value.ToArray(); + + await responseMessage.Content.CopyToAsync(context.Response.Body); +}); + +app.Run(); \ No newline at end of file