-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (74 loc) · 2.83 KB
/
Copy pathProgram.cs
File metadata and controls
78 lines (74 loc) · 2.83 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
using GithubKookBot.Models;
using GithubKookBot.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Text.Json;
namespace GithubKookBot;
internal static class Program
{
[STAThread]
private static void Main()
{
ApplicationConfiguration.Initialize();
FileLogger.Info("GithubKookBot 程序开始启动");
string configPath = Path.Combine(AppContext.BaseDirectory, "config.json");
if (!File.Exists(configPath))
{
FileLogger.Info("未检测到 config.json,自动生成默认配置文件");
CreateDefaultAppSettings(configPath);
}
ConfigSettings globalAppSettings;
string jsonText = File.ReadAllText(configPath);
var jsonOpt = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var doc = JsonDocument.Parse(jsonText);
string appSettingJson = doc.RootElement.GetProperty("Config").GetRawText();
globalAppSettings = JsonSerializer.Deserialize<ConfigSettings>(appSettingJson) ?? new ConfigSettings();
var services = new ServiceCollection();
services.AddSingleton(globalAppSettings);
services.AddLogging(builder =>
{
builder.AddConsole();
});
services.AddHttpClient<GithubService>();
services.AddHttpClient<KookService>();
services.AddSingleton<StateStore>();
services.AddSingleton<UpdateChecker>();
services.AddTransient<Main>();
var sp = services.BuildServiceProvider();
try
{
var form = sp.GetRequiredService<Main>();
Application.Run(form);
FileLogger.Info("程序正常退出");
}
catch (Exception ex)
{
FileLogger.Error("程序启动发生致命异常", ex);
MessageBox.Show($"程序启动失败:{ex.Message}", "致命错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static void CreateDefaultAppSettings(string filePath)
{
var defaultSetting = new ConfigSettings
{
GithubToken = "",
KookBotToken = "",
KookChannelId = "",
CheckIntervalMinutes = 30,
UpdateMode = "Release",
Subscriptions = new List<SubscriptionConfig>(),
BaiduApiKey = "",
BaiduSecretKey = "",
FetchCommitCount = 10
};
var root = new
{
Logging = new { LogLevel = new { Default = "Information", Microsoft = "Warning" } },
Config = defaultSetting
};
string json = JsonSerializer.Serialize(root, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(filePath, json);
FileLogger.Info($"默认配置文件已生成:{filePath}");
}
}