-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
105 lines (94 loc) · 4.18 KB
/
Program.cs
File metadata and controls
105 lines (94 loc) · 4.18 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
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using MultiSourceContentDelivery.DbContexts;
using MultiSourceContentDelivery.Models;
using MultiSourceContentDelivery.Services;
using Polly;
using Polly.Extensions.Http;
namespace MultiSourceContentDelivery
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://*:5010");
// Add configuration
builder.Services.Configure<NodeConfig>(
builder.Configuration.GetSection("NodeConfig"));
builder.Services.AddSingleton(sp =>
sp.GetRequiredService<IOptions<NodeConfig>>().Value);
// Add database context
builder.Services.AddDbContext<FileInfoContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
// Add services
builder.Services.AddHttpClient("NodeSync", client =>
{
client.Timeout = TimeSpan.FromSeconds(30);
client.DefaultRequestHeaders.Add("User-Agent", "MultiSourceContentDelivery-Node");
})
.SetHandlerLifetime(TimeSpan.FromMinutes(5))
.AddTransientHttpErrorPolicy(policy => policy
.WaitAndRetryAsync(3, retryAttempt =>
TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
onRetry: (exception, timeSpan, retryCount, context) =>
{
Console.WriteLine($"正在重试,第 {retryCount} 次尝试,延迟 {timeSpan.TotalSeconds} 秒");
}))
.AddTransientHttpErrorPolicy(policy => policy
.CircuitBreakerAsync(
handledEventsAllowedBeforeBreaking: 5,
durationOfBreak: TimeSpan.FromSeconds(30),
onBreak: (exception, duration) =>
{
Console.WriteLine($"断路器已开启,持续时间:{duration.TotalSeconds} 秒");
},
onReset: () =>
{
Console.WriteLine("断路器已重置");
}));
builder.Services.AddSingleton<FileHashCalculatorService>();
builder.Services.AddSingleton<DnsResolutionService>();
builder.Services.AddScoped<FileStorageService>();
builder.Services.AddHostedService<DirectoryScanService>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.CustomSchemaIds(type =>
{
if (type == typeof(System.IO.FileInfo))
return "SystemFileInfo";
return type.Name;
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<FileInfoContext>();
context.Database.EnsureCreated(); // 确保数据库已创建
context.Database.Migrate(); // 应用所有待处理的迁移
Console.WriteLine("Database migration completed successfully.");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString(), "An error occurred while migrating the database.");
// 根据需要,可以选择在这里停止应用程序或继续运行
}
}
app.Run();
}
}
}