From 58b40fac84733bec11425cd19845a90524bd064a Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Sat, 12 Jul 2025 21:23:06 +0200 Subject: [PATCH] Improved configurability for Swagger and ApiKeyMiddleware --- src/Server/Program.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Server/Program.cs b/src/Server/Program.cs index d8b1024..ac39683 100644 --- a/src/Server/Program.cs +++ b/src/Server/Program.cs @@ -35,7 +35,10 @@ List? allowedIps = builder.Configuration.GetSection("Embeddingsearch:Elm app.Use(async (context, next) => { - if (context.Request.Path.StartsWithSegments("/elmah")) + bool requestIsElmah = context.Request.Path.StartsWithSegments("/elmah"); + bool requestIsSwagger = context.Request.Path.StartsWithSegments("/swagger"); + + if (requestIsElmah || requestIsSwagger) { var remoteIp = context.Connection.RemoteIpAddress?.ToString(); bool blockRequest = allowedIps is null @@ -56,14 +59,18 @@ app.UseElmah(); app.MapHealthChecks("/healthz"); +bool IsDevelopment = app.Environment.IsDevelopment(); +bool useSwagger = app.Configuration.GetValue("UseSwagger"); +bool? UseMiddleware = app.Configuration.GetValue("UseMiddleware"); + // Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) +if (IsDevelopment || useSwagger) { app.UseSwagger(); app.UseSwaggerUI(); //app.UseElmahExceptionPage(); // Messes with JSON response for API calls. Leaving this here so I don't accidentally put this in again later on. } -else +if (UseMiddleware == true || IsDevelopment) { app.UseMiddleware(); }