From 253d998b68b5808d6fd2d9731255616238fcdb71 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Sat, 19 Jul 2025 11:38:47 -0500 Subject: [PATCH] refactor(wrapper): changes to handle docs and frontned now --- LstWrapper/Program.cs | 90 +++++++++++++++++-------------------------- LstWrapper/web.config | 55 ++++++++++++++++++++------ 2 files changed, 78 insertions(+), 67 deletions(-) diff --git a/LstWrapper/Program.cs b/LstWrapper/Program.cs index ec718ab..1fd8bed 100644 --- a/LstWrapper/Program.cs +++ b/LstWrapper/Program.cs @@ -1,85 +1,65 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DependencyInjection; -using System.Net.Http; - var builder = WebApplication.CreateBuilder(args); -// to build the binary dotnet publish -c Release -o ./publish -// Go backend -builder.Services.AddHttpClient("GoBackend", client => -{ - client.BaseAddress = new Uri("http://localhost:8080"); - client.Timeout = TimeSpan.FromSeconds(30); -}); -// Node frontend -builder.Services.AddHttpClient("NodeFrontend", client => -{ - client.BaseAddress = new Uri("http://localhost:3000"); - client.Timeout = TimeSpan.FromSeconds(30); +// Configure clients +builder.Services.AddHttpClient("GoBackend", client => { + client.BaseAddress = new Uri("http://localhost:8080"); }); var app = builder.Build(); -app.UseStaticFiles(); +// Handle trailing slash redirects +app.Use(async (context, next) => { + if (context.Request.Path.Equals("/lst", StringComparison.OrdinalIgnoreCase)) { + context.Response.Redirect("/lst/", permanent: true); + return; + } + await next(); +}); -app.Use((Func, Task>)(async (context, next) => -{ - var clientFactory = context.RequestServices.GetRequiredService(); - - var isApiRequest = - context.Request.Path.StartsWithSegments("/api") || - context.Request.Path.StartsWithSegments("/graphql") || - context.Request.Path.StartsWithSegments("/auth") || - !context.Request.Method.Equals("GET", StringComparison.OrdinalIgnoreCase); - - var client = clientFactory.CreateClient(isApiRequest ? "GoBackend" : "NodeFrontend"); - - try - { - var requestUri = context.Request.Path + context.Request.QueryString; +// Proxy all requests to Go backend +app.Use(async (context, next) => { + // Skip special paths + if (context.Request.Path.StartsWithSegments("/.well-known")) { + await next(); + return; + } + var client = context.RequestServices.GetRequiredService() + .CreateClient("GoBackend"); + + try { var request = new HttpRequestMessage( new HttpMethod(context.Request.Method), - requestUri); + context.Request.Path + context.Request.QueryString); - foreach (var header in context.Request.Headers) - { - if (!request.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray())) - { + // Copy headers + foreach (var header in context.Request.Headers) { + if (!request.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray())) { request.Content ??= new StreamContent(context.Request.Body); request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()); } } - if (context.Request.ContentLength > 0 || context.Request.Headers.ContainsKey("Transfer-Encoding")) - { + if (context.Request.ContentLength > 0) { request.Content = new StreamContent(context.Request.Body); } - var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted); - + var response = await client.SendAsync(request); context.Response.StatusCode = (int)response.StatusCode; - - foreach (var header in response.Headers) - { + + foreach (var header in response.Headers) { context.Response.Headers[header.Key] = header.Value.ToArray(); } - foreach (var header in response.Content.Headers) - { - context.Response.Headers[header.Key] = header.Value.ToArray(); + if (response.Content.Headers.ContentType != null) { + context.Response.ContentType = response.Content.Headers.ContentType.ToString(); } - context.Response.Headers.Remove("transfer-encoding"); - await response.Content.CopyToAsync(context.Response.Body); } - catch (HttpRequestException ex) - { - context.Response.StatusCode = isApiRequest ? 503 : 502; - await context.Response.WriteAsync($"{(isApiRequest ? "Go API" : "Frontend")} unavailable: {ex.Message}"); + catch (HttpRequestException) { + context.Response.StatusCode = 502; } -})); +}); app.Run(); \ No newline at end of file diff --git a/LstWrapper/web.config b/LstWrapper/web.config index 4957891..fd14c7d 100644 --- a/LstWrapper/web.config +++ b/LstWrapper/web.config @@ -1,15 +1,46 @@ - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file