-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
151 lines (123 loc) · 4.16 KB
/
Program.cs
File metadata and controls
151 lines (123 loc) · 4.16 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
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
using System.Diagnostics;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using FluentScheduler;
using Microsoft.AspNetCore.HttpOverrides;
using RestSharp;
using Wx.Share.Models.Settings;
using Wx.Share.Utils.Caching;
using Wx.Share.Utils.Dao;
using Wx.Share.Utils.Wx;
namespace Wx.Share;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// 配置文件
var appSettings = builder.Configuration.Get<AppSettings>();
// 运行配置
builder.WebHost.ConfigureKestrel((b, options) =>
{
var unixSocket = appSettings?.UnixSocket;
var port = appSettings?.Port ?? 0;
if (port > 0)
options.ListenLocalhost(port);
if (!string.IsNullOrWhiteSpace(unixSocket))
options.ListenUnixSocket(unixSocket);
});
var services = builder.Services;
if (!Directory.Exists(appSettings!.DataBase.Directory))
Directory.CreateDirectory(appSettings.DataBase.Directory);
// 转接头,代理
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
//配置跨域
services.AddCors(options =>
{
options.AddDefaultPolicy(b => b
.SetIsOriginAllowedToAllowWildcardSubdomains()
.WithOrigins(appSettings.WithOrigins.ToArray())
.WithMethods("GET", "POST", "OPTIONS")
.AllowAnyHeader()
.AllowCredentials());
});
// 终结点
services.AddControllers().AddJsonOptions(opt =>
{
opt.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
opt.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
});
services.AddSingleton(appSettings);
services.AddSingleton(new RestClient(new HttpClient()));
services.AddSingleton<CachingDbContext>();
services.AddSingleton<MainDbContext>();
services.AddScoped<StringCachingDao>();
services.AddScoped<WxSignPackageCachingDao>();
services.AddScoped<SharePageDao>();
services.AddScoped<WxJsSdk>();
var app = builder.Build();
// 开发模式输出错误
if (app.Environment.IsDevelopment()) app.UseHttpLogging();
// 静态文件
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseForwardedHeaders();
app.UseRouting();
app.UseCors();
app.MapControllers();
var serviceScope = app.Services.CreateScope();
var s = serviceScope.ServiceProvider;
if (!string.IsNullOrWhiteSpace(appSettings.WxSdk.AppId) &&
!string.IsNullOrWhiteSpace(appSettings.WxSdk.AppSecret))
{
var wxJsSdk = s.GetRequiredService<WxJsSdk>();
JobManager.Initialize();
JobManager.AddJob(
async () =>
{
var a = 0;
while (!await wxJsSdk.RefreshAccessTokenAsync())
{
a++;
if (a > 3) break;
}
var b = 0;
while (!await wxJsSdk.RefreshJsApiTicketAsync())
{
b++;
if (b > 3) break;
}
},
sc => sc.ToRunEvery(6900).Seconds()
);
}
var life = s.GetRequiredService<IHostApplicationLifetime>();
var cachingDb = s.GetRequiredService<CachingDbContext>();
var mainDb = s.GetRequiredService<MainDbContext>();
life.ApplicationStarted.Register(() =>
{
// 获取并写入pid文件
var pid = Process.GetCurrentProcess().Id;
TextWriter pidWriter = new StreamWriter(appSettings.PidFile);
pidWriter.Write(pid);
pidWriter.Flush();
pidWriter.Close();
});
life.ApplicationStopped.Register(() =>
{
// 删除文件
if (File.Exists(appSettings.PidFile)) File.Delete(appSettings.PidFile);
if (!string.IsNullOrWhiteSpace(appSettings.UnixSocket) & File.Exists(appSettings.UnixSocket))
File.Delete(appSettings.UnixSocket);
cachingDb.Database.Dispose();
mainDb.Database.Dispose();
});
app.Run();
}
}