/*
* Copyright (c) 2023 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Essentials.ServiceStack
* File: HttpServiceStackBuilder.cs
*
* HttpServiceStackBuilder.cs is part of VNLib.Plugins.Essentials.ServiceStack which is part of the larger
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Essentials.ServiceStack is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Essentials.ServiceStack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
using System;
using System.Collections.Generic;
using VNLib.Net.Http;
namespace VNLib.Plugins.Essentials.ServiceStack
{
///
/// A data structure used to build/create a
/// around a
///
public sealed class HttpServiceStackBuilder
{
///
/// Initializes a new that will
/// generate servers to listen for services exposed by the
/// specified host context
///
public HttpServiceStackBuilder()
{}
private Action>? _hostBuilder;
private Func? _getServers;
///
/// Uses the supplied callback to get a collection of virtual hosts
/// to build the current domain with
///
/// 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;
}
///
/// Spcifies a callback function that builds instances from the hosts
///
/// A callback method that gets the http server implementation for the service group
public HttpServiceStackBuilder WithHttp(Func getServers)
{
_getServers = getServers;
return this;
}
///
/// Builds the new from the configured callbacks, WITHOUT loading plugins
///
/// The newly constructed that may be used to manage your http services
///
public HttpServiceStack Build()
{
_ = _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");
}
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;
}
}
}
}