about summary refs log tree commit diff
path: root/Tests/LibMatrix.HomeserverEmulator/Program.cs
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2024-03-05 11:19:52 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2024-03-05 11:19:52 +0100
commitf41b6e5ec431c88bc1d94e4832d8ba49ddc42004 (patch)
tree503be94f5036f7cc221846c1eabf7c5edd107f1a /Tests/LibMatrix.HomeserverEmulator/Program.cs
parentUnknown changes (diff)
downloadLibMatrix-f41b6e5ec431c88bc1d94e4832d8ba49ddc42004.tar.xz
HomeserverEmulator work
Diffstat (limited to 'Tests/LibMatrix.HomeserverEmulator/Program.cs')
-rw-r--r--Tests/LibMatrix.HomeserverEmulator/Program.cs25
1 files changed, 21 insertions, 4 deletions
diff --git a/Tests/LibMatrix.HomeserverEmulator/Program.cs b/Tests/LibMatrix.HomeserverEmulator/Program.cs
index 516d380..ddf39c7 100644
--- a/Tests/LibMatrix.HomeserverEmulator/Program.cs
+++ b/Tests/LibMatrix.HomeserverEmulator/Program.cs
@@ -1,16 +1,20 @@
 using System.Net.Mime;

+using System.Text.Json.Serialization;

 using LibMatrix;

 using LibMatrix.HomeserverEmulator.Services;

 using Microsoft.AspNetCore.Diagnostics;

 using Microsoft.AspNetCore.Http.Timeouts;

+using Microsoft.AspNetCore.Mvc;

 using Microsoft.OpenApi.Models;

 

 var builder = WebApplication.CreateBuilder(args);

 

 // Add services to the container.

 

-builder.Services.AddControllers();

-// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

+builder.Services.AddControllers().AddJsonOptions(options => {

+    options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;

+});

+

 builder.Services.AddEndpointsApiExplorer();

 builder.Services.AddSwaggerGen(c => {

     c.SwaggerDoc("v1", new OpenApiInfo() {

@@ -20,11 +24,12 @@ builder.Services.AddSwaggerGen(c => {
     });

     c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "LibMatrix.HomeserverEmulator.xml"));

 });

+

 builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

+builder.Services.AddSingleton<HSEConfiguration>();

 builder.Services.AddSingleton<UserStore>();

 builder.Services.AddSingleton<RoomStore>();

 

-

 builder.Services.AddScoped<TokenService>();

 

 builder.Services.AddRequestTimeouts(x => {

@@ -45,7 +50,7 @@ builder.Services.AddRequestTimeouts(x => {
 builder.Services.AddCors(options => {

     options.AddPolicy(

         "Open",

-        policy => policy.AllowAnyOrigin().AllowAnyHeader());

+        policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());

 });

 var app = builder.Build();

 

@@ -62,6 +67,8 @@ app.UseExceptionHandler(exceptionHandlerApp => {
 

         var exceptionHandlerPathFeature =

             context.Features.Get<IExceptionHandlerPathFeature>();

+        if(exceptionHandlerPathFeature?.Error is not null)

+            Console.WriteLine(exceptionHandlerPathFeature.Error.ToString()!);

 

         if (exceptionHandlerPathFeature?.Error is MatrixException mxe) {

             context.Response.StatusCode = mxe.ErrorCode switch {

@@ -87,4 +94,14 @@ app.UseAuthorization();
 

 app.MapControllers();

 

+app.Map("/_matrix/{*_}", (HttpContext ctx) => {

+    Console.WriteLine($"Client hit non-existing route: {ctx.Request.Method} {ctx.Request.Path}");

+    ctx.Response.StatusCode = StatusCodes.Status404NotFound;

+    ctx.Response.ContentType = MediaTypeNames.Application.Json;

+    return ctx.Response.WriteAsJsonAsync(new MatrixException() {

+        ErrorCode = MatrixException.ErrorCodes.M_UNRECOGNISED,

+        Error = "Endpoint not implemented"

+    });

+});

+

 app.Run();