forked from moattarwork/Easify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimalApiBootstrapper.cs
More file actions
225 lines (190 loc) · 8.41 KB
/
MinimalApiBootstrapper.cs
File metadata and controls
225 lines (190 loc) · 8.41 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// This software is part of the LittleBlocks framework
// Copyright (C) 2024 LittleBlocks
//
// This program 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 3 of the License, or
// (at your option) any later version.
//
// This program 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 <http://www.gnu.org/licenses/>.
namespace LittleBlocks.AspNetCore.Bootstrap;
/// <summary>
/// Bootstrapper for minimal APIs that provides the same configuration capabilities as the traditional AppBootstrapper
/// but without requiring a Startup class.
/// </summary>
public sealed class MinimalApiBootstrapper :
IBootstrapApplication,
IConfigureContainer,
IAddExtraConfigSection,
IHandleAdditionalException,
ISetDetailsLevel,
IExtendPipeline,
IConfigureRequestCorrelation,
IConfigureAuthentication,
IConfigureHealthChecks,
IConfigureApplicationBootstrapper
{
private readonly IConfiguration _configuration;
private readonly ConfigurationOptionBuilder _configurationOptionBuilder;
private readonly GlobalErrorHandlerConfigurationBuilder _errorHandlerBuilder;
private readonly List<Action<IServiceCollection, IConfiguration>> _pipelineExtenders =
new List<Action<IServiceCollection, IConfiguration>>();
private readonly IServiceCollection _services;
private readonly IHealthChecksBuilder _healthChecksBuilder;
private readonly AppInfo _appInfo;
private readonly AuthOptions _authOptions;
private Action<IServiceCollection, IConfiguration> _containerFactory;
private Func<IExcludeRequests, IBuildOptions> _requestCorrelationExtender = cop => cop.EnforceCorrelation();
public MinimalApiBootstrapper(
IServiceCollection services,
IConfiguration configuration)
{
_services = services ?? throw new ArgumentNullException(nameof(services));
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
_configurationOptionBuilder = new ConfigurationOptionBuilder(services, _configuration);
_errorHandlerBuilder = new GlobalErrorHandlerConfigurationBuilder(services);
_errorHandlerBuilder.UseStandardMessage();
_appInfo = _configuration.GetApplicationInfo();
_authOptions = _configuration.GetAuthOptions();
_healthChecksBuilder = _services.AddHealthChecks();
}
public IAddExtraConfigSection AndSection<TSection>()
where TSection : class, new()
{
_configurationOptionBuilder.And<TSection>();
return this;
}
public IAddExtraConfigSection AndSection<TSection>(string section)
where TSection : class, new()
{
_configurationOptionBuilder.And<TSection>(section);
return this;
}
public IHandleAdditionalException HandleApplicationException<TApplicationBaseException>()
where TApplicationBaseException : Exception
{
_errorHandlerBuilder.Handle<TApplicationBaseException>();
return this;
}
public void Bootstrap()
{
_configurationOptionBuilder.Build();
_services.TryAddSingleton<IDateTimeProvider, DateTimeProvider>();
// Don't add IUrlHelper for minimal APIs as it requires MVC ActionContext
_services.TryAddScoped<IArgumentsFormatter, ArgumentsFormatter>();
_services.TryAddSingleton(_ => new ArgumentFormatterOptions());
_services.AddDatabaseDeveloperPageExceptionFilter();
_services.AddHttpRequestContext();
_services.AddGlobalExceptionHandler(_ => _errorHandlerBuilder.UseDefault());
_services.AddRequestCorrelation(b => _requestCorrelationExtender(b.ExcludeDefaultUrls()));
_services.AddFeatureFlagging(_configuration);
// For minimal APIs, we don't add the default MVC services automatically
// Users can still add controllers if needed via AddControllers() on the builder
_services.AddDefaultCorsPolicy();
_services.AddAuthentication(_authOptions);
_services.AddAuthorization(); // Required for minimal APIs
// Add minimal API specific services for OpenAPI/Swagger
_services.AddEndpointsApiExplorer();
_services.AddSwaggerGen();
_pipelineExtenders.ForEach(e => e(_services, _configuration));
_containerFactory(_services, _configuration);
}
public IAddExtraConfigSection AddConfigSection<TSection>()
where TSection : class, new()
{
_configurationOptionBuilder.AddSection<TSection>();
return this;
}
public IAddExtraConfigSection AddConfigSection<TSection>(string section)
where TSection : class, new()
{
_configurationOptionBuilder.AddSection<TSection>(section);
return this;
}
public IBootstrapApplication UseContainer<TContainer>(ContainerFactory<TContainer> containerFactory)
where TContainer : class
{
if (containerFactory == null) throw new ArgumentNullException(nameof(containerFactory));
_containerFactory = containerFactory.Create;
return this;
}
public IConfigureRequestCorrelation UseStandardMessage()
{
_errorHandlerBuilder.UseStandardMessage();
return this;
}
public IConfigureRequestCorrelation UseUserErrors()
{
_errorHandlerBuilder.UseUserErrors();
return this;
}
public IConfigureRequestCorrelation UseDetailedErrors()
{
_errorHandlerBuilder.UseDetailedErrors();
return this;
}
public IConfigureAuthentication ConfigureCorrelation(
Func<IExcludeRequests, IBuildOptions> optionsProvider)
{
_requestCorrelationExtender = optionsProvider ??
throw new ArgumentNullException(nameof(optionsProvider));
return this;
}
public IConfigureAuthentication ConfigureCorrelation(Func<IExcludeRequests, ICorrelateRequests> optionsProvider)
{
if (optionsProvider == null) throw new ArgumentNullException(nameof(optionsProvider));
_requestCorrelationExtender = r => optionsProvider(r).EnforceCorrelation();
return this;
}
public IExtendPipeline Extend(Action<IServiceCollection, IConfiguration> pipelineExtender)
{
if (pipelineExtender == null) throw new ArgumentNullException(nameof(pipelineExtender));
_pipelineExtenders.Add(pipelineExtender);
return this;
}
public IConfigureHealthChecks ConfigureAuthentication(Action<ISetAuthenticationMode> configure)
{
if (configure == null) throw new ArgumentNullException(nameof(configure));
configure(_authOptions);
return this;
}
public IExtendPipeline ConfigureHealthChecks(Action<IHealthChecksBuilder> configure)
{
if (configure == null) throw new ArgumentNullException(nameof(configure));
configure(_healthChecksBuilder);
return this;
}
public IHandleAdditionalException AndHandle<TThirdPartyBaseException>()
where TThirdPartyBaseException : Exception
{
_errorHandlerBuilder.AndHandle<TThirdPartyBaseException>();
return this;
}
public IHandleAdditionalException AndHandle<TThirdPartyBaseException>(
Func<TThirdPartyBaseException, bool> predicate) where TThirdPartyBaseException : Exception
{
_errorHandlerBuilder.AndHandle(predicate);
return this;
}
public IHandleAdditionalException AndHandle<TThirdPartyBaseException>(
Func<ISetErrorBuilder<TThirdPartyBaseException>, IProvideErrorBuilder<TThirdPartyBaseException>>
errorBuilderProvider) where TThirdPartyBaseException : Exception
{
_errorHandlerBuilder.AndHandle(errorBuilderProvider);
return this;
}
public IHandleAdditionalException AndHandle<TThirdPartyBaseException>(
Func<ISetErrorBuilder<TThirdPartyBaseException>, IProvideErrorBuilder<TThirdPartyBaseException>>
errorBuilderProvider, Func<TThirdPartyBaseException, bool> predicate)
where TThirdPartyBaseException : Exception
{
_errorHandlerBuilder.AndHandle(errorBuilderProvider, predicate);
return this;
}
}