|
| 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(); |
0 commit comments