-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathStartup.cs
126 lines (105 loc) · 5.08 KB
/
Startup.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
namespace Cdr.Banking.Api;
/// <summary>
/// Represents the <b>startup</b> class.
/// </summary>
public class Startup
{
/// <summary>
/// The configure services method called by the runtime; use this method to add services to the container.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
public void ConfigureServices(IServiceCollection services)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
// Add "page" and "page-size" to the supported paging query string parameters as defined by the CDR specification.
HttpConsts.PagingArgsPageQueryStringNames.Add("page");
HttpConsts.PagingArgsTakeQueryStringNames.Add("page-size");
// Add the core services (including the customized ExecutionContext).
services.AddSettings<BankingSettings>()
.AddExecutionContext(_ => new Business.ExecutionContext())
.AddJsonSerializer()
.AddReferenceDataOrchestrator()
.AddWebApi()
.AddReferenceDataContentWebApi()
.AddRequestCache()
.AddValidationTextProvider()
.AddValidators<AccountManager>();
// Add the cosmos database.
services.AddSingleton(sp =>
{
var settings = sp.GetRequiredService<BankingSettings>();
var cco = new AzCosmos.CosmosClientOptions { SerializerOptions = new AzCosmos.CosmosSerializationOptions { PropertyNamingPolicy = AzCosmos.CosmosPropertyNamingPolicy.CamelCase, IgnoreNullValues = true } };
return new AzCosmos.CosmosClient(settings.CosmosConnectionString, cco);
});
services.AddCosmosDb<ICosmos>(sp =>
{
var settings = sp.GetRequiredService<BankingSettings>();
return new CosmosDb(sp.GetRequiredService<AzCosmos.CosmosClient>().GetDatabase(settings.CosmosDatabaseId), sp.GetRequiredService<CoreEx.Mapping.IMapper>());
});
// Add the generated reference data services.
services.AddGeneratedReferenceDataManagerServices()
.AddGeneratedReferenceDataDataSvcServices()
.AddGeneratedReferenceDataDataServices();
// Add the generated services.
services.AddGeneratedManagerServices()
.AddGeneratedDataSvcServices()
.AddGeneratedDataServices();
// Add AutoMapper services via Assembly-based probing for Profiles.
services.AddMappers<BankingSettings>();
// Add additional services.
services.AddControllers();
services.AddHealthChecks();
services.AddHttpClient();
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Cdr.Banking API", Version = "v1" });
options.OperationFilter<AcceptsBodyOperationFilter>(); // Needed to support AcceptsBodyAttribute where body parameter not explicitly defined.
options.OperationFilter<PagingOperationFilter>(); // Needed to support PagingAttribute where PagingArgs parameter not explicitly defined.
options.OperationFilter<QueryOperationFilter>(); // Needed to support QueryAttribute where QueryArgs parameter not explicitly defined.
});
}
/// <summary>
/// The configure method called by the runtime; use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
public void Configure(IApplicationBuilder app)
{
// Handle any unhandled exceptions.
app.UseWebApiExceptionHandler();
// Add Swagger as a JSON endpoint and to serve the swagger-ui to the pipeline.
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Cdr.Banking"));
// Add execution context set up to the pipeline.
app.UseExecutionContext((hc, ec) =>
{
// TODO: This would be replaced with appropriate OAuth integration, etc... - this is purely for illustrative purposes only.
if (!hc.Request.Headers.TryGetValue("cdr-user", out var username) || username.Count != 1)
throw new AuthenticationException();
var bec = (Business.ExecutionContext)ec;
bec.Timestamp = SystemTime.Get().UtcNow;
switch (username[0])
{
case "jessica":
bec.Accounts.AddRange(new string[] { "12345678", "34567890", "45678901" });
break;
case "jenny":
bec.Accounts.Add("23456789");
break;
case "jason":
break;
default:
throw new AuthenticationException();
}
return Task.CompletedTask;
});
// Add health checks page to the pipeline.
app.UseHealthChecks("/health");
// Use controllers.
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}