-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
107 lines (89 loc) · 3.56 KB
/
Copy pathProgram.cs
File metadata and controls
107 lines (89 loc) · 3.56 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
using CSC.PublicApi.DatabaseContext;
using CSC.PublicApi.ElasticService;
using CSC.PublicApi.Indexer.Configuration;
using CSC.PublicApi.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
namespace CSC.PublicApi.Indexer;
public class Program
{
private const int DefaultQueryTimeout = 300;
public static async Task<int> Main(string[] args)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environment}.json", true)
.AddUserSecrets<Program>()
.AddEnvironmentVariables()
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
try
{
var consoleHost = CreateHostBuilder(args).Build();
var databasePreflightCheck = consoleHost.Services.GetRequiredService<DatabasePreflightCheck>();
if (!databasePreflightCheck.IsGood())
{
return 1;
}
var indexer = consoleHost.Services.GetRequiredService<Indexer>();
await indexer.Start();
return 0;
}
catch (Exception ex)
{
Log.Error(ex, "Indexer failed");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
private static IHostBuilder CreateHostBuilder(string[] args) => Host
.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureServices((hostContext, services) =>
{
// Register the "Main" service.
services.AddTransient<Indexer>();
// Register the database checker service.
services.AddTransient<DatabasePreflightCheck>();
// Register settings.
services.AddSettings(hostContext.Configuration);
// Add ElasticSearch.
services.AddElasticSearch(hostContext.Configuration);
// Add ElasticSearchIndexingService.
services.AddScoped<IElasticSearchIndexService, ElasticSearchIndexService>();
services.AddMemoryCache();
if (!int.TryParse(hostContext.Configuration["QueryTimeout"], out var queryTimeout))
{
queryTimeout = DefaultQueryTimeout;
}
// Configure db & entity framework.
services.AddDbContext<ApiDbContext>(options =>
{
options.UseSqlServer("name=dbconnectionstring", opt =>
{
opt.CommandTimeout(queryTimeout);
opt.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
});
options.ConfigureWarnings(x => x.Throw(RelationalEventId.MultipleCollectionIncludeWarning));
});
services.AddRepositories();
services.AddAutoMapper(typeof(IUnitOfWork).Assembly);
})
.ConfigureHostConfiguration(configurationBuilder => configurationBuilder
// Most of the configuration comes from environment variables.
.AddEnvironmentVariables()
// For local dev we get configuration from user secrets.
.AddUserSecrets(typeof(Program).Assembly, true)
.Build());
}