Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.

Commit 5ed9899

Browse files
authored
Merge pull request #53 from T0shik/feature/https-support
HTTPS Support
2 parents c7ee9ba + cd87d5d commit 5ed9899

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,36 @@ You may also need to add the following tasks to your csproj file. This are simil
159159

160160
```
161161

162+
## Setting up for HTTPS
163+
164+
For consistent schemas & for hot reload to not break while using https you will need to setup
165+
your vue app, before enabling the `https` flag.
166+
167+
1. Get the `mkcert` tool following the installation instructions on their [github](https://github.com/FiloSottile/mkcert) repository or their [releases](https://github.com/FiloSottile/mkcert/releases)
168+
2. Install root CA `mkcert -install`
169+
3. Generate the certificates `mkcert localhost 127.0.0.1 <local ipv4> ::1`
170+
4. Add certificates to `vue.config.js`
171+
```
172+
const fs = require('fs')
173+
174+
module.exports = {
175+
devServer: {
176+
https: {
177+
key: fs.readFileSync('./certs/localhost+3-key.pem'),
178+
cert: fs.readFileSync('./certs/localhost+3.pem'),
179+
}
180+
}
181+
}
182+
```
183+
5. you can now set the https option to true.
184+
```
185+
endpoints.MapToVueCliProxy(
186+
...
187+
https: true
188+
...
189+
);
190+
```
191+
162192
## History
163193

164194
Due to the discussion [here](https://github.com/aspnet/JavaScriptServices/pull/1726), it was decided to not be included in the Microsoft owned package.

src/VueCliMiddleware/VueDevelopmentServerMiddleware.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal static class VueCliMiddleware
1818

1919
public static void Attach(
2020
ISpaBuilder spaBuilder,
21-
string scriptName, int port = 8080, ScriptRunnerType runner = ScriptRunnerType.Npm, string regex = DefaultRegex, bool forceKill = false)
21+
string scriptName, int port = 8080, bool https = false, ScriptRunnerType runner = ScriptRunnerType.Npm, string regex = DefaultRegex, bool forceKill = false)
2222
{
2323
var sourcePath = spaBuilder.Options.SourcePath;
2424
if (string.IsNullOrEmpty(sourcePath))
@@ -39,10 +39,8 @@ public static void Attach(
3939
// Everything we proxy is hardcoded to target http://localhost because:
4040
// - the requests are always from the local machine (we're not accepting remote
4141
// requests that go directly to the vue-cli server)
42-
// - given that, there's no reason to use https, and we couldn't even if we
43-
// wanted to, because in general the vue-cli server has no certificate
4442
var targetUriTask = portTask.ContinueWith(
45-
task => new UriBuilder("http", "localhost", task.Result).Uri);
43+
task => new UriBuilder(https ? "https" : "http", "localhost", task.Result).Uri);
4644

4745
SpaProxyingExtensions.UseProxyToSpaDevelopmentServer(spaBuilder, () =>
4846
{

src/VueCliMiddleware/VueDevelopmentServerMiddlewareExtensions.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ public static class VueCliMiddlewareExtensions
2222
/// <param name="spaBuilder">The <see cref="ISpaBuilder"/>.</param>
2323
/// <param name="npmScript">The name of the script in your package.json file that launches the vue-cli server.</param>
2424
/// <param name="port">Specify vue cli server port number. If &lt; 80, uses random port. </param>
25+
/// <param name="https">Specify vue cli server schema </param>
2526
/// <param name="runner">Specify the runner, Npm and Yarn are valid options. Yarn support is HIGHLY experimental.</param>
2627
/// <param name="regex">Specify a custom regex string to search for in the log indicating proxied server is running. VueCli: "running at", QuasarCli: "Compiled successfully"</param>
2728
public static void UseVueCli(
2829
this ISpaBuilder spaBuilder,
2930
string npmScript = "serve",
3031
int port = 8080,
32+
bool https = false,
3133
ScriptRunnerType runner = ScriptRunnerType.Npm,
3234
string regex = VueCliMiddleware.DefaultRegex,
3335
bool forceKill = false)
@@ -44,7 +46,7 @@ public static void UseVueCli(
4446
throw new InvalidOperationException($"To use {nameof(UseVueCli)}, you must supply a non-empty value for the {nameof(SpaOptions.SourcePath)} property of {nameof(SpaOptions)} when calling {nameof(SpaApplicationBuilderExtensions.UseSpa)}.");
4547
}
4648

47-
VueCliMiddleware.Attach(spaBuilder, npmScript, port, runner: runner, regex: regex, forceKill: forceKill);
49+
VueCliMiddleware.Attach(spaBuilder, npmScript, port, https: https, runner: runner, regex: regex, forceKill: forceKill);
4850
}
4951

5052

@@ -54,12 +56,13 @@ public static IEndpointConventionBuilder MapToVueCliProxy(
5456
SpaOptions options,
5557
string npmScript = "serve",
5658
int port = 8080,
59+
bool https = false,
5760
ScriptRunnerType runner = ScriptRunnerType.Npm,
5861
string regex = VueCliMiddleware.DefaultRegex,
5962
bool forceKill = false)
6063
{
6164
if (pattern == null) { throw new ArgumentNullException(nameof(pattern)); }
62-
return endpoints.MapFallback(pattern, CreateProxyRequestDelegate(endpoints, options, npmScript, port, runner, regex, forceKill));
65+
return endpoints.MapFallback(pattern, CreateProxyRequestDelegate(endpoints, options, npmScript, port, https, runner, regex, forceKill));
6366
}
6467

6568
public static IEndpointConventionBuilder MapToVueCliProxy(
@@ -68,38 +71,41 @@ public static IEndpointConventionBuilder MapToVueCliProxy(
6871
string sourcePath,
6972
string npmScript = "serve",
7073
int port = 8080,
74+
bool https = false,
7175
ScriptRunnerType runner = ScriptRunnerType.Npm,
7276
string regex = VueCliMiddleware.DefaultRegex,
7377
bool forceKill = false)
7478
{
7579
if (pattern == null) { throw new ArgumentNullException(nameof(pattern)); }
7680
if (sourcePath == null) { throw new ArgumentNullException(nameof(sourcePath)); }
77-
return endpoints.MapFallback(pattern, CreateProxyRequestDelegate(endpoints, new SpaOptions { SourcePath = sourcePath }, npmScript, port, runner, regex, forceKill));
81+
return endpoints.MapFallback(pattern, CreateProxyRequestDelegate(endpoints, new SpaOptions { SourcePath = sourcePath }, npmScript, port, https, runner, regex, forceKill));
7882
}
7983

8084
public static IEndpointConventionBuilder MapToVueCliProxy(
8185
this IEndpointRouteBuilder endpoints,
8286
SpaOptions options,
8387
string npmScript = "serve",
8488
int port = 8080,
89+
bool https = false,
8590
ScriptRunnerType runner = ScriptRunnerType.Npm,
8691
string regex = VueCliMiddleware.DefaultRegex,
8792
bool forceKill = false)
8893
{
89-
return endpoints.MapFallback("{*path}", CreateProxyRequestDelegate(endpoints, options, npmScript, port, runner, regex, forceKill));
94+
return endpoints.MapFallback("{*path}", CreateProxyRequestDelegate(endpoints, options, npmScript, port, https, runner, regex, forceKill));
9095
}
9196

9297
public static IEndpointConventionBuilder MapToVueCliProxy(
9398
this IEndpointRouteBuilder endpoints,
9499
string sourcePath,
95100
string npmScript = "serve",
96101
int port = 8080,
102+
bool https = false,
97103
ScriptRunnerType runner = ScriptRunnerType.Npm,
98104
string regex = VueCliMiddleware.DefaultRegex,
99105
bool forceKill = false)
100106
{
101107
if (sourcePath == null) { throw new ArgumentNullException(nameof(sourcePath)); }
102-
return endpoints.MapFallback("{*path}", CreateProxyRequestDelegate(endpoints, new SpaOptions { SourcePath = sourcePath }, npmScript, port, runner, regex, forceKill));
108+
return endpoints.MapFallback("{*path}", CreateProxyRequestDelegate(endpoints, new SpaOptions { SourcePath = sourcePath }, npmScript, port, https, runner, regex, forceKill));
103109
}
104110

105111

@@ -109,6 +115,7 @@ private static RequestDelegate CreateProxyRequestDelegate(
109115
SpaOptions options,
110116
string npmScript = "serve",
111117
int port = 8080,
118+
bool https = false,
112119
ScriptRunnerType runner = ScriptRunnerType.Npm,
113120
string regex = VueCliMiddleware.DefaultRegex,
114121
bool forceKill = false)
@@ -139,7 +146,7 @@ private static RequestDelegate CreateProxyRequestDelegate(
139146

140147
if (!string.IsNullOrWhiteSpace(npmScript))
141148
{
142-
opt.UseVueCli(npmScript, port, runner, regex, forceKill);
149+
opt.UseVueCli(npmScript, port, https, runner, regex, forceKill);
143150
}
144151
});
145152

0 commit comments

Comments
 (0)