Skip to content

Commit db35e72

Browse files
Implement combined HubService for Hub API + Service Core
1 parent 8fe601c commit db35e72

File tree

7 files changed

+356
-0
lines changed

7 files changed

+356
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<RuntimeIdentifiers>linux-x64</RuntimeIdentifiers>
8+
<ContainerRuntimeIdentifier>linux-x64</ContainerRuntimeIdentifier>
9+
<EnableSdkContainerDebugging>True</EnableSdkContainerDebugging>
10+
<ContainerBaseImage>mcr.microsoft.com/dotnet/aspnet:9.0</ContainerBaseImage>
11+
<UserSecretsId>c6402661-abaa-463e-ab79-e40d979f8b77</UserSecretsId>
12+
<DockerfileContext>..\..\..\..\certify-manager</DockerfileContext>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
17+
18+
<!-- included to prevent conflict with older versions in plugins-->
19+
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="9.0.1" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<ContainerPort Include="8081" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<ProjectReference Include="..\..\Certify.Aspire\Certify.Aspire.ServiceDefaults\Certify.Aspire.ServiceDefaults.csproj" />
28+
<ProjectReference Include="..\Certify.Server.Core\Certify.Server.Core\Certify.Server.Core.csproj">
29+
<PrivateAssets>all</PrivateAssets>
30+
</ProjectReference>
31+
<ProjectReference Include="..\Certify.Server.Hub.Api\Certify.Server.Hub.Api.csproj">
32+
<PrivateAssets>all</PrivateAssets>
33+
</ProjectReference>
34+
</ItemGroup>
35+
36+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@Certify.Server.HubService_HostAddress = https://localhost:7187
2+
3+
GET {{Certify.Server.HubService_HostAddress}}/api/v1/health/
4+
Accept: application/json
5+
6+
###
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using Certify.Client;
2+
using Certify.Server.Hub.Api.Middleware;
3+
using Certify.Server.Hub.Api.Services;
4+
using Certify.Server.Hub.Api.SignalR;
5+
using Certify.Server.Hub.Api.SignalR.ManagementHub;
6+
using Certify.Server.HubService.Services;
7+
using Microsoft.AspNetCore.Mvc.ApplicationParts;
8+
using Microsoft.AspNetCore.SignalR;
9+
using Microsoft.AspNetCore.StaticFiles;
10+
11+
var builder = WebApplication.CreateBuilder(args);
12+
13+
builder.AddServiceDefaults();
14+
15+
// Add services to the container
16+
17+
var assembly = typeof(Certify.Server.Hub.Api.Startup).Assembly;
18+
var part = new AssemblyPart(assembly);
19+
20+
builder.Services
21+
.AddMemoryCache()
22+
.AddTokenAuthentication(builder.Configuration)
23+
.AddAuthorization()
24+
.AddControllers()
25+
.ConfigureApplicationPartManager(apm => apm.ApplicationParts.Add(part));
26+
27+
builder.Services
28+
.AddRouting(r => r.LowercaseUrls = true)
29+
.AddSignalR(opt => opt.MaximumReceiveMessageSize = null)
30+
.AddMessagePackProtocol();
31+
32+
builder.Services.AddDataProtection(a =>
33+
{
34+
a.ApplicationDiscriminator = "certify";
35+
});
36+
37+
builder.Services.AddResponseCompression();
38+
39+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
40+
builder.Services.AddOpenApi();
41+
42+
var certifyManager = new Certify.Management.CertifyManager();
43+
await certifyManager.Init();
44+
45+
// setup public/hub api
46+
builder.Services.AddSingleton<Certify.Management.ICertifyManager>(certifyManager);
47+
48+
builder.Services.AddTransient(typeof(ICertifyInternalApiClient), typeof(CertifyHubService));
49+
50+
// setup server core
51+
builder.Services.AddSingleton<IInstanceManagementStateProvider, InstanceManagementStateProvider>();
52+
53+
builder.Services.AddTransient<ManagementAPI>();
54+
55+
// used to directly talk back to the management server process instead of connecting back via SignalR
56+
builder.Services.AddTransient<IInstanceManagementHub, DirectInstanceManagementHub>();
57+
builder.Services.AddTransient<IManagementServerClient, DirectManagementServerClient>();
58+
59+
builder.Services.AddHostedService<ManagementWorker>();
60+
61+
// build app
62+
63+
var app = builder.Build();
64+
65+
app.MapDefaultEndpoints();
66+
67+
// Configure the HTTP request pipeline.
68+
if (app.Environment.IsDevelopment())
69+
{
70+
app.MapOpenApi();
71+
}
72+
73+
app.UseHttpsRedirection();
74+
75+
// serve static files from wwwroot
76+
app.UseDefaultFiles();
77+
// Set up custom content types - associating file extension to MIME type
78+
var provider = new FileExtensionContentTypeProvider();
79+
// Add new mappings
80+
provider.Mappings[".dat"] = "application/octet-stream";
81+
provider.Mappings[".dll"] = "application/octet-stream";
82+
provider.Mappings[".image"] = "image/png";
83+
84+
app.UseStaticFiles(new StaticFileOptions
85+
{
86+
ContentTypeProvider = provider
87+
});
88+
89+
// configure CROS
90+
app.UseCors((p) =>
91+
{
92+
p.AllowAnyOrigin()
93+
.AllowAnyMethod()
94+
.AllowAnyHeader();
95+
});
96+
97+
app.UseAuthentication();
98+
app.UseAuthorization();
99+
100+
app.MapControllers();
101+
102+
app.MapHub<UserInterfaceStatusHub>("/api/internal/status");
103+
app.MapHub<InstanceManagementHub>("/api/internal/managementhub");
104+
105+
app.MapDefaultControllerRoute().WithStaticAssets();
106+
app.UseResponseCompression();
107+
108+
var statusHubContext = app.Services.GetRequiredService<IHubContext<UserInterfaceStatusHub>>();
109+
110+
if (statusHubContext == null)
111+
{
112+
throw new Exception("Status Hub not registered");
113+
}
114+
115+
// setup signalr message forwarding, message received from internal service will be resent to our connected clients via our own SignalR hub
116+
var statusReporting = new UserInterfaceStatusHubReporting(statusHubContext);
117+
//UserInterfaceStatusHubReporting _statusReporting = new UserInterfaceStatusHubReporting();
118+
119+
var directServerClient = app.Services.GetRequiredService<IManagementServerClient>();
120+
certifyManager.SetDirectManagementClient(directServerClient);
121+
122+
app.Start();
123+
124+
System.Diagnostics.Debug.WriteLine($"Server started {string.Join(";", app.Urls)}");
125+
app.WaitForShutdown();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"profiles": {
3+
"http": {
4+
"commandName": "Project",
5+
"environmentVariables": {
6+
"ASPNETCORE_ENVIRONMENT": "Development"
7+
},
8+
"dotnetRunMessages": true,
9+
"applicationUrl": "http://localhost:5028",
10+
"CERTIFY_MANAGEMENT_HUB": "http://localhost:5028/api/internal/managementhub",
11+
"CERTIFY_ENABLE_MANAGEMENT_HUB": "true"
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"environmentVariables": {
16+
"ASPNETCORE_ENVIRONMENT": "Development"
17+
},
18+
"dotnetRunMessages": true,
19+
"applicationUrl": "https://localhost:7187;http://localhost:5028",
20+
"CERTIFY_MANAGEMENT_HUB": "https://localhost:7187/api/internal/managementhub",
21+
"CERTIFY_ENABLE_MANAGEMENT_HUB": "true"
22+
},
23+
"Container (.NET SDK)": {
24+
"commandName": "SdkContainer",
25+
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
26+
"environmentVariables": {
27+
"ASPNETCORE_HTTPS_PORTS": "8081",
28+
"ASPNETCORE_HTTP_PORTS": "8080"
29+
},
30+
"publishAllPorts": true,
31+
"useSSL": true
32+
}
33+
},
34+
"$schema": "https://json.schemastore.org/launchsettings.json"
35+
}

0 commit comments

Comments
 (0)