From 228e1b59a2fc24c2e4ce57aa8171a13a1a18c199 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sat, 8 Apr 2023 16:09:51 -0400 Subject: Minor updates and API changes --- .../src/HttpServiceStack.cs | 6 +- .../src/HttpServiceStackBuilder.cs | 126 ++++++++++++++++----- .../src/PluginLoadConfiguration.cs | 2 +- 3 files changed, 100 insertions(+), 34 deletions(-) (limited to 'lib/Plugins.Essentials.ServiceStack') diff --git a/lib/Plugins.Essentials.ServiceStack/src/HttpServiceStack.cs b/lib/Plugins.Essentials.ServiceStack/src/HttpServiceStack.cs index 45282b3..ae0c522 100644 --- a/lib/Plugins.Essentials.ServiceStack/src/HttpServiceStack.cs +++ b/lib/Plugins.Essentials.ServiceStack/src/HttpServiceStack.cs @@ -39,7 +39,7 @@ namespace VNLib.Plugins.Essentials.ServiceStack /// public sealed class HttpServiceStack : VnDisposeable { - private readonly LinkedList _servers; + private readonly LinkedList _servers; private readonly ServiceDomain _serviceDomain; private CancellationTokenSource? _cts; @@ -48,7 +48,7 @@ namespace VNLib.Plugins.Essentials.ServiceStack /// /// A collection of all loaded servers /// - public IReadOnlyCollection Servers => _servers; + public IReadOnlyCollection Servers => _servers; /// /// The service domain's plugin controller @@ -60,7 +60,7 @@ namespace VNLib.Plugins.Essentials.ServiceStack /// generate servers to listen for services exposed by the /// specified host context /// - internal HttpServiceStack(LinkedList servers, ServiceDomain serviceDomain) + internal HttpServiceStack(LinkedList servers, ServiceDomain serviceDomain) { _servers = servers; _serviceDomain = serviceDomain; diff --git a/lib/Plugins.Essentials.ServiceStack/src/HttpServiceStackBuilder.cs b/lib/Plugins.Essentials.ServiceStack/src/HttpServiceStackBuilder.cs index 0b75031..25b6d5f 100644 --- a/lib/Plugins.Essentials.ServiceStack/src/HttpServiceStackBuilder.cs +++ b/lib/Plugins.Essentials.ServiceStack/src/HttpServiceStackBuilder.cs @@ -23,10 +23,11 @@ */ using System; -using System.Linq; +using System.Threading.Tasks; using System.Collections.Generic; using VNLib.Net.Http; +using VNLib.Utils.Logging; namespace VNLib.Plugins.Essentials.ServiceStack { @@ -36,58 +37,123 @@ namespace VNLib.Plugins.Essentials.ServiceStack /// public sealed class HttpServiceStackBuilder { - private readonly LinkedList _servers; - /// - /// The built + /// Initializes a new that will + /// generate servers to listen for services exposed by the + /// specified host context /// - public HttpServiceStack ServiceStack { get; } + public HttpServiceStackBuilder() + {} + + private Action>? _hostBuilder; + private Func? _getServers; /// - /// Gets the underlying + /// Uses the supplied callback to get a collection of virtual hosts + /// to build the current domain with /// - public ServiceDomain ServiceDomain { get; } + /// The callback method to build virtual hosts + /// A value that indicates if any virtual hosts were successfully loaded + public HttpServiceStackBuilder WithDomainBuilder(Action> hostBuilder) + { + _hostBuilder = hostBuilder; + return this; + } /// - /// Initializes a new that will - /// generate servers to listen for services exposed by the - /// specified host context + /// Spcifies a callback function that builds instances from the hosts /// - public HttpServiceStackBuilder() + /// A callback method that gets the http server implementation for the service group + public HttpServiceStackBuilder WithHttp(Func getServers) { - ServiceDomain = new(); - _servers = new(); - ServiceStack = new(_servers, ServiceDomain); + _getServers = getServers; + return this; } /// - /// Builds all http servers from + /// Builds the new from the configured callbacks, WITHOUT loading plugins /// - /// The http server configuration to user for servers - /// A callback method that gets the transport provider for the given host group - public void BuildServers(in HttpConfig config, Func getTransports) + /// The newly constructed that may be used to manage your http services + /// + public HttpServiceStack Build() { - //enumerate hosts groups - foreach (ServiceGroup hosts in ServiceDomain.ServiceGroups) + _ = _hostBuilder ?? throw new ArgumentNullException("WithDomainBuilder", "You have not configured a service domain configuration callback"); + _ = _getServers ?? throw new ArgumentNullException("WithHttp", "You have not configured a IHttpServer configuration callback"); + + //Inint the service domain + ServiceDomain sd = new(); + try { - //get transport for provider - ITransportProvider transport = getTransports.Invoke(hosts); + if (!sd.BuildDomain(_hostBuilder)) + { + throw new ArgumentException("Failed to configure the service domain, you must expose at least one service host"); + } + + LinkedList servers = new(); - //Create new server - HttpServer server = new(config, transport, hosts.Hosts.Select(static h => h.Processor as IWebRoot)); + //enumerate hosts groups + foreach (ServiceGroup hosts in sd.ServiceGroups) + { + //Create new server + IHttpServer server = _getServers.Invoke(hosts); - //Add server to internal list - _servers.AddLast(server); + //Add server to internal list + servers.AddLast(server); + } + + //Return the service stack + return new HttpServiceStack(servers, sd); + } + catch + { + sd.Dispose(); + throw; } } /// - /// Releases any resources that may be held by the - /// incase of an error + /// Builds the new from configured callbacks, AND loads your plugin environment + /// asynchronously /// - public void ReleaseOnError() + /// The newly constructed that may be used to manage your http services + /// + public async Task BuildAsync(PluginLoadConfiguration config, ILogProvider appLog) { - ServiceStack.Dispose(); + _ = _hostBuilder ?? throw new ArgumentNullException("WithDomainBuilder", "You have not configured a service domain configuration callback"); + _ = _getServers ?? throw new ArgumentNullException("WithHttp", "You have not configured a IHttpServer configuration callback"); + + //Inint the service domain + ServiceDomain sd = new(); + try + { + if (!sd.BuildDomain(_hostBuilder)) + { + throw new ArgumentException("Failed to configure the service domain, you must expose at least one service host"); + } + + //Load plugins async + await sd.PluginManager.LoadPluginsAsync(config, appLog); + + LinkedList servers = new(); + + //enumerate hosts groups + foreach (ServiceGroup hosts in sd.ServiceGroups) + { + //Create new server + IHttpServer server = _getServers.Invoke(hosts); + + //Add server to internal list + servers.AddLast(server); + } + + //Return the service stack + return new HttpServiceStack(servers, sd); + } + catch + { + sd.Dispose(); + throw; + } } } } diff --git a/lib/Plugins.Essentials.ServiceStack/src/PluginLoadConfiguration.cs b/lib/Plugins.Essentials.ServiceStack/src/PluginLoadConfiguration.cs index 4974e71..73cb201 100644 --- a/lib/Plugins.Essentials.ServiceStack/src/PluginLoadConfiguration.cs +++ b/lib/Plugins.Essentials.ServiceStack/src/PluginLoadConfiguration.cs @@ -51,7 +51,7 @@ namespace VNLib.Plugins.Essentials.ServiceStack /// The optional host configuration file to merge with plugin config /// to pass to the loading plugin. /// - public readonly JsonDocument? HostConfig { get; init; } + public readonly JsonElement? HostConfig { get; init; } /// /// Passed to the underlying -- cgit