Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<PackageVersion Include="Particular.Licensing.Sources" Version="6.1.0" />
<PackageVersion Include="Particular.LicensingComponent.Report" Version="1.0.0" />
<PackageVersion Include="Particular.Obsoletes" Version="1.0.0" />
<PackageVersion Include="Particular.PlatformSample.ServicePulse" Version="2.4.0" />
<PackageVersion Include="Polly.Core" Version="8.5.2" />
<PackageVersion Include="PropertyChanged.Fody" Version="4.1.0" />
<PackageVersion Include="PropertyChanging.Fody" Version="1.30.3" />
Expand Down
4 changes: 3 additions & 1 deletion src/ServiceControl/Hosting/Commands/RunCommand.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
namespace ServiceControl.Hosting.Commands
{
using System.Threading.Tasks;
using Infrastructure.WebApi;
using Microsoft.AspNetCore.Builder;
using NServiceBus;
using Particular.ServiceControl;
using Particular.ServiceControl.Hosting;
using ServiceBus.Management.Infrastructure.Settings;
using ServiceControl;
using ServiceControl.Infrastructure.WebApi;

class RunCommand : AbstractCommand
{
Expand All @@ -25,6 +25,8 @@ public override async Task Execute(HostArguments args, Settings settings)

var app = hostBuilder.Build();
app.UseServiceControl();
app.UseServicePulse();

await app.RunAsync(settings.RootUrl);
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/ServiceControl/Infrastructure/WebApi/AppConstantsMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace ServiceControl.Infrastructure.WebApi
{
using System.Net.Mime;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

class AppConstantsMiddleware
{
readonly RequestDelegate next;
readonly string content;
static AppConstantsMiddleware() => FileVersion = ServiceControlVersion.GetFileVersion();
static readonly string FileVersion;

public AppConstantsMiddleware(RequestDelegate next)
{
this.next = next;

var settings = ServicePulseSettings.GetFromEnvironmentVariables();
var constants = new
{
default_route = settings.DefaultRoute,
service_control_url = "api/",
monitoring_urls = new[] { settings.MonitoringUri.ToString() },
showPendingRetry = settings.ShowPendingRetry,
version = FileVersion,
embedded = true
};
var options = new JsonSerializerOptions { PropertyNamingPolicy = null };

content = $"window.defaultConfig = {JsonSerializer.Serialize(constants, options)}";
}

public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Path.StartsWithSegments("/js/app.constants.js"))
{
context.Response.ContentType = MediaTypeNames.Text.JavaScript;

await context.Response.WriteAsync(content);
return;
}

await next(context);
}
}
}
70 changes: 70 additions & 0 deletions src/ServiceControl/Infrastructure/WebApi/ServicePulseSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Text.Json;

class ServicePulseSettings
{
public required Uri MonitoringUri { get; init; }

public required string DefaultRoute { get; init; }

public required bool ShowPendingRetry { get; init; }


public static ServicePulseSettings GetFromEnvironmentVariables()
{
var monitoringUrls = ParseLegacyMonitoringValue(Environment.GetEnvironmentVariable("MONITORING_URLS"));
var monitoringUrl = Environment.GetEnvironmentVariable("MONITORING_URL");

monitoringUrl ??= monitoringUrls;
monitoringUrl ??= "http://localhost:33633/";

var monitoringUri = monitoringUrl == "!" ? null : new Uri(monitoringUrl);
var defaultRoute = Environment.GetEnvironmentVariable("DEFAULT_ROUTE") ?? "/dashboard";

var showPendingRetryValue = Environment.GetEnvironmentVariable("SHOW_PENDING_RETRY");
bool.TryParse(showPendingRetryValue, out var showPendingRetry);

return new ServicePulseSettings
{
MonitoringUri = monitoringUri,
DefaultRoute = defaultRoute,
ShowPendingRetry = showPendingRetry,
};
}

static string ParseLegacyMonitoringValue(string value)
{
if (value is null)
{
return null;
}

var cleanedValue = value.Replace('\'', '"');
var json = $$"""{"Addresses":{{cleanedValue}}}""";

MonitoringUrls result;

try
{
result = JsonSerializer.Deserialize<MonitoringUrls>(json);
}
catch (JsonException)
{
return null;
}

var addresses = result?.Addresses;

if (addresses is not null && addresses.Length > 0)
{
return addresses[0];
}

return null;
}

class MonitoringUrls
{
public string[] Addresses { get; set; } = [];
}
}
1 change: 1 addition & 0 deletions src/ServiceControl/ServiceControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<PackageReference Include="NServiceBus.CustomChecks" />
<PackageReference Include="NServiceBus.Extensions.Hosting" />
<PackageReference Include="NServiceBus.Extensions.Logging" />
<PackageReference Include="Particular.PlatformSample.ServicePulse"/>
<PackageReference Include="ServiceControl.Contracts" />
<PackageReference Include="System.Reactive.Linq" />
<PackageReference Include="Yarp.ReverseProxy" />
Expand Down
23 changes: 22 additions & 1 deletion src/ServiceControl/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
namespace ServiceControl;

using System;
using System.IO;
using System.Reflection;
using Infrastructure.SignalR;
using Infrastructure.WebApi;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.FileProviders;

public static class WebApplicationExtensions
{
public static void UseServiceControl(this WebApplication app)
public static IApplicationBuilder UseServiceControl(this WebApplication app)
{
app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.All });
app.UseResponseCompression();
Expand All @@ -16,5 +20,22 @@ public static void UseServiceControl(this WebApplication app)
app.MapHub<MessageStreamerHub>("/api/messagestream");
app.UseCors();
app.MapControllers();

return app;
}

public static IApplicationBuilder UseServicePulse(this WebApplication app)
{
var servicePulsePath = Path.Combine(AppContext.BaseDirectory, "platform", "servicepulse", "ServicePulse.dll");
var manifestEmbeddedFileProvider = new ManifestEmbeddedFileProvider(Assembly.LoadFrom(servicePulsePath), "wwwroot");
var fileProvider = new CompositeFileProvider(app.Environment.WebRootFileProvider, manifestEmbeddedFileProvider);
var defaultFilesOptions = new DefaultFilesOptions { FileProvider = fileProvider };
var staticFileOptions = new StaticFileOptions { FileProvider = fileProvider };

app.UseMiddleware<AppConstantsMiddleware>()
.UseDefaultFiles(defaultFilesOptions)
.UseStaticFiles(staticFileOptions);

return app;
}
}