5 Commits

Author SHA1 Message Date
swcs a8f07fc203 1.2 release 2024-08-20 14:20:27 +02:00
swcs 0f1df639f8 dbg type = embedded 2024-08-18 23:50:09 +02:00
swcs 2ce477f9b8 remove default useStaticFiles call 2024-05-24 13:23:21 +02:00
swcs fcfec6b2f9 new working directory allows absolute paths; allow multiple proxies with named configs 2024-02-29 13:09:47 +01:00
swcs da23dc2815 update image src 2022-11-11 12:07:59 +01:00
6 changed files with 73 additions and 41 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
![ViteProxy](/viteproxy.png "ViteProxy")
![ViteProxy](https://raw.githubusercontent.com/ceee/ViteProxy/main/viteproxy.png "ViteProxy")
## Introduction
+5 -4
View File
@@ -5,13 +5,14 @@
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>ViteProxy</RootNamespace>
<AssemblyName>ViteProxy</AssemblyName>
<Version>0.0.0</Version>
<Version>1.2.0</Version>
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup>
<Product>ViteProxy</Product>
<Title>ViteProxy</Title>
<Copyright>Copyright © Tobias Klika 2022</Copyright>
<Copyright>Copyright © Tobias Klika 2024</Copyright>
<Tags>Vite, npm, middleware, server, development</Tags>
<Authors>Tobias Klika</Authors>
<Description>ViteProxy is a proxy for vite projects within ASP.NET Core.</Description>
@@ -20,7 +21,7 @@
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<PackageId>ViteProxy</PackageId>
<PackageProjectUrl>https://github.com/ceee/ViteProxy</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/ceee/ViteProxy/main/viteproxy.png</PackageIconUrl>
<PackageIconUrl>https://raw.githubusercontent.com/ceee/ViteProxy/main/viteproxy-logo.png</PackageIconUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
@@ -33,7 +34,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
</Project>
+21 -19
View File
@@ -8,40 +8,42 @@ namespace ViteProxy;
public static class ViteProxyApplicationBuilderExtensions
{
public static IApplicationBuilder UseViteStaticFiles(this IApplicationBuilder app)
/// <summary>
/// Provides static files from the vite working directory or a custom directory or from the named vite settings
/// </summary>
/// <param name="app">The app builder</param>
/// <param name="path">Provide a file path for static file delivery</param>
/// <param name="configName">When vite proxy is added via named options these can be used here for correct retrieval of the vite working directory</param>
/// <returns>The app builde</returns>
public static IApplicationBuilder UseViteStaticFiles(this IApplicationBuilder app, string path = null, string configName = null)
{
IWebHostEnvironment env = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
IOptions<ViteProxyOptions> options = app.ApplicationServices.GetRequiredService<IOptions<ViteProxyOptions>>();
string workingDirectory = (options.Value.WorkingDirectory ?? String.Empty).Trim('/');
if (workingDirectory == null)
if (path == null && configName == null)
{
return app;
IOptions<ViteProxyOptions> options = app.ApplicationServices.GetRequiredService<IOptions<ViteProxyOptions>>();
path = options.Value.WorkingDirectory;
}
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
if (configName != null)
{
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, workingDirectory))
});
return app;
}
public static IApplicationBuilder UseViteStaticFiles(this IApplicationBuilder app, string path)
{
IWebHostEnvironment env = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
using IServiceScope scope = app.ApplicationServices.CreateScope();
IOptionsSnapshot<ViteProxyOptions> optionsSnapschat = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<ViteProxyOptions>>();
ViteProxyOptions options = optionsSnapschat.Get(configName);
path = options.WorkingDirectory;
}
if (path == null)
{
return app;
}
app.UseStaticFiles();
// TODO build full path
ViteWorkingDirectory workingDirectory = new(env, path);
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, path))
FileProvider = new PhysicalFileProvider(workingDirectory.Path)
});
return app;
+10 -13
View File
@@ -8,15 +8,17 @@ namespace ViteProxy;
public class ViteProxyService : IHostedService
{
ViteProxyOptions options;
ViteWorkingDirectory workingDirectory;
ILogger<ViteProxyService> logger;
string workingDirectory;
bool isRunning = false;
public ViteProxyService(IWebHostEnvironment env, ILogger<ViteProxyService> logger, IOptions<ViteProxyOptions> options)
public ViteProxyService(IWebHostEnvironment env, ILogger<ViteProxyService> logger, IOptions<ViteProxyOptions> options) : this(env, logger, options.Value ?? new()) { }
public ViteProxyService(IWebHostEnvironment env, ILogger<ViteProxyService> logger, ViteProxyOptions options)
{
this.options = options.Value ?? new();
this.workingDirectory = Path.Combine(env.ContentRootPath, (this.options.WorkingDirectory ?? String.Empty).Trim('/'));
this.options = options ?? new();
this.workingDirectory = new ViteWorkingDirectory(env, options.WorkingDirectory);
this.logger = logger;
}
@@ -30,16 +32,11 @@ public class ViteProxyService : IHostedService
}
// locate npm version and throw if it is not installed
Version npmVersion = await FindNpmVersion();
if (npmVersion == null)
{
throw new Exception("Please install node+npm to use the vite dev service (https://www.npmjs.com/)");
}
Version npmVersion = await FindNpmVersion() ?? throw new Exception("Please install node+npm to use the vite dev service (https://www.npmjs.com/)");
// start vite server
ProcessProxy viteProcess = await StartDevServer(options.Port);
logger.LogInformation("vite listening on: http://localhost:{port}", options.Port);
logger.LogInformation("vite listening on: http://localhost:{port} (path: {workingDirectory})", options.Port, this.workingDirectory.Path);
}
public Task StopAsync(CancellationToken cancellationToken)
@@ -55,7 +52,7 @@ public class ViteProxyService : IHostedService
{
Version version = null;
ProcessProxy process = new ProcessProxy(workingDirectory, "npm").Argument("-v").Capture((value, err) =>
ProcessProxy process = new ProcessProxy(workingDirectory.Path, "npm").Argument("-v").Capture((value, err) =>
{
if (version == null && !value.Contains("not recognized") && Version.TryParse(value, out Version _version))
{
@@ -79,7 +76,7 @@ public class ViteProxyService : IHostedService
PidUtils.KillPort((ushort)port, true);
// create and run the vite script
ProcessProxy process = new ProcessProxy(workingDirectory, "npm", options.ForwardLog)
ProcessProxy process = new ProcessProxy(workingDirectory.Path, "npm", options.ForwardLog)
.Argument("run " + options.ScriptName)
.EnvVar("PORT", port.ToString())
.Capture(CaptureLog);
+11 -4
View File
@@ -1,5 +1,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace ViteProxy;
@@ -28,12 +31,16 @@ public static class ViteProxyServiceCollectionExtensions
}
public static IServiceCollection AddViteProxy(this IServiceCollection services, IConfiguration namedConfigurationSection)
public static IServiceCollection AddViteProxy(this IServiceCollection services, string configName, IConfiguration namedConfigurationSection)
{
services.AddLogging();
services.AddOptions();
services.AddHostedService<ViteProxyService>();
services.AddOptions<ViteProxyOptions>().Bind(namedConfigurationSection);
services.AddHostedService((svc) => new ViteProxyService(
env: svc.GetService<IWebHostEnvironment>(),
logger: svc.GetService<ILogger<ViteProxyService>>(),
options: svc.GetService<IOptionsSnapshot<ViteProxyOptions>>().Get(configName)
));
services.AddOptions<ViteProxyOptions>(configName).Bind(namedConfigurationSection);
return services;
}
+25
View File
@@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Hosting;
namespace ViteProxy;
internal class ViteWorkingDirectory
{
public string Path { get; protected set; }
public string InputPath { get; protected set; }
public ViteWorkingDirectory(IWebHostEnvironment env, string inputPath)
{
InputPath = inputPath;
string path = inputPath.Replace('\\', '/').Trim().TrimEnd('/');
if (System.IO.Path.IsPathFullyQualified(path))
{
Path = path;
}
else
{
Path = System.IO.Path.Combine(env.ContentRootPath, path);
}
}
}