153 lines
6.3 KiB
C#
153 lines
6.3 KiB
C#
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Sarand.Net.Standard.Dbas.Base;
|
|
using Sarand.Net.Standard.Dbas.Data;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Email;
|
|
using Sarand.Net.Standard.Base.IO;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Saxonite.Net.Standard.Base.Consoles;
|
|
using Saxonite.Net.Core.Web.Online.Consoles;
|
|
using Saxonite.Net.Core.Web.Base.App;
|
|
using Saxonite.Net.Core.Web.Base.Helpers;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Saxonite.Net.Standard.Dbas.App;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
|
|
namespace Saxonite.Net.Core.Web.Online.App
|
|
{
|
|
//=======================================================================
|
|
public class SaxApplication_Online : SaxApplication_Web
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private Dictionary<String, SaxAppConfig> appConfigs = new Dictionary<string, SaxAppConfig>();
|
|
private Dictionary<String, SaxEmailConfig> emailConfigs = new Dictionary<string, SaxEmailConfig>();
|
|
// Session contexts.
|
|
private class SessionContextData
|
|
{
|
|
public SessionContextData(SaxSessionContext_Web sessionContext)
|
|
{
|
|
SessionContext = sessionContext;
|
|
}
|
|
public SaxSessionContext_Web SessionContext { get; }
|
|
public ISacsDatabase CoreDatabase { get; set; }
|
|
}
|
|
private readonly Dictionary<int, SessionContextData> sessionContexts = new Dictionary<int, SessionContextData>();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SaxApplication_Online(IConfiguration configuration)
|
|
{
|
|
InitialiseTemporaryLoggingFolder();
|
|
try
|
|
{
|
|
AppInstance = this;
|
|
SaxGlobals.App = this;
|
|
SaxGlobals.System = (System_Web = new SaxSystem_Web());
|
|
Configuration = configuration;
|
|
ConsoleHelper_Web = new SaxConsoleHelper_WebOnline(this);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Error(GetType().Name + ".CTR " + ExceptionUtil.GetErrorStateMessage(exc));
|
|
throw exc;
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public static SaxApplication_Online AppInstance { get; private set; }
|
|
public override ISacsDatabase GetCoreDatabase(SaxSessionContext sessionContext)
|
|
{
|
|
return sessionContexts.GetValue(sessionContext.Index, null)?.CoreDatabase;
|
|
}
|
|
public override String SessionCookieName { get { return Sax.Config.Online.Session.Cookie.Name; } }
|
|
// Configuration.
|
|
public override SaxAppConfig AppConfig { get { return appConfigs.Values.ToList().getConfigForApplication(); } }
|
|
public override SaxEmailConfig EmailConfig { get { return emailConfigs.Values.ToList().getConfigForSession(AppConfig); } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Initialisation.
|
|
/// <summary>Initialise environment - from Startup.</summary>
|
|
internal void InitWebHostEnvironment(IWebHostEnvironment env)
|
|
{
|
|
InitEnvironment(env);
|
|
}
|
|
/// <summary>Initialise environment - from internal (above) or unit tests.</summary>
|
|
protected void InitEnvironment(IWebHostEnvironment env)
|
|
{
|
|
String fn = GetType().Name + ".InitEnvironment";
|
|
Development = env.IsDevelopment();
|
|
environmentName = env.EnvironmentName;
|
|
SaLogger.LogFilenamePrefix = "Sax-Web-Onl";
|
|
SaLogger.IncludeHostInFileMessage = false;
|
|
SaLogger.IncludeUserInFileMessage = false;
|
|
|
|
try
|
|
{
|
|
// Load server configuration.
|
|
var builder = new ConfigurationBuilder()
|
|
.SetBasePath(env.ContentRootPath)
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
|
|
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false)
|
|
.AddJsonFile($"appsettings.machine.{SaxSystem.ComputerName}.json", optional: false, reloadOnChange: false)
|
|
.AddEnvironmentVariables();
|
|
SaxConfiguration = builder.Build();
|
|
|
|
// Application configuration.
|
|
appConfigs = SaxAppConfigHelper.ExtractAppConfigs(SaxConfiguration.GetSection("AppConfigs"));
|
|
emailConfigs = SaxEmailConfigHelper.ExtractEmailConfigs(SaxConfiguration.GetSection("EmailConfigs"));
|
|
LoggingRootFolder = AppConfig.Logging.RootFolder;
|
|
|
|
// Application instance.
|
|
if (AppConfig.Instance.EqualsCI("Dev")) instanceType = Sax.InstanceType.Dev;
|
|
else if (AppConfig.Instance.ContainsCI("Q")) instanceType = Sax.InstanceType.QC;
|
|
else if (AppConfig.Instance.ContainsCI("UAT")) instanceType = Sax.InstanceType.UAT;
|
|
else if (AppConfig.Instance.EqualsCI("PreProd")) instanceType = Sax.InstanceType.UAT;
|
|
|
|
// Initialise hard-coded logging path.
|
|
if (StringUtil.IsEmpty(LoggingRootFolder))
|
|
{
|
|
if (SaxSystem.ComputerName.EqualsCI("WIN-BPCLM1QUOMT"))
|
|
LoggingRootFolder = @"D:\Saxonite\Logs\" + (Development ? "Dev" : "Test");
|
|
}
|
|
if (DirectoryUtil.DirectoryExists(LoggingRootFolder))
|
|
SaLogger.LogFolder = LoggingRootFolder;
|
|
LogApplicationStart(fn);
|
|
|
|
// Configure database connection.
|
|
SacsDatabaseConsole.OdbcConfig config = new SacsDatabaseConsole.OdbcConfig();
|
|
config.location = AppConfig.CoreDatabase.Location;
|
|
config.user = AppConfig.CoreDatabase.User;
|
|
config.pword = AppConfig.CoreDatabase.Password;
|
|
SacsDatabase coreDatabase = SacsDatabaseConsole.OpenOdbcDatabase(config);
|
|
if (coreDatabase.HasErrorState())
|
|
SaLogger.Warn(fn + " Database initialisation error: " + coreDatabase.ErrorStateMessage);
|
|
|
|
// Session context - only one context available/necessary (for now).
|
|
SessionContextData sessionContextData = new SessionContextData(new SaxSessionContext_Web(100, Sax.Application.Online));
|
|
sessionContextData.CoreDatabase = coreDatabase;
|
|
sessionContexts.Add(sessionContextData.SessionContext.Index, sessionContextData);
|
|
SessionContext = sessionContextData.SessionContext;
|
|
|
|
// Data Cache console - to initialise refresh. Must be done for every Session Context.
|
|
SaxDataCacheConsole dataCacheConsole = SaxGlobals.ConsoleHelper.GetDataCacheConsole(SessionContext);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Error(fn + " " + ExceptionUtil.GetLogMessage(exc));
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|