This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
/
NancyInternalConfiguration.cs
421 lines (368 loc) · 18 KB
/
NancyInternalConfiguration.cs
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
namespace Nancy.Bootstrapper
{
using System;
using System.Collections.Generic;
using System.Linq;
using Nancy.Configuration;
using Nancy.Culture;
using Nancy.Diagnostics;
using Nancy.ErrorHandling;
using Nancy.Localization;
using Nancy.ModelBinding;
using Nancy.Responses;
using Nancy.Responses.Negotiation;
using Nancy.Routing;
using Nancy.Routing.Constraints;
using Nancy.Routing.Trie;
using Nancy.Security;
using Nancy.Validation;
using Nancy.ViewEngines;
/// <summary>
/// Configuration class for Nancy's internals.
/// Contains implementation types/configuration for Nancy that usually
/// do not require overriding in "general use".
/// </summary>
public sealed class NancyInternalConfiguration
{
/// <summary>
/// Gets the Nancy default configuration
/// </summary>
public static Func<ITypeCatalog, NancyInternalConfiguration> Default
{
get
{
return typeCatalog => new NancyInternalConfiguration
{
Binder = typeof(DefaultBinder),
BindingDefaults = typeof(BindingDefaults),
ContextFactory = typeof(DefaultNancyContextFactory),
CsrfTokenValidator = typeof(DefaultCsrfTokenValidator),
CultureService = typeof(DefaultCultureService),
DefaultConfigurationProviders = typeCatalog.GetTypesAssignableTo<INancyDefaultConfigurationProvider>().ToList(),
Diagnostics = typeof(DefaultDiagnostics),
EnvironmentFactory = typeof(DefaultNancyEnvironmentFactory),
EnvironmentConfigurator = typeof(DefaultNancyEnvironmentConfigurator),
FieldNameConverter = typeof(DefaultFieldNameConverter),
InteractiveDiagnosticProviders = new List<Type>(typeCatalog.GetTypesAssignableTo<IDiagnosticsProvider>()),
ModelBinderLocator = typeof(DefaultModelBinderLocator),
ModelValidatorLocator = typeof(DefaultValidatorLocator),
NancyEngine = typeof(NancyEngine),
NancyModuleBuilder = typeof(DefaultNancyModuleBuilder),
ObjectSerializer = typeof(DefaultObjectSerializer),
RenderContextFactory = typeof(DefaultRenderContextFactory),
RequestDispatcher = typeof(DefaultRequestDispatcher),
RequestTraceFactory = typeof(DefaultRequestTraceFactory),
RequestTracing = typeof(DefaultRequestTracing),
ResourceAssemblyProvider = typeof(ResourceAssemblyProvider),
ResourceReader = typeof(DefaultResourceReader),
ResponseFormatterFactory = typeof(DefaultResponseFormatterFactory),
ResponseNegotiator = typeof(DefaultResponseNegotiator),
ResponseProcessors = typeCatalog.GetTypesAssignableTo<IResponseProcessor>().ToList(),
RouteCache = typeof(RouteCache),
RouteCacheProvider = typeof(DefaultRouteCacheProvider),
RouteInvoker = typeof(DefaultRouteInvoker),
RouteResolver = typeof(DefaultRouteResolver),
RouteResolverTrie = typeof(RouteResolverTrie),
RouteSegmentConstraints = typeCatalog.GetTypesAssignableTo<IRouteSegmentConstraint>().ToList(),
RouteSegmentExtractor = typeof(DefaultRouteSegmentExtractor),
RouteMetadataProviders = typeCatalog.GetTypesAssignableTo<IRouteMetadataProvider>().ToList(),
RouteDescriptionProvider = typeof(DefaultRouteDescriptionProvider),
RuntimeEnvironmentInformation = typeof(DefaultRuntimeEnvironmentInformation),
SerializerFactory = typeof(DefaultSerializerFactory),
Serializers = typeCatalog.GetTypesAssignableTo<ISerializer>(TypeResolveStrategies.ExcludeNancy).Union(new List<Type>(new[] { typeof(DefaultJsonSerializer), typeof(DefaultXmlSerializer) })).ToList(),
StaticContentProvider = typeof(DefaultStaticContentProvider),
StatusCodeHandlers = new List<Type>(typeCatalog.GetTypesAssignableTo<IStatusCodeHandler>(TypeResolveStrategies.ExcludeNancy).Concat(new[] { typeof(DefaultStatusCodeHandler) })),
TextResource = typeof(ResourceBasedTextResource),
TrieNodeFactory = typeof(TrieNodeFactory),
ViewLocator = typeof(DefaultViewLocator),
ViewFactory = typeof(DefaultViewFactory),
ViewResolver = typeof(DefaultViewResolver),
ViewCache = typeof(DefaultViewCache),
ViewLocationProvider = typeof(FileSystemViewLocationProvider),
};
}
}
/// <summary>
/// Gets or sets the runtime environment information
/// </summary>
public Type RuntimeEnvironmentInformation { get; set; }
/// <summary>
/// Gets or sets the serializer factory.
/// </summary>
public Type SerializerFactory { get; set; }
/// <summary>
/// Gets or sets the default configuration providers
/// </summary>
public IList<Type> DefaultConfigurationProviders { get; set; }
/// <summary>
/// Gets or sets the environment configurator
/// </summary>
public Type EnvironmentConfigurator { get; set; }
/// <summary>
///Gets or sets the environment factory
/// </summary>
public Type EnvironmentFactory { get; set; }
/// <summary>
/// Gets or sets the route metadata providers
/// </summary>
public IList<Type> RouteMetadataProviders { get; set; }
/// <summary>
/// Gets or sets the route resolver
/// </summary>
public Type RouteResolver { get; set; }
/// <summary>
/// Gets or sets the context factory
/// </summary>
public Type ContextFactory { get; set; }
/// <summary>
/// Gets or sets the nancy engine
/// </summary>
public Type NancyEngine { get; set; }
/// <summary>
/// Gets or sets the route cache
/// </summary>
public Type RouteCache { get; set; }
/// <summary>
/// Gets or sets the route cache provider
/// </summary>
public Type RouteCacheProvider { get; set; }
/// <summary>
/// Gets or sets the view locator
/// </summary>
public Type ViewLocator { get; set; }
/// <summary>
/// Gets or sets the view factory
/// </summary>
public Type ViewFactory { get; set; }
/// <summary>
/// Gets or sets the nancy module builder
/// </summary>
public Type NancyModuleBuilder { get; set; }
/// <summary>
/// Gets or sets the response formatter factory
/// </summary>
public Type ResponseFormatterFactory { get; set; }
/// <summary>
/// Gets or sets themodel binder locator
/// </summary>
public Type ModelBinderLocator { get; set; }
/// <summary>
/// Gets or sets the binder
/// </summary>
public Type Binder { get; set; }
/// <summary>
/// Gets or sets the binding defaults
/// </summary>
public Type BindingDefaults { get; set; }
/// <summary>
/// Gets or sets the field name converter
/// </summary>
public Type FieldNameConverter { get; set; }
/// <summary>
/// Gets or sets the model validator locator
/// </summary>
public Type ModelValidatorLocator { get; set; }
/// <summary>
///Gets or sets the view resolver
/// </summary>
public Type ViewResolver { get; set; }
/// <summary>
/// Gets or sets the view cache
/// </summary>
public Type ViewCache { get; set; }
/// <summary>
/// Gets or sets the render context factory
/// </summary>
public Type RenderContextFactory { get; set; }
/// <summary>
/// Gets or sets the view location provider
/// </summary>
public Type ViewLocationProvider { get; set; }
/// <summary>
/// Gets or sets the status code handlers
/// </summary>
public IList<Type> StatusCodeHandlers { get; set; }
/// <summary>
/// Gets or sets the CSRF token validator
/// </summary>
public Type CsrfTokenValidator { get; set; }
/// <summary>
/// Gets or sets the object serializer
/// </summary>
public Type ObjectSerializer { get; set; }
/// <summary>
/// Gets or sets the types for serializers
/// </summary>
public IList<Type> Serializers { get; set; }
/// <summary>
/// Gets or sets the interactive diagnostic providers
/// </summary>
public IList<Type> InteractiveDiagnosticProviders { get; set; }
/// <summary>
/// Gets or sets the request tracing
/// </summary>
public Type RequestTracing { get; set; }
/// <summary>
/// Gets or sets the route invoker
/// </summary>
public Type RouteInvoker { get; set; }
/// <summary>
/// Gets or sets the response processors
/// </summary>
public IList<Type> ResponseProcessors { get; set; }
/// <summary>
/// Gets or sets the request dispatcher
/// </summary>
public Type RequestDispatcher { get; set; }
/// <summary>
/// Gets or sets the diagnostics
/// </summary>
public Type Diagnostics { get; set; }
/// <summary>
/// Gets or sets the route segment extractor
/// </summary>
public Type RouteSegmentExtractor { get; set; }
/// <summary>
/// Gets or sets the route description provider
/// </summary>
public Type RouteDescriptionProvider { get; set; }
/// <summary>
/// Gets or sets the culture service
/// </summary>
public Type CultureService { get; set; }
/// <summary>
/// Gets or sets the text resource
/// </summary>
public Type TextResource { get; set; }
/// <summary>
/// Gets or sets the resource assembly provider
/// </summary>
public Type ResourceAssemblyProvider { get; set; }
/// <summary>
/// Gets or sets the resource reader
/// </summary>
public Type ResourceReader { get; set; }
/// <summary>
/// Gets or sets the static content provider
/// </summary>
public Type StaticContentProvider { get; set; }
/// <summary>
/// Gets or sets the route resolver trie
/// </summary>
public Type RouteResolverTrie { get; set; }
/// <summary>
/// Gets or sets the trie node factory
/// </summary>
public Type TrieNodeFactory { get; set; }
/// <summary>
/// Gets or sets the route segment constraints
/// </summary>
public IList<Type> RouteSegmentConstraints { get; set; }
/// <summary>
/// Gets or sets the request trace factory
/// </summary>
public Type RequestTraceFactory { get; set; }
/// <summary>
/// Gets or sets the response negotiator
/// </summary>
public Type ResponseNegotiator { get; set; }
/// <summary>
/// Gets a value indicating whether the configuration is valid.
/// </summary>
public bool IsValid
{
get
{
try
{
return this.GetTypeRegistrations().All(tr => tr.RegistrationType != null);
}
catch (ArgumentNullException)
{
return false;
}
}
}
/// <summary>
/// Creates a new nancy internal configuration initializer with overrides for default types.
/// </summary>
/// <param name="builder">Action that overrides default configuration types</param>
/// <returns>Initializer with overriden default types</returns>
public static Func<ITypeCatalog, NancyInternalConfiguration> WithOverrides(Action<NancyInternalConfiguration> builder)
{
return catalog =>
{
var configuration =
Default.Invoke(catalog);
builder.Invoke(configuration);
return configuration;
};
}
/// <summary>
/// Returns the configuration types as a TypeRegistration collection
/// </summary>
/// <returns>TypeRegistration collection representing the configuration types</returns>
public IEnumerable<TypeRegistration> GetTypeRegistrations()
{
return new[]
{
new TypeRegistration(typeof(IRouteResolver), this.RouteResolver),
new TypeRegistration(typeof(INancyEngine), this.NancyEngine),
new TypeRegistration(typeof(IRouteCache), this.RouteCache),
new TypeRegistration(typeof(IRouteCacheProvider), this.RouteCacheProvider),
new TypeRegistration(typeof(IViewLocator), this.ViewLocator),
new TypeRegistration(typeof(IViewFactory), this.ViewFactory),
new TypeRegistration(typeof(INancyContextFactory), this.ContextFactory),
new TypeRegistration(typeof(INancyModuleBuilder), this.NancyModuleBuilder),
new TypeRegistration(typeof(IResponseFormatterFactory), this.ResponseFormatterFactory),
new TypeRegistration(typeof(IModelBinderLocator), this.ModelBinderLocator),
new TypeRegistration(typeof(IBinder), this.Binder),
new TypeRegistration(typeof(BindingDefaults), this.BindingDefaults),
new TypeRegistration(typeof(IFieldNameConverter), this.FieldNameConverter),
new TypeRegistration(typeof(IViewResolver), this.ViewResolver),
new TypeRegistration(typeof(IViewCache), this.ViewCache),
new TypeRegistration(typeof(IRenderContextFactory), this.RenderContextFactory),
new TypeRegistration(typeof(IViewLocationProvider), this.ViewLocationProvider),
new TypeRegistration(typeof(ICsrfTokenValidator), this.CsrfTokenValidator),
new TypeRegistration(typeof(IObjectSerializer), this.ObjectSerializer),
new TypeRegistration(typeof(IModelValidatorLocator), this.ModelValidatorLocator),
new TypeRegistration(typeof(IRequestTracing), this.RequestTracing),
new TypeRegistration(typeof(IRouteInvoker), this.RouteInvoker),
new TypeRegistration(typeof(IRequestDispatcher), this.RequestDispatcher),
new TypeRegistration(typeof(IDiagnostics), this.Diagnostics),
new TypeRegistration(typeof(IRouteSegmentExtractor), this.RouteSegmentExtractor),
new TypeRegistration(typeof(IRouteDescriptionProvider), this.RouteDescriptionProvider),
new TypeRegistration(typeof(ICultureService), this.CultureService),
new TypeRegistration(typeof(ITextResource), this.TextResource),
new TypeRegistration(typeof(IResourceAssemblyProvider), this.ResourceAssemblyProvider),
new TypeRegistration(typeof(IResourceReader), this.ResourceReader),
new TypeRegistration(typeof(IStaticContentProvider), this.StaticContentProvider),
new TypeRegistration(typeof(IRouteResolverTrie), this.RouteResolverTrie),
new TypeRegistration(typeof(ITrieNodeFactory), this.TrieNodeFactory),
new TypeRegistration(typeof(IRequestTraceFactory), this.RequestTraceFactory),
new TypeRegistration(typeof(IResponseNegotiator), this.ResponseNegotiator),
new TypeRegistration(typeof(INancyEnvironmentConfigurator), this.EnvironmentConfigurator),
new TypeRegistration(typeof(INancyEnvironmentFactory), this.EnvironmentFactory),
new TypeRegistration(typeof(ISerializerFactory), this.SerializerFactory),
new TypeRegistration(typeof(IRuntimeEnvironmentInformation), this.RuntimeEnvironmentInformation)
};
}
/// <summary>
/// Returns the collection configuration types as a CollectionTypeRegistration collection
/// </summary>
/// <returns>CollectionTypeRegistration collection representing the configuration types</returns>
public IEnumerable<CollectionTypeRegistration> GetCollectionTypeRegistrations()
{
return new[]
{
new CollectionTypeRegistration(typeof(IResponseProcessor), this.ResponseProcessors),
new CollectionTypeRegistration(typeof(ISerializer), this.Serializers),
new CollectionTypeRegistration(typeof(IStatusCodeHandler), this.StatusCodeHandlers),
new CollectionTypeRegistration(typeof(IDiagnosticsProvider), this.InteractiveDiagnosticProviders),
new CollectionTypeRegistration(typeof(IRouteSegmentConstraint), this.RouteSegmentConstraints),
new CollectionTypeRegistration(typeof(IRouteMetadataProvider), this.RouteMetadataProviders),
new CollectionTypeRegistration(typeof(INancyDefaultConfigurationProvider), this.DefaultConfigurationProviders),
};
}
}
}