Skip to content

Commit c3af825

Browse files
committed
Reworked Configuration and added support for Enviroment Variable
1 parent aec3d80 commit c3af825

File tree

4 files changed

+83
-51
lines changed

4 files changed

+83
-51
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ Desktop.ini
4141
/dataSources/
4242
/dataSources.local.xml
4343

44-
/ContainerBind
44+
/ContainerBind
45+
/.idea/.idea.OdysseyWebServer/.idea/encodings.xml
46+
/.idea/.idea.OdysseyWebServer/.idea/indexLayout.xml
47+
/.idea/.idea.OdysseyWebServer/.idea/projectSettingsUpdater.xml
48+
/.idea/.idea.OdysseyWebServer/.idea/vcs.xml

OdysseyWebServer/Configuration.cs

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,84 @@ public static class Configuration
1717
public static string UserName { get; set; } = "";
1818
public static string Password { get; set; } = "";
1919

20+
public static readonly IReadOnlyList<ConfigSetter> Configs = new List<ConfigSetter>()
21+
{
22+
new("token", nameof(Token), null, ""),
23+
new("server", nameof(Server), null, ""),
24+
new("port", nameof(Port), ParseInt, 1027),
25+
26+
new("updateFrequency", nameof(UpdateFrequency), ParseInt, 100),
27+
new("updateFlippedPlayers", nameof(UpdateFlippedPlayersFrequency), ParseInt, -1),
28+
new("onlyRequestWhenNecessary", nameof(OnlyRequestWhenNecessary), ParseBool, true),
29+
30+
new("serverName", nameof(ServerName), null, "Odyssey Server"),
31+
new("accountName", nameof(UserName), null, "admin"),
32+
new("accountPassword", nameof(Password), null, "admin"),
33+
};
34+
2035
public static void LoadConfig(ConfigurationManager manager)
2136
{
22-
var settingsPath = File.Exists("/.dockerenv") ? "/data/settings.json" : null;
23-
24-
if (!string.IsNullOrWhiteSpace(settingsPath))
37+
foreach (var config in Configs)
38+
{
39+
var value = manager[config.ConfigName];
40+
if (value == null) continue;
41+
config.SetConfig(value);
42+
}
43+
44+
var settingsPath = GetSettingsPath();
45+
if (settingsPath != null)
2546
{
2647
if (!File.Exists(settingsPath))
2748
{
2849
GenerateSettingsFile(settingsPath);
29-
Console.WriteLine($"Settings was generated in {settingsPath}.\nPlease configure a proper server ip/domain and restart the application.");
30-
while (true)
31-
{
32-
Thread.Sleep(10000);
33-
}
34-
return;
3550
}
3651
var json = File.ReadAllText(settingsPath);
3752
var jObject = JObject.Parse(json);
3853

39-
Token = jObject["token"]?.ToString() ?? "";
40-
Server = jObject["server"]?.ToString() ?? "";
41-
42-
Port = ParseInt(jObject["port"]?.ToString(), 1027);
43-
UpdateFrequency = ParseInt(jObject["updateFrequency"]?.ToString(), 100);
44-
UpdateFlippedPlayersFrequency = ParseInt(jObject["updateFlippedPlayers"]?.ToString(), -1);
45-
OnlyRequestWhenNecessary = ParseBool(jObject["onlyRequestWhenNecessary"]?.ToString(), true);
46-
47-
ServerName = jObject["serverName"]?.ToString() ?? "";
48-
UserName = manager["accountName"] ?? "admin";
49-
Password = manager["accountPassword"] ?? "admin";
50-
return;
54+
foreach (var config in Configs)
55+
{
56+
var value = jObject[config.ConfigName]?.ToString();
57+
if (value == null) continue;
58+
config.SetConfig(value);
59+
}
5160
}
5261

53-
Token = manager["Token"] ?? "";
54-
Server = manager["Server"] ?? "";
55-
Port = ParseInt(manager["Port"], 1027);
56-
UpdateFrequency = ParseInt(manager["UpdateFrequency"], 100);
57-
UpdateFlippedPlayersFrequency = ParseInt(manager["UpdateFlippedPlayersFrequency"], -1);
58-
OnlyRequestWhenNecessary = ParseBool(manager["OnlyRequestWhenNecessary"], true);
59-
ServerName = manager["ServerName"] ?? "Odyssey Online";
60-
UserName = manager["AccountName"] ?? "admin";
61-
Password = manager["AccountPassword"] ?? "admin";
62+
foreach (var config in Configs)
63+
{
64+
var value = Environment.GetEnvironmentVariable(config.ConfigName.ToUpper());
65+
if (value == null) continue;
66+
config.SetConfig(value);
67+
}
68+
69+
if (!string.IsNullOrWhiteSpace(Server) && Port >= 0) return;
70+
71+
Console.WriteLine($"You need to configure a proper Host/IP and Port. Please check your configuration and restart the application. Current Configuration:{Server}:{Port}");
72+
while (true)
73+
{
74+
Thread.Sleep(1000);
75+
}
6276
}
6377

64-
private static int ParseInt(string? input, int defaultValue)
78+
private static object ParseInt(string input, object defaultValue)
6579
{
6680
return int.TryParse(input, out var result) ? result : defaultValue;
6781
}
68-
69-
private static bool ParseBool(string? input, bool defaultValue)
82+
83+
private static object ParseBool(string input, object defaultValue)
7084
{
7185
return bool.TryParse(input, out var result) ? result : defaultValue;
7286
}
7387

88+
private static string? GetSettingsPath()
89+
{
90+
if (File.Exists("/.dockerenv"))
91+
{
92+
return "/data/settings.json";
93+
}
94+
95+
return File.Exists("settings.json") ? "settings.json" : null;
96+
}
97+
7498
private static void GenerateSettingsFile(string file)
7599
{
76100
var jObject = new JObject()
@@ -88,4 +112,15 @@ private static void GenerateSettingsFile(string file)
88112
File.Create(file).Close();
89113
File.WriteAllText(file, jObject.ToString());
90114
}
115+
116+
public class ConfigSetter(string configName, string propertyName, Func<string,object,object>? parser, object defaultValue)
117+
{
118+
public string ConfigName => configName;
119+
120+
public void SetConfig(string value)
121+
{
122+
var finalValue = parser?.Invoke(value, defaultValue) ?? value;
123+
typeof(Configuration).GetProperty(propertyName)?.SetValue(this, finalValue);
124+
}
125+
}
91126
}

OdysseyWebServer/Program.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
var builder = WebApplication.CreateBuilder(args);
88

99
Configuration.LoadConfig(builder.Configuration);
10-
if (string.IsNullOrEmpty(Configuration.Server))
11-
{
12-
Console.WriteLine("Please configure a hostname or ip that connects to a SMOO Server and restart the application.");
13-
while (true)
14-
{
15-
Thread.Sleep(10000);
16-
}
17-
}
1810

1911
Console.WriteLine($"Trying to establish communication with Super Mario Odyssey Online Server: {Configuration.Server}:{Configuration.Port}");
2012
Cache.Initialize().GetAwaiter().GetResult();

OdysseyWebServer/appsettings.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
}
77
},
88
"AllowedHosts": "*",
9-
"Token": "",
10-
"Server": "",
11-
"Port": 1027,
12-
"UpdateFrequency": 100,
13-
"UpdateFlippedPlayersFrequency": -1,
14-
"OnlyRequestWhenNecessary": true,
15-
"ServerName": "Odyssey Server",
16-
"AccountName": "admin",
17-
"AccountPassword": "admin"
9+
10+
"token": "",
11+
"server": "",
12+
"port": 1027,
13+
"updateFrequency": 100,
14+
"updateFlippedPlayersFrequency": -1,
15+
"onlyRequestWhenNecessary": true,
16+
"serverName": "Odyssey Server",
17+
"accountName": "admin",
18+
"accountPassword": "admin"
1819
}

0 commit comments

Comments
 (0)