244 lines
9.7 KiB
C#
244 lines
9.7 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Saxonite.Net.Core.Web.Base.App
|
|
{
|
|
//=======================================================================
|
|
public class SaxAppConfig
|
|
{
|
|
//---------------------------------------------------------------
|
|
// Data members.
|
|
private readonly List<String> environments = new List<string>();
|
|
|
|
//---------------------------------------------------------------
|
|
// Construction.
|
|
public SaxAppConfig(String context, ConfigurationSection configSection, SaxAppConfig defaultConfig)
|
|
{
|
|
Context = context;
|
|
populateDefaults(defaultConfig);
|
|
PopulateConfig(configSection);
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
private void populateDefaults(SaxAppConfig defaultConfig)
|
|
{
|
|
if (defaultConfig == null) return;
|
|
Security.Password.MinLength = defaultConfig.Security.Password.MinLength;
|
|
Security.SignIn.KeepLoggedIn.Allowed = defaultConfig.Security.SignIn.KeepLoggedIn.Allowed;
|
|
Security.SignIn.KeepLoggedIn.CookieExpiresDays = defaultConfig.Security.SignIn.KeepLoggedIn.CookieExpiresDays;
|
|
}
|
|
private void PopulateConfig(ConfigurationSection configSection)
|
|
{
|
|
if (configSection != null)
|
|
{
|
|
// Environments.
|
|
foreach (ConfigurationSection environment in configSection.GetSection("Environments").GetChildren())
|
|
environments.Add(StringUtil.TrimAll(environment.Value));
|
|
Instance = StringUtil.OnEmpty(configSection["Instance"], Instance);
|
|
// URLs.
|
|
IConfigurationSection urls = configSection.GetSection("URLs");
|
|
if (urls.Exists())
|
|
{
|
|
IConfigurationSection onlineUrls = urls.GetSection("Online");
|
|
if (onlineUrls.Exists())
|
|
{
|
|
URL.Online.Root = onlineUrls["Root"];
|
|
}
|
|
}
|
|
// Security.
|
|
IConfigurationSection security = configSection.GetSection("Security");
|
|
if (security.Exists())
|
|
{
|
|
// Security - Password.
|
|
IConfigurationSection securityPassword = security.GetSection("Password");
|
|
if (securityPassword.Exists())
|
|
{
|
|
Security.Password.MinLength = StringUtil.ToInt(securityPassword["MinLength"], Security.Password.MinLength);
|
|
}
|
|
// Security - Sign-Up.
|
|
IConfigurationSection securitySignUp = security.GetSection("SignUp");
|
|
if (securitySignUp.Exists())
|
|
{
|
|
}
|
|
// Security - Sign-In.
|
|
IConfigurationSection securitySignIn = security.GetSection("SignIn");
|
|
if (securitySignIn.Exists())
|
|
{
|
|
IConfigurationSection keepLoggedIn = securitySignIn.GetSection("SignIn");
|
|
if (keepLoggedIn.Exists())
|
|
{
|
|
Security.SignIn.KeepLoggedIn.Allowed = StringUtil.ToBool(StringUtil.OnEmpty(keepLoggedIn["Allowed"], Security.SignIn.KeepLoggedIn.Allowed.ToString()));
|
|
Security.SignIn.KeepLoggedIn.CookieExpiresDays = StringUtil.ToInt(keepLoggedIn["CookieExpiresDays"], Security.SignIn.KeepLoggedIn.CookieExpiresDays);
|
|
}
|
|
}
|
|
}
|
|
// Email.
|
|
IConfigurationSection email = configSection.GetSection("Email");
|
|
if (email.Exists())
|
|
{
|
|
Email.Context = email["Context"];
|
|
PopulateEmailConfig(Email.ContactUs, email.GetSection("ContactUs"));
|
|
PopulateEmailConfig(Email.Signup_Activate, email.GetSection("Signup").GetSection("Activate"));
|
|
PopulateEmailConfig(Email.Signup_Welcome, email.GetSection("Signup").GetSection("Welcome"));
|
|
}
|
|
// Documents.
|
|
IConfigurationSection documents = configSection.GetSection("Documents");
|
|
if (documents.Exists())
|
|
{
|
|
Documents.RootFolder = StringUtil.OnEmpty(documents["RootFolder"], Documents.RootFolder);
|
|
}
|
|
// Logging.
|
|
IConfigurationSection logging = configSection.GetSection("Logging");
|
|
if (logging.Exists())
|
|
{
|
|
Logging.RootFolder = StringUtil.OnEmpty(logging["RootFolder"], Logging.RootFolder);
|
|
}
|
|
// Database.
|
|
extractDatabasesConfig(Databases, configSection.GetSection("Databases"));
|
|
// Variants.
|
|
foreach (ConfigurationSection variantSection in configSection.GetSection("Variants").GetChildren())
|
|
{
|
|
// Variants override selected values but must match the application root folder to identify a specific instance.
|
|
String applicationRootPath = variantSection["ApplicationRootFolder"];
|
|
if (StringUtil.IsEmpty(applicationRootPath)) continue;
|
|
if (!SaxSystem.ApplicationFolder.EqualsCI(applicationRootPath)) continue;
|
|
PopulateConfig(variantSection);
|
|
}
|
|
}
|
|
}
|
|
private void PopulateEmailConfig(EmailConfig.SpecificEmailConfig config, IConfigurationSection emailSection)
|
|
{
|
|
if (!emailSection.Exists()) return;
|
|
foreach (var addressTo in emailSection.GetSection("AddressesTo").GetChildren())
|
|
config.AddressesTo.Add(StringUtil.TrimAll(addressTo.Value));
|
|
foreach (var addressCc in emailSection.GetSection("AddressesCc").GetChildren())
|
|
config.AddressesCc.Add(StringUtil.TrimAll(addressCc.Value));
|
|
foreach (var addressBcc in emailSection.GetSection("AddressesBcc").GetChildren())
|
|
config.AddressesBcc.Add(StringUtil.TrimAll(addressBcc.Value));
|
|
}
|
|
private void extractDatabasesConfig(DatabasesConfig databases, IConfigurationSection databasesSection)
|
|
{
|
|
if (!databasesSection.Exists()) return;
|
|
// 'Database' section contains a separate named configuration for each database: 'Core', 'Support' etc.
|
|
foreach (var databaseSection in databasesSection.GetChildren())
|
|
extractDatabaseConfig(databases, databaseSection);
|
|
}
|
|
private void extractDatabaseConfig(DatabasesConfig databases, IConfigurationSection databaseSection)
|
|
{
|
|
if (!databaseSection.Exists()) return;
|
|
Sax.Database databaseType = EnumUtil.ParseEnum<Sax.Database>(databaseSection.Key);
|
|
if (databaseType <= 0)
|
|
{
|
|
SaLogger.Warn(GetType().Name + ".extractDatabaseConfig Unrecognised database: " + databaseSection.Key);
|
|
return;
|
|
}
|
|
// Extract database configuration.
|
|
DatabaseConfig database = new DatabaseConfig();
|
|
database.Location = StringUtil.OnEmpty(databaseSection["Location"], database.Location);
|
|
database.User = StringUtil.OnEmpty(databaseSection["User"], database.User);
|
|
database.Password = StringUtil.OnEmpty(databaseSection["Password"], database.Password);
|
|
databases.Databases.Add(databaseType, database);
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
// Properties.
|
|
public String Context { get; }
|
|
public List<String> Environments { get { return new List<String>(environments); } }
|
|
public String Instance { get; set; } = "";
|
|
// URLs.
|
|
public class URLConfig
|
|
{
|
|
public class OnlineConfig
|
|
{
|
|
public String Root { get; set; }
|
|
}
|
|
public OnlineConfig Online { get; } = new OnlineConfig();
|
|
}
|
|
public URLConfig URL { get; } = new URLConfig();
|
|
// Security.
|
|
public class SecurityConfig
|
|
{
|
|
public class PasswordConfig
|
|
{
|
|
public int MinLength { get; set; }
|
|
}
|
|
public PasswordConfig Password { get; } = new PasswordConfig();
|
|
public class SignUpConfig
|
|
{
|
|
public class ActivationConfig
|
|
{
|
|
public int EmailLinkExpiresMins = Sax.Config.Security.Activation.EmailLinkExpiresMins_Default;
|
|
}
|
|
public ActivationConfig Activation { get; } = new ActivationConfig();
|
|
}
|
|
public SignUpConfig SignUp { get; } = new SignUpConfig();
|
|
public class SignInConfig
|
|
{
|
|
public class PasswordRequestConfig
|
|
{
|
|
public int EmailLinkExpiresMins = Sax.Config.Security.Activation.EmailLinkExpiresMins_Default;
|
|
}
|
|
public PasswordRequestConfig PasswordRequest { get; } = new PasswordRequestConfig();
|
|
public class KeepLoggedInConfig
|
|
{
|
|
public bool Allowed { get; set; } = Sax.Config.Security.SignIn.KeepLoggedIn.Allowed_Default;
|
|
public int CookieExpiresDays { get; set; } = Sax.Config.Security.SignIn.KeepLoggedIn.CookieExpiresDays_Default;
|
|
}
|
|
public KeepLoggedInConfig KeepLoggedIn { get; } = new KeepLoggedInConfig();
|
|
}
|
|
public SignInConfig SignIn { get; } = new SignInConfig();
|
|
}
|
|
public SecurityConfig Security { get; } = new SecurityConfig();
|
|
// Email.
|
|
public class EmailConfig
|
|
{
|
|
public class SpecificEmailConfig
|
|
{
|
|
public List<String> AddressesTo { get; } = new List<string>();
|
|
public List<String> AddressesCc { get; } = new List<string>();
|
|
public List<String> AddressesBcc { get; } = new List<string>();
|
|
}
|
|
public SpecificEmailConfig ContactUs { get; } = new SpecificEmailConfig();
|
|
public SpecificEmailConfig Signup_Activate { get; } = new SpecificEmailConfig();
|
|
public SpecificEmailConfig Signup_Welcome { get; } = new SpecificEmailConfig();
|
|
public String Context { get; internal set; }
|
|
}
|
|
public EmailConfig Email { get; } = new EmailConfig();
|
|
// Databases.
|
|
public class DatabaseConfig
|
|
{
|
|
public String Location { get; internal set; }
|
|
public String User { get; internal set; }
|
|
public String Password { get; internal set; }
|
|
}
|
|
public class DatabasesConfig
|
|
{
|
|
public Dictionary<Sax.Database, DatabaseConfig> Databases { get; } = new Dictionary<Sax.Database, DatabaseConfig>();
|
|
public DatabaseConfig Core { get { return Databases.GetValue(Sax.Database.Core, new DatabaseConfig()); } }
|
|
public DatabaseConfig Support { get { return Databases.GetValue(Sax.Database.Support, new DatabaseConfig()); } }
|
|
}
|
|
public DatabasesConfig Databases { get; } = new DatabasesConfig();
|
|
// Documents.
|
|
public class DocumentsConfig
|
|
{
|
|
public String RootFolder { get; internal set; }
|
|
}
|
|
public DocumentsConfig Documents { get; } = new DocumentsConfig();
|
|
// Logging.
|
|
public class LoggingConfig
|
|
{
|
|
public String RootFolder { get; internal set; }
|
|
}
|
|
public LoggingConfig Logging { get; } = new LoggingConfig();
|
|
|
|
//---------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|