aboutsummaryrefslogtreecommitdiff
path: root/lib/Plugins.Essentials.ServiceStack/src/HttpServiceStackBuilder.cs
blob: 0b75031a29a36ccf6d8f588677ae0e518cf2e77c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* 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.Linq;
using System.Collections.Generic;

using VNLib.Net.Http;

namespace VNLib.Plugins.Essentials.ServiceStack
{
    /// <summary>
    /// A data structure used to build/create a <see cref="HttpServiceStack"/>
    /// around a <see cref="ServiceDomain"/>
    /// </summary>
    public sealed class HttpServiceStackBuilder
    {
        private readonly LinkedList<HttpServer> _servers;

        /// <summary>
        /// The built <see cref="HttpServiceStack"/>
        /// </summary>
        public HttpServiceStack ServiceStack { get; }

        /// <summary>
        /// Gets the underlying <see cref="ServiceDomain"/>
        /// </summary>
        public ServiceDomain ServiceDomain { get; }

        /// <summary>
        /// Initializes a new <see cref="HttpServiceStack"/> that will 
        /// generate servers to listen for services exposed by the 
        /// specified host context
        /// </summary>
        public HttpServiceStackBuilder()
        {
            ServiceDomain = new();
            _servers = new();
            ServiceStack = new(_servers, ServiceDomain);
        }

        /// <summary>
        /// Builds all http servers from 
        /// </summary>
        /// <param name="config">The http server configuration to user for servers</param>
        /// <param name="getTransports">A callback method that gets the transport provider for the given host group</param>
        public void BuildServers(in HttpConfig config, Func<ServiceGroup, ITransportProvider> getTransports)
        {
            //enumerate hosts groups
            foreach (ServiceGroup hosts in ServiceDomain.ServiceGroups)
            {
                //get transport for provider
                ITransportProvider transport = getTransports.Invoke(hosts);

                //Create new server
                HttpServer server = new(config, transport, hosts.Hosts.Select(static h => h.Processor as IWebRoot));

                //Add server to internal list
                _servers.AddLast(server);
            }
        }

        /// <summary>
        /// Releases any resources that may be held by the <see cref="ServiceDomain"/>
        /// incase of an error
        /// </summary>
        public void ReleaseOnError()
        {
            ServiceStack.Dispose();
        }
    }
}