/
Program.cs
82 lines (63 loc) · 2.17 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
using provider.Domain;
using provider.Services;
using Microsoft.EntityFrameworkCore;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("ProviderTests")]
var builder = WebApplication.CreateBuilder(args);
var settings = new AppSettings();
builder.Configuration.GetSection("AppSettings").Bind(settings);
var cstr = new ConnectionStrings();
builder.Configuration.GetSection("ConnectionStrings").Bind(cstr);
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
builder.Services.Configure<ConnectionStrings>(builder.Configuration.GetSection("ConnectionStrings"));
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
var filePath = Path.Combine(AppContext.BaseDirectory, "provider.xml");
c.IncludeXmlComments(filePath, includeControllerXmlComments: true);
});
builder.Services.AddScoped<LinkService>();
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.SetIsOriginAllowed(s => true)
.AllowCredentials()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
builder.Services.AddDbContext<LinkDatabase>(opts => opts.UseSqlite(cstr.LinkDbConnectionString));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment() || settings.ShowSwaggerDoc)
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
if (settings.Network == "test" && KzBsv.Kz.Chain == KzBsv.KzChain.unkown)
{
// use testnet
KzBsv.Kz.CreateChainParams(KzBsv.KzChain.test);
}
using (var serviceScope = app.Services.GetService<IServiceScopeFactory>().CreateScope())
{
// run db migration
var context = serviceScope.ServiceProvider.GetRequiredService<LinkDatabase>();
if (context.Database.IsRelational())
{
context.Database.Migrate();
}
}
app.Run();
namespace provider
{
// expose this to allow testing with WebApplicationFactory
public partial class Program { }
}