Move off the web.config in favor of an Archetype config

This commit is contained in:
Kevin Giszewski
2017-09-07 08:55:26 -04:00
parent 77ec2d7a9c
commit a127dac03a
9 changed files with 101 additions and 119 deletions
@@ -9,6 +9,7 @@ using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Mvc;
using Umbraco.Web.Editors;
using Archetype.Extensions;
using Archetype.Models;
namespace Archetype.Api
{
@@ -67,11 +68,14 @@ namespace Archetype.Api
public object GetByGuid(Guid guid, string contentTypeAlias, string propertyTypeAlias, string archetypeAlias, int nodeId)
{
var dataType = Services.DataTypeService.GetDataTypeDefinitionById(guid);
if (dataType == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
var dataTypeDisplay = Mapper.Map<IDataTypeDefinition, DataTypeDisplay>(dataType);
return new { selectedEditor = dataTypeDisplay.SelectedEditor, preValues = dataTypeDisplay.PreValues, contentTypeAlias = contentTypeAlias, propertyTypeAlias = propertyTypeAlias, archetypeAlias = archetypeAlias, nodeId = nodeId };
}
@@ -93,7 +97,7 @@ namespace Archetype.Api
{
return new
{
isCheckingForUpdates = ArchetypeHelper.Instance.GetGlobalSettings().IsCheckingForUpdates
isCheckingForUpdates = ArchetypeGlobalSettings.Instance.CheckForUpdates
};
}
@@ -104,7 +108,8 @@ namespace Archetype.Api
[HttpPost]
public void SetCheckForUpdates([FromBody] bool isChecking)
{
ArchetypeHelper.Instance.SetCheckForUpdates(isChecking);
ArchetypeGlobalSettings.Instance.CheckForUpdates = isChecking;
ArchetypeGlobalSettings.Instance.Save();
}
/// <summary>
@@ -114,7 +119,7 @@ namespace Archetype.Api
[HttpPost]
public object CheckForUpdates()
{
if (!ArchetypeHelper.Instance.GetGlobalSettings().IsCheckingForUpdates)
if (!ArchetypeGlobalSettings.Instance.CheckForUpdates)
{
return new
{
@@ -232,7 +232,6 @@
<Compile Include="Api\ArchetypeDataTypeController.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Events\ExpireCache.cs" />
<Compile Include="Events\StartUp.cs" />
<Compile Include="Extensions\ArchetypeFieldsetModelExtensions.cs" />
<Compile Include="Extensions\ArchetypeHelper.cs" />
<Compile Include="Extensions\ArchetypeModelExtensions.cs" />
@@ -240,6 +239,7 @@
<Compile Include="Extensions\ArchetypePropertyModelExtensions.cs" />
<Compile Include="Extensions\PropertyEditorExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Models\ArchetypeConfigFileModel.cs" />
<Compile Include="Models\ArchetypeGlobalSettings.cs" />
<Compile Include="Models\ArchetypeModel.cs" />
<Compile Include="Models\ArchetypePreValue.cs" />
@@ -13,8 +13,6 @@
public const string CacheKey_PreValueFromDataTypeId = "Archetype_GetArchetypePreValueFromDataTypeId_";
public const string CheckForUpdatesAlias = "Archetype:CheckForUpdates";
public const string IdAlias = "Archetype:Id";
public const string UmbracoVersionAlias = "umbracoConfigurationStatus";
public const string NotificationUrl = "https://api.gizmo42.com/v1/archetype/check-for-updates";
}
@@ -1,38 +0,0 @@
using System;
using System.Web.Configuration;
using Umbraco.Core;
namespace Archetype.Events
{
public class StartUp : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarting(umbracoApplication, applicationContext);
var config = WebConfigurationManager.OpenWebConfiguration("~");
//do we have an Archetype Id?
if (config.AppSettings.Settings[Constants.IdAlias] == null)
{
//guess we need one
config.AppSettings.Settings.Add(Constants.IdAlias, Guid.NewGuid().ToString());
config.Save();
}
else
{
//we have the setting, but is it a GUID?
Guid id;
if (!Guid.TryParse(config.AppSettings.Settings[Constants.IdAlias].Value, out id))
{
config.AppSettings.Settings.Remove(Constants.IdAlias);
config.AppSettings.Settings.Add(Constants.IdAlias, Guid.NewGuid().ToString());
config.Save();
}
//guess we're g2g
}
}
}
}
@@ -31,17 +31,10 @@ namespace Archetype.Extensions
/// The application
/// </summary>
protected ApplicationContext _app;
/// <summary>
/// The global settings
/// </summary>
private readonly ArchetypeGlobalSettings _globalSettings;
/// <summary>
/// The pad lock
/// </summary>
private static readonly object _padLock = new object();
/// <summary>
/// The instance
/// </summary>
@@ -79,7 +72,6 @@ namespace Archetype.Extensions
_jsonSettings = new JsonSerializerSettings { ContractResolver = dcr };
_app = ApplicationContext.Current;
_globalSettings = new ArchetypeGlobalSettings();
}
/// <summary>
@@ -166,25 +158,6 @@ namespace Archetype.Extensions
return prevalues.OverrideDefaultPropertyValueConverter;
}
/// <summary>
/// Gets the global settings.
/// </summary>
/// <returns>ArchetypeGlobalSettings.</returns>
internal ArchetypeGlobalSettings GetGlobalSettings()
{
return _globalSettings;
}
/// <summary>
/// Sets the check for updates.
/// </summary>
/// <param name="isChecking">if set to <c>true</c> [is checking].</param>
internal void SetCheckForUpdates(bool isChecking)
{
_globalSettings.IsCheckingForUpdates = isChecking;
_globalSettings.Save();
}
/// <summary>
/// Checks for updates.
/// </summary>
@@ -195,18 +168,11 @@ namespace Archetype.Extensions
{
using (var client = new HttpClient())
{
var id = ConfigurationManager.AppSettings[Constants.IdAlias];
if (id == null)
{
id = Guid.NewGuid().ToString();
}
var content = new StringContent(JsonConvert.SerializeObject(new
{
umbracoVersion = ConfigurationManager.AppSettings[Constants.UmbracoVersionAlias],
archetypeVersion = DllVersion(),
id = id
id = ArchetypeGlobalSettings.Instance.Id
}), Encoding.UTF8, "application/json");
var response = client.PostAsync(new Uri(Constants.NotificationUrl), content).Result;
@@ -0,0 +1,10 @@
using System;
namespace Archetype.Models
{
public class ArchetypeConfigFileModel
{
public Guid Id { get; set; }
public bool OptInNewVersionNotification { get; set; }
}
}
@@ -1,56 +1,104 @@
using System.Web.Configuration;
using System;
using System.IO;
using Newtonsoft.Json;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
namespace Archetype.Models
{
public class ArchetypeGlobalSettings
{
private bool? _isCheckingForUpdates;
public bool CheckForUpdates { get; set; }
public Guid Id { get; set; }
public bool IsCheckingForUpdates
private static ArchetypeGlobalSettings _instance;
private static string _pathToConfig = @"~/Config/Archetype.config.js";
private static readonly string _mappedPathToConfig = IOHelper.MapPath(_pathToConfig);
private static object _padLock = new object();
private ArchetypeGlobalSettings()
{
}
public static ArchetypeGlobalSettings Instance
{
get
{
if (_isCheckingForUpdates == null)
if (_instance == null)
{
var setting = WebConfigurationManager.AppSettings[Constants.CheckForUpdatesAlias];
if (setting == null)
lock (_padLock)
{
_isCheckingForUpdates = true;
}
if (_instance == null)
{
_instance = new ArchetypeGlobalSettings();
var settingValue = true;
if (bool.TryParse(setting, out settingValue))
{
_isCheckingForUpdates = settingValue;
}
else
{
_isCheckingForUpdates = true;
_loadSettingsFromConfigFile();
}
}
}
return _isCheckingForUpdates.Value;
}
set
{
_isCheckingForUpdates = value;
return _instance;
}
}
public void Save()
{
var config = WebConfigurationManager.OpenWebConfiguration("~");
if (config.AppSettings.Settings[Constants.CheckForUpdatesAlias] != null)
//write to JSON
var configFileModel = new ArchetypeConfigFileModel
{
config.AppSettings.Settings.Remove(Constants.CheckForUpdatesAlias);
}
Id = _instance.Id,
OptInNewVersionNotification = _instance.CheckForUpdates
};
config.AppSettings.Settings.Add(Constants.CheckForUpdatesAlias, IsCheckingForUpdates.ToString());
config.Save();
var serializedJson = JsonConvert.SerializeObject(configFileModel, Formatting.Indented);
File.WriteAllText(_mappedPathToConfig, serializedJson);
}
private static void _loadSettingsFromConfigFile()
{
try
{
if (File.Exists(_mappedPathToConfig))
{
//load
var deserializedConfigFile = JsonConvert.DeserializeObject<ArchetypeConfigFileModel>(File.ReadAllText(_mappedPathToConfig));
if (deserializedConfigFile != null)
{
_instance.Id = deserializedConfigFile.Id;
_instance.CheckForUpdates = deserializedConfigFile.OptInNewVersionNotification;
}
else
{
_createNewConfigFile("Config file model was null!");
}
}
else
{
_createNewConfigFile("Config file was missing!");
}
}
catch (Exception ex)
{
LogHelper.Error<ArchetypeGlobalSettings>(ex.Message, ex);
_createNewConfigFile("Exception!");
}
}
private static void _createNewConfigFile(string reason)
{
//write a new file with defaults
LogHelper.Info<ArchetypeGlobalSettings>(string.Format("Generating a new config file reason: {0}", reason));
_instance.Id = Guid.NewGuid();
_instance.CheckForUpdates = true;
_instance.Save();
}
}
}
+1 -8
View File
@@ -2,14 +2,7 @@ angular.module('umbraco').controller('ArchetypeConfigGlobalOptionsController', f
$scope.globalSettings = {};
$scope.confirmCheckNewVersionChange = function() {
if(confirm("By changing this value, it will cause a restart of the app domain, are you sure?"))
{
archetypeGlobalConfigService.setCheckForUpdates($scope.globalSettings.checkForNewVersion);
}
else
{
$scope.globalSettings.checkForNewVersion = !$scope.globalSettings.checkForNewVersion;
}
archetypeGlobalConfigService.setCheckForUpdates($scope.globalSettings.checkForNewVersion);
}
function getGlobalSettings()
@@ -12,7 +12,7 @@
<label for="archetypeOverrideDefaultConverter"><input type="checkbox" id="archetypeOverrideDefaultConverter" ng-model="dialogData.model.overrideDefaultPropertyValueConverter" /><archetype-localize key="overrideDefaultConverter">Override Default Property Value Converter?</archetype-localize><small><archetype-localize key="overrideDefaultConverterDescription">Check this if you wish to use your own custom property value converter.</archetype-localize></small></label>
</div>
<div ng-controller="ArchetypeConfigGlobalOptionsController">
<label for="archetypeCheckForNewVersion"><input type="checkbox" id="archetypeCheckForNewVersion" ng-model="globalSettings.checkForNewVersion" ng-click="confirmCheckNewVersionChange();" /><archetype-localize key="checkForNewVersion">Automatically check for newer versions?</archetype-localize><small><archetype-localize key="checkForNewVersionDescription">Check this if you wish to be notified when a newer version of Archetype is available. This is a application level setting and changing this will cause the site to restart.</archetype-localize></small></label>
<label for="archetypeCheckForNewVersion"><input type="checkbox" id="archetypeCheckForNewVersion" ng-model="globalSettings.checkForNewVersion" ng-click="confirmCheckNewVersionChange();" /><archetype-localize key="checkForNewVersion">Automatically check for newer versions?</archetype-localize><small><archetype-localize key="checkForNewVersionDescription">Check this if you wish to be notified when a newer version of Archetype is available. This is a application level setting.</archetype-localize></small></label>
</div>
<div>
<label for="archetypeAdvancedOptionsConfigModel"><archetype-localize key="configModel">Config Model</archetype-localize><small><archetype-localize key="configModelDescription">Be careful editing the text below, it controls the schema for this Archetype.</archetype-localize></small></label>