aboutsummaryrefslogtreecommitdiff
path: root/lib/Plugins.Essentials.ServiceStack/src/HttpServiceStackBuilder.cs
blob: 25b6d5ffe7f648a74fb234001e00a0b670486993 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* 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.Threading.Tasks;
using System.Collections.Generic;

using VNLib.Net.Http;
using VNLib.Utils.Logging;

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
    {
        /// <summary>
        /// Initializes a new <see cref="HttpServiceStack"/> that will 
        /// generate servers to listen for services exposed by the 
        /// specified host context
        /// </summary>
        public HttpServiceStackBuilder()
        {}

        private Action<ICollection<IServiceHost>>? _hostBuilder;
        private Func<ServiceGroup, IHttpServer>? _getServers;

        /// <summary>
        /// Uses the supplied callback to get a collection of virtual hosts
        /// to build the current domain with
        /// </summary>
        /// <param name="hostBuilder">The callback method to build virtual hosts</param>
        /// <returns>A value that indicates if any virtual hosts were successfully loaded</returns>
        public HttpServiceStackBuilder WithDomainBuilder(Action<ICollection<IServiceHost>> hostBuilder)
        {
            _hostBuilder = hostBuilder;
            return this;
        }

        /// <summary>
        /// Spcifies a callback function that builds <see cref="IHttpServer"/> instances from the hosts
        /// </summary>
        /// <param name="getServers">A callback method that gets the http server implementation for the service group</param>
        public HttpServiceStackBuilder WithHttp(Func<ServiceGroup, IHttpServer> getServers)
        {
            _getServers = getServers;
            return this;
        }

        /// <summary>
        /// Builds the new <see cref="HttpServiceStack"/> from the configured callbacks, WITHOUT loading plugins
        /// </summary>
        /// <returns>The newly constructed <see cref="HttpServiceStack"/> that may be used to manage your http services</returns>
        /// <exception cref="ArgumentNullException"></exception>
        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<IHttpServer> 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;
            }
        }

        /// <summary>
        /// Builds the new <see cref="HttpServiceStack"/> from configured callbacks, AND loads your plugin environment 
        /// asynchronously
        /// </summary>
        /// <returns>The newly constructed <see cref="HttpServiceStack"/> that may be used to manage your http services</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public async Task<HttpServiceStack> BuildAsync(PluginLoadConfiguration config, ILogProvider appLog)
        {
            _ = _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<IHttpServer> 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;
            }
        }
    }
}