-
Notifications
You must be signed in to change notification settings - Fork 33
/
Program.cs
105 lines (93 loc) 路 3.88 KB
/
Program.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
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Tzkt.Api.Services.Auth;
using Tzkt.Data;
namespace Tzkt.Api
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Check().Init().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureAppConfiguration((host, appConfig) =>
{
appConfig.Sources.Clear();
appConfig.AddJsonFile("appsettings.json", true);
appConfig.AddJsonFile($"appsettings.{host.HostingEnvironment.EnvironmentName}.json", true);
appConfig.AddEnvironmentVariables();
appConfig.AddEnvironmentVariables("TZKT_API_");
appConfig.AddCommandLine(args);
})
.ConfigureLogging(logConfig =>
{
logConfig.ClearProviders();
logConfig.AddConsole();
});
}
static class IHostExt
{
public static IHost Init(this IHost host, int attempt = 0)
{
using var scope = host.Services.CreateScope();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
var db = scope.ServiceProvider.GetRequiredService<TzktContext>();
logger.LogInformation("Version {version}",
Assembly.GetExecutingAssembly().GetName().Version.ToString());
try
{
logger.LogInformation("Initialize database");
var migrations = db.Database.GetMigrations().ToList();
var appliedMigrations = db.Database.GetAppliedMigrations().ToList();
for (int i = 0; i < Math.Min(migrations.Count, appliedMigrations.Count); i++)
{
if (migrations[i] != appliedMigrations[i])
{
attempt = 30;
throw new Exception($"API and DB schema have incompatible versions. Drop the DB and restore it from the appropriate snapshot.");
}
}
if (appliedMigrations.Count > migrations.Count)
{
attempt = 30;
throw new Exception($"API version seems older than version of the DB schema. Update the API to the newer version.");
}
if (appliedMigrations.Count < migrations.Count)
throw new Exception($"{migrations.Count - appliedMigrations.Count} database migrations are pending.");
var state = db.AppState.Single();
if (state.Level < 1)
throw new Exception("database is empty, at least two blocks are needed.");
logger.LogInformation("Database initialized");
return host;
}
catch (Exception ex)
{
logger.LogCritical($"Failed to initialize database: {ex.Message}");
if (attempt >= 30) throw;
Thread.Sleep(1000);
return host.Init(++attempt);
}
}
public static IHost Check(this IHost host)
{
using var scope = host.Services.CreateScope();
var config = scope.ServiceProvider.GetRequiredService<IConfiguration>();
config.ValidateAuthConfig();
return host;
}
}
}