Skip to content

Commit

Permalink
Addressed additional feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercn committed Apr 9, 2021
1 parent eaf3937 commit d5d2f24
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
38 changes: 23 additions & 15 deletions src/Middleware/Spa/SpaProxy/src/SpaProxyLaunchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ internal class SpaProxyLaunchManager : IHostedService, IDisposable
{
private readonly SpaDevelopmentServerOptions _options;
private readonly ILogger<SpaProxyLaunchManager> _logger;
private readonly HttpClient _httpClient = new(new HttpClientHandler()
{
// It's ok for us to do this here since this service is only plugged in during development.
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
});

private Process? _spaProcess;
private bool _disposedValue;
Expand All @@ -39,50 +34,63 @@ public SpaProxyLaunchManager(ILogger<SpaProxyLaunchManager> logger)

public async Task StartAsync(CancellationToken cancellationToken)
{
var httpClient = new HttpClient(new HttpClientHandler()
{
// It's ok for us to do this here since this service is only plugged in during development.
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
});

_logger.LogInformation("Starting SPA development server");
var running = await ProbeSpaDevelopmentServerUrl(cancellationToken);
var running = await ProbeSpaDevelopmentServerUrl(httpClient, cancellationToken);
if (running)
{
_logger.LogInformation($"Found SPA development server running at {_options.ServerUrl}");
}
else
{
_logger.LogInformation($"No SPA development server running at {_options.ServerUrl} found.");
await StartSpaProcessAndProbeForLiveness(cancellationToken);
await StartSpaProcessAndProbeForLiveness(httpClient, cancellationToken);
}
}

private async Task<bool> ProbeSpaDevelopmentServerUrl(CancellationToken cancellationToken)
private async Task<bool> ProbeSpaDevelopmentServerUrl(HttpClient httpClient, CancellationToken cancellationToken)
{
using var timeout = new CancellationTokenSource(1000);
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, cancellationToken);
try
{
var response = await _httpClient.GetAsync(_options.ServerUrl, cancellationToken);
var response = await httpClient.GetAsync(_options.ServerUrl, cancellationTokenSource.Token);
var running = response.IsSuccessStatusCode;
return running;
}
catch (HttpRequestException httpException)
catch (Exception exception) when (exception is HttpRequestException || exception is TaskCanceledException)
{
_logger.LogDebug(httpException, "Failed to connect to the SPA Development proxy.");
_logger.LogDebug(exception, "Failed to connect to the SPA Development proxy.");
return false;
}
}

private async Task StartSpaProcessAndProbeForLiveness(CancellationToken cancellationToken)
private async Task StartSpaProcessAndProbeForLiveness(HttpClient httpClient, CancellationToken cancellationToken)
{
LaunchDevelopmentProxy();
var sw = Stopwatch.StartNew();
var livenessProbeSucceeded = false;
var maxTimeoutReached = false;
while (_spaProcess != null && !_spaProcess.HasExited && !livenessProbeSucceeded && !maxTimeoutReached)
while (_spaProcess != null && !_spaProcess.HasExited && !maxTimeoutReached)
{
livenessProbeSucceeded = await ProbeSpaDevelopmentServerUrl(cancellationToken);
livenessProbeSucceeded = await ProbeSpaDevelopmentServerUrl(httpClient, cancellationToken);
if (livenessProbeSucceeded)
{
break;
}

if (cancellationToken.IsCancellationRequested)
{
return;
}

maxTimeoutReached = sw.Elapsed >= _options.MaxTimeout;
await Task.Delay(1000);
await Task.Delay(1000, cancellationToken);
}

if (_spaProcess == null || _spaProcess.HasExited)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ const baseFolder =
? `${process.env.APPDATA}/ASP.NET/https`
: `${process.env.HOME}/.aspnet/https`;

const certificateName = process.argv.map(arg => arg.match(/--name=(?<value>.+)/i))
.filter(Boolean)
.reduce((previous, current) => previous || current.groups.value, undefined) ||
process.env.npm_package_name;
const certificateArg = process.argv.map(arg => arg.match(/--name=(?<value>.+)/i)).filter(Boolean)[0];
const certificateName = certificateArg ? certificateArg.groups.value : process.env.npm_package_name;

if (!certificateName) {
console.error('Invalid certificate name. Run this script in the context of an npm/yarn script or pass --name=<<app>> explicitly.')
Expand All @@ -22,19 +20,14 @@ const certFilePath = path.join(baseFolder, `${certificateName}.pem`);
const keyFilePath = path.join(baseFolder, `${certificateName}.key`);

if (!fs.existsSync(certFilePath) || !fs.existsSync(keyFilePath)) {
spawn(
'dotnet',
[
'dev-certs',
'https',
'--export-path',
certFilePath,
'--format',
'Pem',
'--no-password',
],
{
stdio: 'inherit',
}
).on('exit', (code) => process.exit(code));
spawn('dotnet', [
'dev-certs',
'https',
'--export-path',
certFilePath,
'--format',
'Pem',
'--no-password',
], { stdio: 'inherit', })
.on('exit', (code) => process.exit(code));
}

0 comments on commit d5d2f24

Please sign in to comment.