namespace Framework.Config { using Framework.App; using Framework.DataAccessLayer; using Framework.Server; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.IO; /// /// Configuration used by deployed web server. This file is generated by cli build command. It is an /// extract of ConfigCli depending on current EnvironmentName. See also method ConfigCli.CopyConfigCliToConfigServer(); /// internal class ConfigServer { /// /// Gets or sets Comment. Comment to display text: "Do not modify this file ConfigServer.json! Values are copied and extracted from file ConfigCli.json on every wpx command". /// public string Comment { get; set; } /// /// Gets or sets EnvironmentName. For example DEV, TEST or PROD. /// public string EnvironmentName { get; set; } /// /// Gets or sets IsUseDeveloperExceptionPage. If true, show detailed exceptions. Restart web server after value change! /// public bool IsUseDeveloperExceptionPage { get; set; } /// /// Gets or sets IsRedirectHttps. If true, http is redirected to https. By default this value is false. Restart web server after value change! /// public bool IsRedirectHttps { get; set; } /// /// Gets or sets IsRedirectWww. If ture, non www is redirected to wwww. For example workplacex.org is redirected to www.workplacex.org. /// Does not work for loacalhost! /// public bool IsRedirectWww { get; set; } /// /// Gets or sets IsServerSideRendering. By default this value is true. Can be changed on the deployed server for trouble shooting. /// public bool IsServerSideRendering { get; set; } /// /// Gets or sets ConnectionStringFramework. This value is copied from ConfigCli.ConnectionStringFramework by cli build command. /// public string ConnectionStringFramework { get; set; } /// /// Gets or sets ConnectionStringApplication. This value is copied from CliConfig.ConnectionStringApplication by cli build command. /// public string ConnectionStringApplication { get; set; } /// /// Returns ConnectionString. Used by WebServer and Cli. /// public static string ConnectionString(bool isFrameworkDb) { string connectionStringApplication = null; string connectionStringFramework = null; // Application running on WebServer? (or cli) if (UtilServer.Context != null) { var configuration = (IConfiguration)UtilServer.Context.RequestServices.GetService(typeof(IConfiguration)); // ConnectionString defined in file appsettings.json (or Azure) has higher priority than file ConfigServer.json. // Typically this is used on a PROD WebServer only. No ConnectionString on CI server is needed in ConfigCli secret. // For appsettings.json see also: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-strings connectionStringApplication = UtilFramework.StringNull(ConfigurationExtensions.GetConnectionString(configuration, "ConnectionStringApplication")); connectionStringFramework = UtilFramework.StringNull(ConfigurationExtensions.GetConnectionString(configuration, "ConnectionStringFramework")); } if (isFrameworkDb == false) { if (connectionStringApplication != null) { return connectionStringApplication; } return ConfigServer.Load().ConnectionStringApplication; } else { if (connectionStringFramework != null) { return connectionStringFramework; } return ConfigServer.Load().ConnectionStringFramework; } } /// /// Returns ConnectionString for Application or Framework database. /// /// Application or Framework data row. public static string ConnectionString(Type typeRow) { bool isFrameworkDb = UtilDalType.TypeRowIsFrameworkDb(typeRow); return ConnectionString(isFrameworkDb); } public List WebsiteList { get; set; } /// /// Gets ConfigServer.json. Created und updated by CommandBuild. See also publish folder. /// internal static string FileName { get { if (UtilServer.IsIssServer == false) { return UtilFramework.FolderName + "ConfigServer.json"; } else { return UtilServer.FolderNameContentRoot() + "ConfigServer.json"; } } } /// /// Returns default ConfigServer.json /// private static ConfigServer Init() { ConfigServer result = new ConfigServer(); result.IsServerSideRendering = true; result.WebsiteList = new List(); return result; } internal static ConfigServer Load() { ConfigServer result; if (File.Exists(FileName)) { result = UtilFramework.ConfigLoad(FileName); } else { result = Init(); } if (result.WebsiteList == null) { result.WebsiteList = new List(); } var folderNameAngularList = new Dictionary(); int folderNameAngularIndex = 0; foreach (var website in result.WebsiteList) { // Init DomainNameList if (website.DomainNameList == null) { website.DomainNameList = new List(); } // Init FolderNameAngularIndex var folderNameAngular = UtilFramework.FolderNameParse(website.FolderNameAngular); if (folderNameAngular != null) { if (folderNameAngularList.ContainsKey(folderNameAngular) == false) { website.FolderNameAngularIndex = folderNameAngularIndex; folderNameAngularIndex += 1; } else { website.FolderNameAngularIsDuplicate = true; website.FolderNameAngularIndex = folderNameAngularList[folderNameAngular]; } } } return result; } internal static void Save(ConfigServer configServer) { configServer.Comment = "Do not modify this file ConfigServer.json! Values are copied and extracted from file ConfigCli.json on every wpx command!"; UtilFramework.ConfigSave(configServer, FileName); } } internal class ConfigServerWebsite { /// /// Returns FolderNameServer. For example: "Application.Server/Framework/Application.Website/Website01/browser/". /// public string FolderNameServerGet(AppSelector appSelector, string prefixRemove) { string result = "Application.Server/Framework/Application.Website/" + appSelector.ConfigWebsite.FolderNameAngularWebsite + "browser/"; result = result.Substring(prefixRemove.Length); return result; } /// /// Gets or sets FolderNameAngular. This is the FolderName when running on the server. /// public string FolderNameAngular { get; set; } /// /// Gets or sets FolderNameAngularIndex. Two websites with same FolderNameAngular get same index. /// internal int? FolderNameAngularIndex { get; set; } /// /// Gets FolderNameAngularWebsite. For example Website01. /// internal string FolderNameAngularWebsite => string.Format("Website{0:00}/", FolderNameAngularIndex.GetValueOrDefault() + 1); /// /// Gets FolderNameAngularPort. Used for SSR when running in Visual Studio. /// internal int FolderNameAngularPort => 4000 + 1 + FolderNameAngularIndex.GetValueOrDefault(); /// /// Gets or sets FolderNameAngularIsDuplicate. True, if another website has the same FolderNameAngular. /// internal bool FolderNameAngularIsDuplicate { get; set; } public List DomainNameList { get; set; } } /// /// DomainName to AppTypeName. /// internal class ConfigServerWebsiteDomain { public string DomainName { get; set; } /// /// Gets or sets AppTypeName. Needs to derrive from AppJson. For example: "Application.AppJson, Application". If null, index.html is rendered without server side rendering. /// public string AppTypeName { get; set; } /// /// Gets or sets IsRedirectHttps. If true, it redirects on website level. See also property ConfigCliEnvironment.IsRedirectHttps. /// public bool IsRedirectHttps { get; set; } /// /// Gets or sets BingMapKey. See also class BingMap. /// public string BingMapKey { get; set; } /// /// Gets or sets GoogleAnalyticsId. This id is for Google Analytics 4. For example "G-XXXXXXXXXX". /// public string GoogleAnalyticsId { get; set; } /// /// Gets or sets GoogleAdSenseId. For example "ca-pub-XXXXXXXXXXXXXXXX". /// public string GoogleAdSenseId { get; set; } /// /// Gets or sets Custom. /// public object Custom { get; set; } /// /// Gets or sets PasswordSalt. Additional salt value for each website to validate passwords. /// public string PasswordSalt { get; set; } } }