Repository navigation
Expand file tree
/
Copy pathStartup.cs
More file actions
137 lines (120 loc) · 5.29 KB
/
Copy pathStartup.cs
File metadata and controls
137 lines (120 loc) · 5.29 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
using System;
using System.Collections.Generic;
using System.Linq;
using Akka.Actor;
using Akka.Configuration;
using Akka.Util.Internal;
using AutoMapper;
using EmailMessageRouter.Data.Cache;
using EmailMessageRouter.Data.EntityModel;
using EmailMessageRouter.Data.Repositories;
using EmailMessageRouter.Domain.Model;
using EmailMessageRouter.Domain.Services;
using EmailMessageRouter.Processor.Actors;
using EmailMessageRouter.Processor.Model;
using EmailMessageRouter.Web.EntityModel;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace EmailMessageRouter.Web
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var mapperConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap<PostmarkEmail, Email>();
cfg.CreateMap<EmailMessage, Email>();
cfg.CreateMap<Email, EmailMessage>();
});
var mapper = new Mapper(mapperConfig);
services.AddSingleton(x => mapper);
// Loads configuration from appsetting.json file.
// CI build tool will override this file for each environment.
var maxBatchSize = Configuration.GetValue<int>("MaxBatchEmails");
var cacheLifetime = Configuration.GetValue<int>("CacheLifetime");
var validationRulesSettings = GetValidationRuleSettings();
var handlersSettings = GetHandlerSettings();
var apiSettings = GetApiSettings();
// All dependencies required by actor system are
// instantiate and inserted here for the purposes of
// this assessment. The parent MessageRoutingManagerActor
// would have its internal DI framework to handle injecting
// its dependencies. Configuration from appsettings.json would
// still passed to actor from here.
var accountRepository = new AccountRepositoryImpl();
var actorSystem = ActorSystem.Create("EmailRoutingActorSystem");
services.AddSingleton(x => actorSystem);
services.AddSingleton(x =>
actorSystem.ActorOf(
MessageRoutingManagerActor.Props(
new MessageRoutingServiceImpl(
accountRepository,
new MessageRequestRepositoryImpl(),
new InMemoryCache<string, Account>(cacheLifetime)
),
new EmailDeliveryServiceImpl(apiSettings),
accountRepository, mapper,
maxBatchSize, validationRulesSettings, handlersSettings
),
typeof(MessageRoutingManagerActor).Name
)
);
services.AddControllers();
}
private Dictionary<string, string> GetApiSettings()
{
var apiSettings = new Dictionary<string, string>();
Configuration
.GetSection("Api")
.GetChildren()
.ForEach(x => apiSettings.Add(x.Key, x.Value));
return apiSettings;
}
private Dictionary<string, bool> GetValidationRuleSettings()
{
var validationRulesSettings = new Dictionary<string, bool>();
Configuration
.GetSection("ValidationRules")
.GetChildren()
.ForEach(x => validationRulesSettings.Add(x.Key, bool.Parse(x.Value)));
if (validationRulesSettings.ContainsKey("DownStreamSingleEmail"))
throw new ConfigurationException("DownStreamSingleEmail configuration required");
if (validationRulesSettings.ContainsKey("DownStreamBulkEmail"))
throw new ConfigurationException("DownStreamBulkEmail configuration required");
return validationRulesSettings;
}
private Dictionary<string, bool> GetHandlerSettings()
{
var handlersSettings = new Dictionary<string, bool>();
Configuration
.GetSection("BusinessEvaluationHandlers")
.GetChildren()
.ForEach(x => handlersSettings.Add(x.Key, bool.Parse( x.Value)));
return handlersSettings;
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
// Code to register instance with Consul Agent
// would go here.
}
}
}