forked from Fr8org/Fr8Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
269 lines (226 loc) · 10.7 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
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
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using Microsoft.Owin;
using Microsoft.Owin.Hosting;
using Owin;
using StructureMap;
using Data.Entities;
using Data.Interfaces;
using Data.Repositories;
using Data.States;
using Fr8.Infrastructure.Interfaces;
using Fr8.Infrastructure.Utilities;
using Fr8.Infrastructure.Utilities.Configuration;
using Hub.Infrastructure;
using Hub.Interfaces;
using Hub.Managers;
using Hub.Security;
using Hangfire;
using Hangfire.StructureMap;
using Hub.StructureMap;
using HubWeb.App_Start;
using GlobalConfiguration = Hangfire.GlobalConfiguration;
using System.Globalization;
using System.Threading;
using Fr8.Infrastructure.Data.Manifests;
using Hub.Enums;
using Hub.Services;
using HubWeb.Infrastructure_PD;
[assembly: OwinStartup(typeof(HubWeb.Startup))]
namespace HubWeb
{
public class Startup : IHttpControllerActivator
{
public void Configuration(IAppBuilder app)
{
//fix to IIS hanging on MemoryCache initialization
//taken from here https://www.zpqrtbnk.net/posts/appdomains-threads-cultureinfos-and-paracetamol
var currentCulture = CultureInfo.CurrentCulture;
var invariantCulture = currentCulture;
while (invariantCulture.Equals(CultureInfo.InvariantCulture) == false)
invariantCulture = invariantCulture.Parent;
if (!(ReferenceEquals(invariantCulture, CultureInfo.InvariantCulture)))
{
var thread = Thread.CurrentThread;
thread.CurrentCulture = CultureInfo.GetCultureInfo(thread.CurrentCulture.Name);
thread.CurrentUICulture = CultureInfo.GetCultureInfo(thread.CurrentUICulture.Name);
}
Configuration(app, false);
}
public async void Configuration(IAppBuilder app, bool selfHostMode)
{
ObjectFactory.Configure(Fr8.Infrastructure.StructureMap.StructureMapBootStrapper.LiveConfiguration);
StructureMapBootStrapper.ConfigureDependencies(StructureMapBootStrapper.DependencyType.LIVE);
//For PlanDirectory merge
ObjectFactory.Configure(PlanDirectoryBootStrapper.LiveConfiguration);
ObjectFactory.GetInstance<AutoMapperBootStrapper>().ConfigureAutoMapper();
var db = ObjectFactory.GetInstance<DbContext>();
db.Database.Initialize(true);
EventReporter curReporter = ObjectFactory.GetInstance<EventReporter>();
curReporter.SubscribeToAlerts();
IncidentReporter incidentReporter = ObjectFactory.GetInstance<IncidentReporter>();
incidentReporter.SubscribeToAlerts();
StartupMigration.UpdateTransitionNames();
SetServerUrl();
OwinInitializer.ConfigureAuth(app, "/Account/Index");
if (!selfHostMode)
{
System.Web.Http.GlobalConfiguration.Configure(ConfigureControllerActivator);
}
ConfigureHangfire(app, "Fr8LocalDB");
#pragma warning disable 4014
RegisterTerminalActions(selfHostMode);
#pragma warning restore 4014
await GenerateManifestPages();
EnsureMThasaDocuSignRecipientCMTypeStored();
}
private void EnsureMThasaDocuSignRecipientCMTypeStored()
{
using (var uow = ObjectFactory.GetInstance<IUnitOfWorkFactory>().Create())
{
var type = uow.MultiTenantObjectRepository.FindTypeReference(typeof(DocuSignRecipientCM));
if (type == null)
{
var user = uow.UserRepository.GetQuery().FirstOrDefault();
if (user != null)
{
uow.MultiTenantObjectRepository.Add(new DocuSignRecipientCM() { RecipientId = Guid.NewGuid().ToString() }, user.Id);
uow.SaveChanges();
}
}
}
}
private async Task GenerateManifestPages()
{
var systemUserAccount = ObjectFactory.GetInstance<Fr8Account>().GetSystemUser();
if(systemUserAccount == null) return;
var systemUser = systemUserAccount.EmailAddress?.Address;
var generator = ObjectFactory.GetInstance<IManifestPageGenerator>();
using (var uow = ObjectFactory.GetInstance<IUnitOfWorkFactory>().Create())
{
var generateTasks = new List<Task>();
foreach (var manifestName in uow.MultiTenantObjectRepository.Query<ManifestDescriptionCM>(systemUser, x => true).Select(x => x.Name).Distinct())
{
generateTasks.Add(generator.Generate(manifestName, GenerateMode.GenerateAlways));
}
await Task.WhenAll(generateTasks);
}
}
public void ConfigureControllerActivator(HttpConfiguration configuration)
{
configuration.Services.Replace(typeof(IHttpControllerActivator), this);
}
private void SetServerUrl()
{
var config = ObjectFactory.GetInstance<IConfigRepository>();
var serverProtocol = config.Get("ServerProtocol", String.Empty);
var domainName = config.Get("ServerDomainName", String.Empty);
var domainPort = config.Get<int?>("ServerPort", null);
if (!String.IsNullOrWhiteSpace(domainName) && !String.IsNullOrWhiteSpace(serverProtocol) && domainPort.HasValue)
{
Server.ServerUrl = $"{serverProtocol}{domainName}{(domainPort.Value == 80 ? String.Empty : (":" + domainPort.Value))}/";
Server.ServerHostName = domainName;
}
}
private async Task RegisterTerminalActions(bool selfHostMode)
{
var terminalDiscovery = ObjectFactory.GetInstance<ITerminalDiscoveryService>();
await terminalDiscovery.DiscoverAll();
if (!selfHostMode)
{
#pragma warning disable 4014
//We don't await this call as this is Hangfire dispatcher job
ObjectFactory.GetInstance<IJobDispatcher>().Enqueue(() => StartMonitoringManifestRegistrySubmissions());
#pragma warning restore 4014
}
}
public static async Task StartMonitoringManifestRegistrySubmissions()
{
await ObjectFactory.GetInstance<IManifestRegistryMonitor>().StartMonitoringManifestRegistrySubmissions();
}
public void ConfigureHangfire(IAppBuilder app, string connectionString)
{
GlobalConfiguration.Configuration
.UseSqlServerStorage(connectionString)
.UseStructureMapActivator(ObjectFactory.Container);
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
AuthorizationFilters = new[] { new HangFireAuthorizationFilter() },
});
app.UseHangfireServer();
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 });
}
//SeedDatabases
//Ensure that critical configuration information is present in the database
//Twilio SMS messages are based on CommunicationConfigurations
//Database should have a CommunicationConfiguration that sends an SMS to 14158067915
//Load Repository and query for CommunicationConfigurations. If null, create one set to 14158067915
//If not null, make sure that at least one exists where the ToAddress is 14158067915
public void ConfigureCommunicationConfigs()
{
IUnitOfWork uow = ObjectFactory.GetInstance<IUnitOfWork>();
CommunicationConfigurationRepository communicationConfigurationRepo = uow.CommunicationConfigurationRepository;
List<CommunicationConfigurationDO> curConfigureCommunicationConfigs = communicationConfigurationRepo.GetAll().ToList();
if (curConfigureCommunicationConfigs.Find(config => config.ToAddress == CloudConfigurationManager.GetSetting("MainSMSAlertNumber")) == null)
// it is not true that there is at least one commConfig that has the Main alert number
{
CommunicationConfigurationDO curCommConfig = new CommunicationConfigurationDO();
curCommConfig.CommunicationType = CommunicationType.Sms;
curCommConfig.ToAddress = CloudConfigurationManager.GetSetting("MainSMSAlertNumber");
communicationConfigurationRepo.Add(curCommConfig);
uow.SaveChanges();
}
}
public void AddMainSMSAlertToDb(CommunicationConfigurationRepository communicationConfigurationRepo)
{
}
// @alexavrutin here: Daemon-related code needs to be reworked, the code below is no more actual.
//private static void ConfigureDaemons()
//{
// DaemonSettings daemonConfig = ConfigurationManager.GetSection("daemonSettings") as DaemonSettings;
// if (daemonConfig != null)
// {
// if (daemonConfig.Enabled)
// {
// foreach (DaemonConfig daemon in daemonConfig.Daemons)
// {
// try
// {
// if (daemon.Enabled)
// {
// Type type = Type.GetType(daemon.InitClass, true);
// Daemon obj = Activator.CreateInstance(type) as Daemon;
// if (obj == null)
// throw new ArgumentException(
// string.Format(
// "A daemon must implement IDaemon. Type '{0}' does not implement the interface.",
// type.Name));
// obj.Start();
// }
// }
// catch (Exception e)
// {
// Logger.GetLogger().Error("Error initializing daemon '" + daemon.Name + "'.", e);
// }
// }
// }
// }
//}
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
return ObjectFactory.GetInstance(controllerType) as IHttpController;
}
public static IDisposable CreateServer(string url)
{
return WebApp.Start<Startup>(url: url);
}
}
}