Files
raven/server/AyaNova/util/ServerBootConfig.cs
2021-12-01 17:17:15 +00:00

251 lines
12 KiB
C#

using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;
namespace AyaNova.Util
{
/// <summary>
/// Contains config values from bootup
/// </summary>
internal static class ServerBootConfig
{
//############################################################################################################
//STATIC HARD CODED COMPILE TIME DEFAULTS NOT SET THROUGH CONFIG
internal const int FAILED_AUTH_DELAY = 3000;//ms
//UPLOAD LIMITS 1048576 = 1MiB for testing 10737420000 10737418240 10,737,418,240
internal const long MAX_ATTACHMENT_UPLOAD_BYTES = 10737420000;//slight bit of overage as 10737418241=10GiB
internal const long MAX_LOGO_UPLOAD_BYTES = 512000;//500KiB limit
internal const long MAX_IMPORT_FILE_UPLOAD_BYTES = 104857600;//100MiB limit
internal const long MAX_REPORT_TEMPLATE_UPLOAD_BYTES = 15728640;//15MiB limit; currently the largest v7 export for a report template is 828kb, I'm guessing 15mb is more than enough
internal const long MAX_TRANSLATION_UPLOAD_BYTES = 15728640;//15MiB limit; currently export file is 200kb * 50 maximum at a time = 15mb
//############################################################################################################
//############################
//SEEDING FLAG INTERNAL ONLY
//used to speed up seeding with bypasses to normal validation etc
internal static bool SEEDING { get; set; }
//############################
//############################
//MIGRATING FLAG INTERNAL ONLY
//used to speed up v8 migration with bypasses to notification processing
internal static bool MIGRATING { get; set; }
//############################
//Diagnostic static values used during development, may not be related to config at all, this is just a convenient class to put them in
#if (DEBUG)
internal static List<string> TranslationKeysRequested { get; set; }
#endif
//TEST MODE - BOOT WILL ERASE DB AND GENERATE SAMPLE DATA EVERY TIME
//** Not intended for end users
internal static bool AYANOVA_SERVER_TEST_MODE { get; set; }
internal static decimal AYANOVA_SERVER_TEST_MODE_TZ_OFFSET { get; set; }
internal static string AYANOVA_SERVER_TEST_MODE_SEEDLEVEL { get; set; }
//CONTENTROOTPATH
//** Not intended for end users
internal static string AYANOVA_CONTENT_ROOT_PATH { get; set; } //Note: set in startup.cs, not in program.cs as it requires startup IHostingEnvironment
//LANGUAGE / Translation
internal static string AYANOVA_DEFAULT_TRANSLATION { get; set; }
//** Not intended for end users
internal static long AYANOVA_DEFAULT_TRANSLATION_ID { get; set; } //internal setting set at boot by TranslationBiz::ValidateTranslations
//API
internal static string AYANOVA_JWT_SECRET { get; set; }
internal static string AYANOVA_USE_URLS { get; set; }
internal static int AYANOVA_REPORT_RENDERING_TIMEOUT { get; set; }
internal static int AYANOVA_REPORT_RENDERING_MAX_INSTANCES { get; set; }
//DATABASE
internal static string AYANOVA_DB_CONNECTION { get; set; }
//** Not intended for end users
internal static bool AYANOVA_PERMANENTLY_ERASE_DATABASE { get; set; }
//FILE FOLDERS
internal static string AYANOVA_ATTACHMENT_FILES_PATH { get; set; }
internal static string AYANOVA_BACKUP_FILES_PATH { get; set; }
internal static string AYANOVA_TEMP_FILES_PATH { get; set; }
//BACKUP PG_DUMP PATH (IF NOT IN PATH ALREADY)
internal static string AYANOVA_BACKUP_PG_DUMP_PATH { get; set; }
//LOGGING
internal static string AYANOVA_LOG_PATH { get; set; }
internal static string AYANOVA_LOG_LEVEL { get; set; }
internal static bool AYANOVA_LOG_ENABLE_LOGGER_DIAGNOSTIC_LOG { get; set; }
//SECURITY
internal static string AYANOVA_SET_SUPERUSER_PW { get; set; }
//HELPFUL INFORMATION FOR DIAGNOSTICS
internal static Dictionary<string, string> BOOT_DIAGNOSTIC_INFO { get; set; } = new Dictionary<string, string>();
internal static Dictionary<string, string> DBSERVER_DIAGNOSTIC_INFO { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Populate the config from the configuration found at boot
/// called by program.cs
/// </summary>
/// <param name="config"></param>
internal static void SetConfiguration(IConfigurationRoot config)
{
#if (DEBUG)
TranslationKeysRequested = new List<string>();
#endif
bool? bTemp = null;
#region SERVER BASICS
//TEST MODE?
bTemp = config.GetValue<bool?>("AYANOVA_SERVER_TEST_MODE");
AYANOVA_SERVER_TEST_MODE = (null == bTemp) ? false : (bool)bTemp;
AYANOVA_SERVER_TEST_MODE_SEEDLEVEL = config.GetValue<string>("AYANOVA_SERVER_TEST_MODE_SEEDLEVEL");
AYANOVA_SERVER_TEST_MODE_SEEDLEVEL = string.IsNullOrWhiteSpace(AYANOVA_SERVER_TEST_MODE_SEEDLEVEL) ? "small" : AYANOVA_SERVER_TEST_MODE_SEEDLEVEL;
decimal? dTemp = config.GetValue<decimal?>("AYANOVA_SERVER_TEST_MODE_TZ_OFFSET");
AYANOVA_SERVER_TEST_MODE_TZ_OFFSET = (null == dTemp) ? 0 : (decimal)dTemp;
//LANGUAGE
//TranslationBiz will validate this later at boot pfc and ensure a sane default is set (English)
AYANOVA_DEFAULT_TRANSLATION = config.GetValue<string>("AYANOVA_DEFAULT_TRANSLATION");
AYANOVA_DEFAULT_TRANSLATION = string.IsNullOrWhiteSpace(AYANOVA_DEFAULT_TRANSLATION) ? "en" : AYANOVA_DEFAULT_TRANSLATION;
string lowTranslation = AYANOVA_DEFAULT_TRANSLATION.ToLowerInvariant();
switch (lowTranslation)
{
case "en":
case "english":
AYANOVA_DEFAULT_TRANSLATION = "en";
break;
case "de":
case "deutsch":
case "german":
AYANOVA_DEFAULT_TRANSLATION = "de";
break;
case "es":
case "español":
case "spanish":
AYANOVA_DEFAULT_TRANSLATION = "es";
break;
case "fr":
case "français":
case "french":
AYANOVA_DEFAULT_TRANSLATION = "fr";
break;
default:
AYANOVA_DEFAULT_TRANSLATION = "en";
break;
}
//LOGLEVEL
AYANOVA_LOG_LEVEL = config.GetValue<string>("AYANOVA_LOG_LEVEL");
AYANOVA_LOG_LEVEL = string.IsNullOrWhiteSpace(AYANOVA_LOG_LEVEL) ? "Info" : AYANOVA_LOG_LEVEL;
//LOGGING DIAGNOSTIC LOG
bTemp = config.GetValue<bool?>("AYANOVA_LOG_ENABLE_LOGGER_DIAGNOSTIC_LOG");
AYANOVA_LOG_ENABLE_LOGGER_DIAGNOSTIC_LOG = (null == bTemp) ? false : (bool)bTemp;
//PORT / API
AYANOVA_USE_URLS = config.GetValue<string>("AYANOVA_USE_URLS");
AYANOVA_USE_URLS = string.IsNullOrWhiteSpace(AYANOVA_USE_URLS) ? "http://*:7575" : AYANOVA_USE_URLS;
AYANOVA_JWT_SECRET = config.GetValue<string>("AYANOVA_JWT_SECRET");
//backdoor back door password superuser reset
AYANOVA_SET_SUPERUSER_PW = config.GetValue<string>("AYANOVA_SET_SUPERUSER_PW");
//REPORT RENDERING PROCESS CONTROL
int? nTemp = config.GetValue<int?>("AYANOVA_REPORT_RENDERING_TIMEOUT");
AYANOVA_REPORT_RENDERING_TIMEOUT = (null == nTemp) ? 30000 : (int)nTemp;//default is 30 seconds
if (AYANOVA_REPORT_RENDERING_TIMEOUT < 1000) AYANOVA_REPORT_RENDERING_TIMEOUT = 1000; //one second minimum timeout
if (AYANOVA_REPORT_RENDERING_TIMEOUT > 180000) AYANOVA_REPORT_RENDERING_TIMEOUT = 180000; //3 minutes maximum timeout
nTemp = config.GetValue<int?>("AYANOVA_REPORT_RENDERING_MAX_INSTANCES");
AYANOVA_REPORT_RENDERING_MAX_INSTANCES = (null == nTemp) ? 3 : (int)nTemp;
if (AYANOVA_REPORT_RENDERING_MAX_INSTANCES < 1) AYANOVA_REPORT_RENDERING_MAX_INSTANCES = 1; //minimum instances
if (AYANOVA_REPORT_RENDERING_MAX_INSTANCES > 10) AYANOVA_REPORT_RENDERING_MAX_INSTANCES = 10; //Fixed maximum instances
//DB
AYANOVA_DB_CONNECTION = config.GetValue<string>("AYANOVA_DB_CONNECTION");
bTemp = config.GetValue<bool?>("AYANOVA_PERMANENTLY_ERASE_DATABASE");
AYANOVA_PERMANENTLY_ERASE_DATABASE = (null == bTemp) ? false : (bool)bTemp;
//FOLDERS
string DataFolderPath = ActualFullPath(config.GetValue<string>("AYANOVA_DATA_PATH"));
string LogPath = ActualFullPath(config.GetValue<string>("AYANOVA_LOG_PATH"));
string AttachmentFilesPath = ActualFullPath(config.GetValue<string>("AYANOVA_ATTACHMENT_FILES_PATH"));
string BackupFilesPath = ActualFullPath(config.GetValue<string>("AYANOVA_BACKUP_FILES_PATH"));
string TempFilesPath = ActualFullPath(config.GetValue<string>("AYANOVA_TEMP_FILES_PATH"));
AYANOVA_BACKUP_PG_DUMP_PATH = ActualFullPath(config.GetValue<string>("AYANOVA_BACKUP_PG_DUMP_PATH"));
if (string.IsNullOrWhiteSpace(DataFolderPath))
{
//In this case *must* have paths for *everything* specified
if (string.IsNullOrWhiteSpace(LogPath))
throw new System.ArgumentNullException("AYANOVA_LOG_PATH configuration setting missing and required");
if (string.IsNullOrWhiteSpace(AttachmentFilesPath))
throw new System.ArgumentNullException("AYANOVA_ATTACHMENT_FILES_PATH configuration setting missing and required");
if (string.IsNullOrWhiteSpace(BackupFilesPath))
throw new System.ArgumentNullException("AYANOVA_BACKUP_FILES_PATH configuration setting missing and required");
if (string.IsNullOrWhiteSpace(TempFilesPath))
throw new System.ArgumentNullException("AYANOVA_TEMP_FILES_PATH configuration setting missing and required");
}
//set paths
AYANOVA_LOG_PATH = (string.IsNullOrWhiteSpace(LogPath)) ? Path.Combine(DataFolderPath, "logs") : LogPath;
AYANOVA_ATTACHMENT_FILES_PATH = (string.IsNullOrWhiteSpace(AttachmentFilesPath)) ? Path.Combine(DataFolderPath, "attachments") : AttachmentFilesPath;
AYANOVA_BACKUP_FILES_PATH = (string.IsNullOrWhiteSpace(BackupFilesPath)) ? Path.Combine(DataFolderPath, "backups") : BackupFilesPath;
AYANOVA_TEMP_FILES_PATH = (string.IsNullOrWhiteSpace(TempFilesPath)) ? Path.Combine(DataFolderPath, "temp") : TempFilesPath;
#endregion server BASICS
}
internal static string ActualFullPath(string p)
{
if (string.IsNullOrWhiteSpace(p))
return string.Empty;
return Path.GetFullPath(FileUtil.StringPathDecodeEnvironmentVariables(p));
}
//Fetch first url from list of urls (used by generator)
internal static string FirstOfAyaNovaUseUrls
{
get
{
if (string.IsNullOrWhiteSpace(AYANOVA_USE_URLS))
{ return null; }
if (!AYANOVA_USE_URLS.Contains(";"))
{
return AYANOVA_USE_URLS.Replace("*", "localhost");
}
var s = AYANOVA_USE_URLS.Split(';');
return s[0].Replace("*", "localhost");
}
}
}//eoc
}//eons