Skip to content

Commit 31dd76f

Browse files
committed
Refactor config
1 parent 6e81b3b commit 31dd76f

File tree

3 files changed

+68
-42
lines changed

3 files changed

+68
-42
lines changed

Api5704/Config.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@ public class Config
4545
/// </summary>
4646
public string ServerAddress { get; set; } = "https://ssp.nbki.ru/qbch/";
4747

48+
/// <summary>
49+
/// Проверять отпечаток сервера
50+
/// (имеет смысл при указанном ServerThumbprint).
51+
/// </summary>
52+
public bool ValidateThumbprint { get; set; } = false;
53+
4854
/// <summary>
4955
/// Отпечаток сертификата сервера ServerAddress
5056
/// (имеет смысл при включении ValidateThumbprint).
5157
/// </summary>
52-
public string? ServerThumbprint { get; set; } = "18042E6D06AE9F05B639DF511A8583FEDE72784D";
58+
public string ServerThumbprint { get; set; } = string.Empty;
5359

5460
/// <summary>
5561
/// Проверять валидность сертификатов для подключения.
5662
/// </summary>
5763
public bool ValidateTls { get; set; } = true;
58-
59-
/// <summary>
60-
/// Проверять отпечаток сервера
61-
/// (имеет смысл при указанном ServerThumbprint).
62-
/// </summary>
63-
public bool ValidateThumbprint { get; set; } = true;
6464

6565
/// <summary>
6666
/// Показывать дамп сертификата сервера при подключении.
@@ -76,7 +76,7 @@ public class Config
7676
/// Адрес и порт сервера прокси
7777
/// (имеет смысл при включении UseProxy).
7878
/// </summary>
79-
public string? ProxyAddress { get; set; } = "http://192.168.2.1:3128";
79+
public string ProxyAddress { get; set; } = "http://192.168.2.1:3128";
8080

8181
/// <summary>
8282
/// Подписывать файлы при отправке или же они уже подписаны

Api5704/ConfigManager.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#region License
2+
/*
3+
Copyright 2022-2024 Dmitrii Evdokimov
4+
Open source software
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
#endregion
19+
20+
using System.Text.Json;
21+
22+
namespace Api5704;
23+
24+
public static class ConfigManager
25+
{
26+
private static JsonSerializerOptions GetJsonOptions()
27+
{
28+
return new()
29+
{
30+
WriteIndented = true
31+
};
32+
}
33+
34+
public static Config Read()
35+
{
36+
try
37+
{
38+
string appsettings = Path.ChangeExtension(Environment.ProcessPath!, ".config.json");
39+
40+
if (File.Exists(appsettings))
41+
{
42+
using var read = File.OpenRead(appsettings);
43+
return JsonSerializer.Deserialize<Config>(read)!;
44+
}
45+
46+
using var write = File.OpenWrite(appsettings);
47+
JsonSerializer.Serialize(write, new Config(), GetJsonOptions());
48+
49+
Console.WriteLine(@$"Создан новый файл настроек ""{appsettings}"" - откорректируйте его.");
50+
throw new Exception();
51+
}
52+
catch
53+
{
54+
Environment.Exit(2);
55+
}
56+
57+
return new();
58+
}
59+
}

Api5704/Program.cs

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,13 @@ namespace Api5704;
2525

2626
internal class Program
2727
{
28-
public static Config Config { get; private set; } = new();
28+
public static Config Config { get; set; } = ConfigManager.Read();
2929

3030
static async Task Main(string[] args)
3131
{
3232
Console.WriteLine("Hello, World!"); // :)
3333
int result = 0;
3434

35-
if (!TryGetConfig())
36-
{
37-
Environment.Exit(2);
38-
}
39-
4035
if (args.Length == 0)
4136
{
4237
if (!string.IsNullOrEmpty(Config.DirSource))
@@ -166,32 +161,4 @@ Подробнее см. в README.
166161

167162
Environment.Exit(1);
168163
}
169-
170-
private static JsonSerializerOptions GetJsonOptions()
171-
{
172-
return new()
173-
{
174-
WriteIndented = true
175-
};
176-
}
177-
178-
private static bool TryGetConfig()
179-
{
180-
string appsettings = Path.ChangeExtension(Environment.ProcessPath!, ".config.json");
181-
182-
if (File.Exists(appsettings))
183-
{
184-
using var stream = File.OpenRead(appsettings);
185-
Config = JsonSerializer.Deserialize<Config>(stream)!;
186-
return true;
187-
}
188-
else
189-
{
190-
using var stream = File.OpenWrite(appsettings);
191-
JsonSerializer.Serialize(stream, Config, GetJsonOptions());
192-
193-
Console.WriteLine(@$"Создан новый файл настроек ""{appsettings}"" - откорректируйте его.");
194-
return false;
195-
}
196-
}
197164
}

0 commit comments

Comments
 (0)