Skip to content

Commit

Permalink
add support for new __devtools__, change name in ext-methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
mumrich committed Apr 30, 2024
1 parent 8db3513 commit 279ccf6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 49 deletions.
6 changes: 3 additions & 3 deletions Mumrich.SpaDevMiddleware.Domain/Models/SpaSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;

using Mumrich.SpaDevMiddleware.Domain.Types;

namespace Mumrich.SpaDevMiddleware.Domain.Models
Expand Down Expand Up @@ -76,7 +75,8 @@ public class SpaSettings
/// The RegExp for detecting SPA-Assets requests.
/// </summary>
//language=regexp
public string SpaAssetsExpression { get; set; } = "^(src|node_modules|favicon.+|@[a-zA-Z]+|.*vite.*|.*\\.json|.*\\.js|.*\\.css)$";
public string SpaAssetsExpression { get; set; } =
"^(src|node_modules|favicon.+|@[a-zA-Z]+|.*vite.*|.*\\.json|.*\\.js|.*\\.css|__devtools__.*)$";

/// <summary>
/// The RegExp for detecting SPA-Root requests.
Expand All @@ -90,4 +90,4 @@ public class SpaSettings
/// </summary>
public string SpaRootPath { get; set; }
}
}
}
103 changes: 57 additions & 46 deletions Mumrich.SpaDevMiddleware/Extensions/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,34 @@
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using Mumrich.SpaDevMiddleware.Domain.Contracts;
using Mumrich.SpaDevMiddleware.Domain.Models;
using Mumrich.SpaDevMiddleware.Domain.Types;
using Mumrich.SpaDevMiddleware.Helpers;
using Mumrich.SpaDevMiddleware.HostedServices;

using Newtonsoft.Json.Linq;

namespace Mumrich.SpaDevMiddleware.Extensions
{
/// <summary>
/// Extension methods for <see cref="WebApplicationBuilder" />.
/// </summary>
public static class WebApplicationBuilderExtensions
{
[SuppressMessage("Usage", "ASP0013:Suggest switching from using Configure methods to WebApplicationBuilder.Configuration")]
public static void RegisterSinglePageAppDevMiddleware(this WebApplicationBuilder builder, ISpaDevServerSettings spaDevServerSettings)
[Obsolete("use 'SetupSpaDevMiddleware' instead. This method will be removed eventually!")]
[SuppressMessage(
"Usage",
"ASP0013:Suggest switching from using Configure methods to WebApplicationBuilder.Configuration"
)]
public static void RegisterSinglePageAppDevMiddleware(
this WebApplicationBuilder builder,
ISpaDevServerSettings spaDevServerSettings
)
{
if (!builder.Environment.IsDevelopment())
{
Expand All @@ -42,7 +48,8 @@ public static void RegisterSinglePageAppDevMiddleware(this WebApplicationBuilder
{
BundlerType.ViteJs => GetViteJsYarpConfig(appPath, guid, spaSettings),
BundlerType.QuasarCli => GetQuasarYarpConfig(appPath, guid, spaSettings),
BundlerType.Custom => JObject.FromObject(new { ReverseProxy = spaSettings.CustomYarpConfiguration }),
BundlerType.Custom
=> JObject.FromObject(new { ReverseProxy = spaSettings.CustomYarpConfiguration }),
_ => throw new NotImplementedException()
};

Expand All @@ -63,18 +70,27 @@ public static void RegisterSinglePageAppDevMiddleware(this WebApplicationBuilder
builder.Services.AddReverseProxy().LoadFromConfig(reverseProxyConfig);
}

/// <summary>
/// Setup all SPA-Dev-Servers defined in <see cref="ISpaDevServerSettings" />.
/// </summary>
/// <param name="webSpplicationBuilder"></param>
/// <param name="spaDevServerSettings"></param>
public static void SetupSpaDevMiddleware(
this WebApplicationBuilder webSpplicationBuilder,
ISpaDevServerSettings spaDevServerSettings
)
{
webSpplicationBuilder.RegisterSinglePageAppDevMiddleware(spaDevServerSettings);
}

private static JObject GetQuasarYarpConfig(string appPath, Guid guid, SpaSettings spaSettings)
{
return GetYarpConfig(
appPath,
spaSettings,
new Dictionary<string, string>
{
{
$"SpaRoot-{guid}", "{**any}"
}
},
guid);
new Dictionary<string, string> { { $"SpaRoot-{guid}", "{**any}" } },
guid
);
}

private static JObject GetViteJsYarpConfig(string appPath, Guid guid, SpaSettings spaSettings)
Expand All @@ -84,29 +100,28 @@ private static JObject GetViteJsYarpConfig(string appPath, Guid guid, SpaSetting
spaSettings,
new Dictionary<string, string>
{
{
$"SpaRoot-{guid}", $"{{filename:regex({spaSettings.SpaRootExpression})?}}"
},
{
$"SpaAssets-{guid}", $"{{name:regex({spaSettings.SpaAssetsExpression})}}/{{**any}}"
}
{ $"SpaRoot-{guid}", $"{{filename:regex({spaSettings.SpaRootExpression})?}}" },
{ $"SpaAssets-{guid}", $"{{name:regex({spaSettings.SpaAssetsExpression})}}/{{**any}}" }
},
guid);
guid
);
}

private static JObject GetYarpConfig(
string appPath,
SpaSettings spaSettings,
Dictionary<string, string> routeMatches,
Guid guid)
Guid guid
)
{
appPath = AppPathHelper.GetValidIntermediateAppPath(appPath);
string clusterId = $"spa-cluster-{guid}";
JObject rootConfig = JObject.FromObject(new
{
ReverseProxy = new
JObject rootConfig = JObject.FromObject(
new
{
Clusters = new Dictionary<string, object>
ReverseProxy = new
{
Clusters = new Dictionary<string, object>
{
{
clusterId,
Expand All @@ -116,17 +131,15 @@ private static JObject GetYarpConfig(
{
{
$"spa-cluster-destination-{guid}",
new
{
Address = spaSettings.DevServerAddress
}
new { Address = spaSettings.DevServerAddress }
}
}
}
}
}
}
}
});
);

foreach ((string route, string path) in routeMatches)
{
Expand All @@ -136,15 +149,17 @@ private static JObject GetYarpConfig(
return rootConfig;
}

private static JObject GetYarpRoute(string route, string clusterId, string path, SpaSettings spaSettings)
private static JObject GetYarpRoute(
string route,
string clusterId,
string path,
SpaSettings spaSettings
)
{
dynamic proxyRouteConfig = new ExpandoObject();

proxyRouteConfig.ClusterId = clusterId;
proxyRouteConfig.Match = new
{
Path = path
};
proxyRouteConfig.Match = new { Path = path };

if (spaSettings.AuthorizationPolicy != null)
{
Expand All @@ -156,19 +171,15 @@ private static JObject GetYarpRoute(string route, string clusterId, string path,
proxyRouteConfig.CorsPolicy = spaSettings.CorsPolicy;
}

return JObject.FromObject(new
{
ReverseProxy = new
return JObject.FromObject(
new
{
Routes = new Dictionary<string, object>
ReverseProxy = new
{
{
route,
proxyRouteConfig
}
Routes = new Dictionary<string, object> { { route, proxyRouteConfig } }
}
}
});
);
}
}
}
}

0 comments on commit 279ccf6

Please sign in to comment.