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]")] [Route("[controller]")]
public class QuotesController : Controller public class QuotesController : Controller
{ {
private readonly Quotes _quotes; private readonly QuotesContainer _quotes;
public QuotesController(Quotes quotes) public QuotesController(QuotesContainer quotes)
{ {
_quotes = quotes; _quotes = quotes;
} }

View File

@@ -1,18 +1,24 @@
using System.Net;
using System.Text.Json; using System.Text.Json;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.OpenApi; using Microsoft.OpenApi;
using QuotesTest; 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); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddControllersWithViews(); 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."); 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<Quotes>(); builder.Services.AddSingleton<QuotesContainer>();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
List<string>? ApiKeys = builder.Configuration.GetSection("ApiKeys").Get<List<string>>(); List<string>? ApiKeys = builder.Configuration.GetSection("ApiKeys").Get<List<string>>();
@@ -23,7 +29,9 @@ builder.Services.AddOpenApi(options =>
{ {
if (ApiKeys is null) if (ApiKeys is null)
return Task.CompletedTask; return Task.CompletedTask;
document.Servers = [
new OpenApiServer { Url = fullBasePath }
];
document.Components ??= new(); document.Components ??= new();
document.Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>(); document.Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>();
@@ -51,10 +59,6 @@ builder.Services.AddOpenApi(options =>
}); });
var app = builder.Build(); 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); app.UsePathBase(absoluteBasePath);
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
@@ -70,7 +74,8 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions
{ {
ForwardedHeaders = ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto ForwardedHeaders.XForwardedProto|
ForwardedHeaders.XForwardedHost
}); });
app.UseHttpsRedirection(); app.UseHttpsRedirection();

View File

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

View File

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