/
Config.cs
151 lines (124 loc) · 4.61 KB
/
Config.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
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
namespace Nagase;
public class Config
{
public const string EnvDebugMode = "DEBUG_MODE";
public const string EnvOpenAIApiKey = "OPENAI_API_KEY";
public const string EnvOpenAIApiRateLimit = "OPENAI_API_RATE_LIMIT";
public const string EnvTelegramBotApiToken = "TELEGRAM_BOT_API_TOKEN";
public const string EnvTelegramBotApiRateLimit = "TELEGRAM_BOT_API_RATE_LIMIT";
public const string EnvChatLifeTime = "CHAT_LIFE_TIME";
public const string EnvChatIdWhiteList = "CHAT_ID_WHITE_LIST";
public const string EnvUserIDWhiteList = "USER_ID_WHITE_LIST";
public const string EnvChatCommand = "CHAT_COMMAND";
const int DefaultChatLifeTime = 300;
const string DefaultChatCommand = "chat";
static Config? instance;
static readonly object lockObject = new();
readonly string chatCommand;
readonly long[] chatIdWhiteList;
readonly long chatLifeTime;
readonly bool debugMode;
readonly string openAIApiKey;
readonly long openAIApiRateLimit;
readonly long telegramBotApiRateLimit;
readonly string telegramBotApiToken;
readonly long[] userIDWhiteList;
Config()
{
GetBoolEnvironmentVariable(out debugMode, EnvDebugMode);
GetStringEnvironmentVariable(out openAIApiKey, EnvOpenAIApiKey);
GetLongEnvironmentVariable(out openAIApiRateLimit, EnvOpenAIApiRateLimit);
GetStringEnvironmentVariable(out telegramBotApiToken, EnvTelegramBotApiToken);
GetLongEnvironmentVariable(out telegramBotApiRateLimit, EnvTelegramBotApiRateLimit);
GetLongEnvironmentVariable(out chatLifeTime, EnvChatLifeTime, DefaultChatLifeTime);
GetLongArrayEnvironmentVariable(out chatIdWhiteList, EnvChatIdWhiteList);
GetLongArrayEnvironmentVariable(out userIDWhiteList, EnvUserIDWhiteList);
GetStringEnvironmentVariable(out chatCommand, EnvChatCommand, DefaultChatCommand);
}
public static Config Instance
{
get
{
if (instance == null)
{
lock (lockObject)
{
instance ??= new Config();
}
}
return instance;
}
}
public bool DebugMode => debugMode;
public string OpenAIApiKey => openAIApiKey;
public long OpenAIApiRateLimit => openAIApiRateLimit;
public string TelegramBotApiToken => telegramBotApiToken;
public long TelegramBotApiRateLimit => telegramBotApiRateLimit;
public long ChatLifeTime => chatLifeTime;
public long[] ChatIdWhiteList => chatIdWhiteList;
public long[] UserIdWhiteList => userIDWhiteList;
public string ChatCommand => chatCommand;
void GetStringEnvironmentVariable(out string field, string env, string defaultValue = "")
{
var nullableStr = Environment.GetEnvironmentVariable(env);
if (!string.IsNullOrWhiteSpace(nullableStr))
{
field = nullableStr;
return;
}
field = defaultValue;
}
void GetStringArrayEnvironmentVariable(out string[] field, string env)
{
GetStringEnvironmentVariable(out var str, env);
field = str.Split(",");
}
void GetIntEnvironmentVariable(out int field, string env, int defaultValue = default)
{
var nullableStr = Environment.GetEnvironmentVariable(env);
if (!string.IsNullOrWhiteSpace(nullableStr))
{
if (int.TryParse(nullableStr, out var value))
{
field = value;
return;
}
}
field = defaultValue;
}
void GetLongArrayEnvironmentVariable(out long[] field, string env)
{
GetStringEnvironmentVariable(out var strEnv, env);
field = strEnv.Split(",").Select(s =>
{
long.TryParse(s, out var o);
return o;
}).ToArray();
}
void GetLongEnvironmentVariable(out long field, string env, long defaultValue = default)
{
var nullableStr = Environment.GetEnvironmentVariable(env);
if (!string.IsNullOrWhiteSpace(nullableStr))
{
if (int.TryParse(nullableStr, out var value))
{
field = value;
return;
}
}
field = defaultValue;
}
void GetBoolEnvironmentVariable(out bool field, string env, bool defaultValue = default)
{
var nullableStr = Environment.GetEnvironmentVariable(env);
if (!string.IsNullOrWhiteSpace(nullableStr))
{
if (bool.TryParse(nullableStr, out var value))
{
field = value;
return;
}
}
field = defaultValue;
}
}