Fixed sub-path routing

This commit is contained in:
2026-02-02 14:07:58 +01:00
parent d35bccbb9f
commit fffe29ed1d
4 changed files with 19 additions and 14 deletions

View File

@@ -6,8 +6,8 @@ namespace QuotesTest.Controllers;
[Route("[controller]")]
public class QuotesController : Controller
{
private readonly Quotes _quotes;
public QuotesController(Quotes quotes)
private readonly QuotesContainer _quotes;
public QuotesController(QuotesContainer quotes)
{
_quotes = quotes;
}

View File

@@ -1,18 +1,24 @@
using System.Net;
using System.Text.Json;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.OpenApi;
using QuotesTest;
string domain = "https://ld50.dev"; // TODO: Remove magic strings, i.e. move to config file
string basePath = "QuotesTest"; // TODO: Remove magic strings
string absoluteBasePath = basePath.Length > 0 ? $"/{basePath}" : basePath;
string fullBasePath = $"{domain}/{basePath}";
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
string file = File.ReadAllText("tools/scraper/quotes.json");
string file = File.ReadAllText("Tools/scraper/quotes.json");
List<Quote> quotes = JsonSerializer.Deserialize<List<Quote>>(file) ?? throw new Exception("Konnte Quotes nicht laden.");
builder.Services.AddSingleton(quotes);
builder.Services.AddSingleton<Quotes>();
builder.Services.AddSingleton<QuotesContainer>();
builder.Services.AddEndpointsApiExplorer();
List<string>? ApiKeys = builder.Configuration.GetSection("ApiKeys").Get<List<string>>();
@@ -23,7 +29,9 @@ builder.Services.AddOpenApi(options =>
{
if (ApiKeys is null)
return Task.CompletedTask;
document.Servers = [
new OpenApiServer { Url = fullBasePath }
];
document.Components ??= new();
document.Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>();
@@ -51,10 +59,6 @@ builder.Services.AddOpenApi(options =>
});
var app = builder.Build();
string domain = "https://ld50.dev";
string basePath = "QuotesTest";
string absoluteBasePath = basePath.Length > 0 ? $"/{basePath}" : basePath;
string fullBasePath = $"{domain}/{basePath}";
app.UsePathBase(absoluteBasePath);
// Configure the HTTP request pipeline.
@@ -70,7 +74,8 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders =
ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto
ForwardedHeaders.XForwardedProto|
ForwardedHeaders.XForwardedHost
});
app.UseHttpsRedirection();

View File

@@ -2,10 +2,10 @@ using System.Text.Json.Serialization;
namespace QuotesTest;
public class Quotes
public class QuotesContainer
{
private readonly List<Quote> _quotes;
public Quotes(List<Quote> quotes)
public readonly List<Quote> _quotes;
public QuotesContainer(List<Quote> quotes)
{
_quotes = quotes;
}

View File

@@ -8,7 +8,7 @@
"Kestrel":{
"Endpoints": {
"http":{
"Url": "http://0.0.0.0:5246"
"Url": "https://0.0.0.0:5246"
}
}
},