aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Models/Misc/AppConfiguration.cs
diff options
context:
space:
mode:
authorivar <i@oiee.no>2023-11-12 00:01:31 +0100
committerivar <i@oiee.no>2023-11-12 00:01:31 +0100
commita9f9650400ed9e7f40db5ec5fd08a861f4c30902 (patch)
tree7df4e6a086acc21a5b47b4fae8bf395f79061f29 /code/api/src/Models/Misc/AppConfiguration.cs
parent854dedead3a3ed987997a0132f527db73b65b0ac (diff)
downloadgreatoffice-a9f9650400ed9e7f40db5ec5fd08a861f4c30902.tar.xz
greatoffice-a9f9650400ed9e7f40db5ec5fd08a861f4c30902.zip
Change how config is operated
Diffstat (limited to 'code/api/src/Models/Misc/AppConfiguration.cs')
-rw-r--r--code/api/src/Models/Misc/AppConfiguration.cs102
1 files changed, 95 insertions, 7 deletions
diff --git a/code/api/src/Models/Misc/AppConfiguration.cs b/code/api/src/Models/Misc/AppConfiguration.cs
index 9ed6fe4..a71970c 100644
--- a/code/api/src/Models/Misc/AppConfiguration.cs
+++ b/code/api/src/Models/Misc/AppConfiguration.cs
@@ -1,9 +1,42 @@
+using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
+using System.Text;
namespace IOL.GreatOffice.Api.Models.Models;
public class AppConfiguration
{
+ public AppConfiguration()
+ {
+
+ }
+
+ public AppConfiguration(IConfiguration c)
+ {
+ DB_HOST = c.GetValue<string>(nameof(DB_HOST));
+ DB_PORT = c.GetValue<string>(nameof(DB_PORT));
+ DB_NAME = c.GetValue<string>(nameof(DB_NAME));
+ DB_USER = c.GetValue<string>(nameof(DB_USER));
+ DB_PASSWORD = c.GetValue<string>(nameof(DB_PASSWORD));
+ QUARTZ_DB_HOST = c.GetValue<string>(nameof(QUARTZ_DB_HOST));
+ QUARTZ_DB_NAME = c.GetValue<string>(nameof(QUARTZ_DB_NAME));
+ QUARTZ_DB_PASSWORD = c.GetValue<string>(nameof(QUARTZ_DB_PASSWORD));
+ QUARTZ_DB_USER = c.GetValue<string>(nameof(QUARTZ_DB_USER));
+ QUARTZ_DB_PORT = c.GetValue<string>(nameof(QUARTZ_DB_PORT));
+ APP_CERT = c.GetValue<string>(nameof(APP_CERT));
+ APP_AES_KEY = c.GetValue<string>(nameof(APP_AES_KEY));
+ SEQ_API_KEY = c.GetValue<string>(nameof(SEQ_API_KEY));
+ SEQ_API_URL = c.GetValue<string>(nameof(SEQ_API_URL));
+ POSTMARK_TOKEN = c.GetValue<string>(nameof(POSTMARK_TOKEN));
+ EMAIL_FROM_ADDRESS = c.GetValue<string>(nameof(EMAIL_FROM_ADDRESS));
+ CANONICAL_FRONTEND_URL = c.GetValue<string>(nameof(CANONICAL_FRONTEND_URL));
+ CANONICAL_BACKEND_URL = c.GetValue<string>(nameof(CANONICAL_BACKEND_URL));
+ ASPNETCORE_ENVIRONMENT = c.GetValue<string>(nameof(ASPNETCORE_ENVIRONMENT));
+ _configuration = c;
+ }
+
+ private static IConfiguration _configuration { get; set; }
+
/// <summary>
/// An reachable ip address or url that points to a postgres database.
/// </summary>
@@ -94,6 +127,11 @@ public class AppConfiguration
/// </summary>
public string APP_CERT { get; set; }
+ /// <summary>
+ /// A string signaling to the framework what environment it is running in, usually Development, Testing or Production.
+ /// </summary>
+ public string ASPNETCORE_ENVIRONMENT { get; set; }
+
public X509Certificate2 CERT1() => new(Convert.FromBase64String(APP_CERT));
public object GetPublicObject()
@@ -103,18 +141,68 @@ public class AppConfiguration
DB_HOST,
DB_PORT,
DB_USER,
- DB_PASSWORD = DB_PASSWORD.Obfuscate() ?? "",
+ DB_PASSWORD = DB_PASSWORD.Obfuscate(),
QUARTZ_DB_HOST,
QUARTZ_DB_PORT,
QUARTZ_DB_USER,
- QUARTZ_DB_PASSWORD = QUARTZ_DB_PASSWORD.Obfuscate() ?? "",
- SEQ_API_KEY = SEQ_API_KEY.Obfuscate() ?? "",
+ QUARTZ_DB_PASSWORD = QUARTZ_DB_PASSWORD.Obfuscate(),
+ SEQ_API_KEY = SEQ_API_KEY.Obfuscate(),
SEQ_API_URL,
- POSTMARK_TOKEN = POSTMARK_TOKEN.Obfuscate() ?? "",
+ POSTMARK_TOKEN = POSTMARK_TOKEN.Obfuscate(),
EMAIL_FROM_ADDRESS,
- APP_AES_KEY = APP_AES_KEY.Obfuscate() ?? "",
- CERT1 = CERT1().PublicKey.Oid.FriendlyName,
- CANONICAL_FRONTEND_URL
+ APP_AES_KEY = APP_AES_KEY.Obfuscate(),
+ CERT1 = CERT1().Thumbprint,
+ CANONICAL_FRONTEND_URL,
+ ASPNETCORE_ENVIRONMENT
};
}
+
+ public string GetEnvironmentVariable(string variableName, string fallback = "")
+ {
+ if (_configuration == default)
+ {
+ Debug.WriteLine("AppConfiguration was instantiated without a full IConfiguration");
+ return "";
+ }
+ if (fallback.HasValue()) return _configuration.GetValue(variableName, fallback);
+ return _configuration.GetValue<string>(variableName);
+ }
+
+ public string GetAppDatabaseConnectionString()
+ {
+ var builder = new StringBuilder();
+
+ builder.Append($"Server={DB_HOST};Port={DB_PORT};Database={DB_NAME};User Id={DB_USER};Password={DB_PASSWORD}");
+
+ if (ASPNETCORE_ENVIRONMENT == "Development")
+ {
+ builder.Append(";Include Error Detail=true");
+ }
+
+ Log.Debug("Using app database connection string: " + builder.ToString());
+ return builder.ToString();
+ }
+
+ public string GetQuartzDatabaseConnectionString()
+ {
+ var builder = new StringBuilder();
+
+ builder.Append($"Server={QUARTZ_DB_HOST};Port={QUARTZ_DB_PORT};Database={QUARTZ_DB_NAME};User Id={QUARTZ_DB_USER};Password={QUARTZ_DB_PASSWORD}");
+
+ if (ASPNETCORE_ENVIRONMENT == "Development")
+ {
+ builder.Append(";Include Error Detail=true");
+ }
+
+ Log.Debug("Using quartz database connection string: " + builder.ToString());
+ return builder.ToString();
+ }
+
+ public string GetAppVersion()
+ {
+ var versionFilePath = Path.Combine(AppPaths.AppData.HostPath, "version.txt");
+ if (!File.Exists(versionFilePath)) return "unknown-" + ASPNETCORE_ENVIRONMENT;
+ var versionText = File.ReadAllText(versionFilePath);
+ return versionText + "-" + ASPNETCORE_ENVIRONMENT;
+ }
} \ No newline at end of file