Browse Source

Implemented exposed values.

pull/386/head
Sebastian Stehle 7 years ago
parent
commit
b227ce822d
  1. 15
      src/Squidex.Web/ExposedConfiguration.cs
  2. 56
      src/Squidex.Web/ExposedValues.cs
  3. 19
      src/Squidex/Areas/Api/Controllers/Ping/PingController.cs
  4. 4
      src/Squidex/Config/Web/WebServices.cs
  5. 11
      src/Squidex/appsettings.json
  6. 77
      tests/Squidex.Web.Tests/ExposedValuesTests.cs

15
src/Squidex.Web/ExposedConfiguration.cs

@ -0,0 +1,15 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
namespace Squidex.Web
{
public sealed class ExposedConfiguration : Dictionary<string, string>
{
}
}

56
src/Squidex.Web/ExposedValues.cs

@ -0,0 +1,56 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Configuration;
using Squidex.Infrastructure;
namespace Squidex.Web
{
public sealed class ExposedValues : Dictionary<string, string>
{
public ExposedValues()
{
}
public ExposedValues(ExposedConfiguration configured, IConfiguration configuration)
{
Guard.NotNull(configured, nameof(configured));
Guard.NotNull(configuration, nameof(configuration));
foreach (var kvp in configured)
{
var value = configuration.GetValue<string>(kvp.Value);
if (!string.IsNullOrWhiteSpace(value))
{
this[kvp.Key] = value;
}
}
}
public override string ToString()
{
var sb = new StringBuilder();
foreach (var kvp in this)
{
if (sb.Length > 0)
{
sb.Append(", ");
}
sb.Append(kvp.Key);
sb.Append(": ");
sb.Append(kvp.Value);
}
return sb.ToString();
}
}
}

19
src/Squidex/Areas/Api/Controllers/Ping/PingController.cs

@ -18,9 +18,26 @@ namespace Squidex.Areas.Api.Controllers.Ping
[ApiExplorerSettings(GroupName = nameof(Ping))]
public sealed class PingController : ApiController
{
public PingController(ICommandBus commandBus)
private readonly ExposedValues exposedValues;
public PingController(ICommandBus commandBus, ExposedValues exposedValues)
: base(commandBus)
{
this.exposedValues = exposedValues;
}
/// <summary>
/// Get general info status of the API.
/// </summary>
/// <returns>
/// 200 => Infos returned.
/// </returns>
[HttpGet]
[ProducesResponseType(typeof(ExposedValues), 200)]
[Route("info/")]
public IActionResult Info()
{
return Ok(exposedValues);
}
/// <summary>

4
src/Squidex/Config/Web/WebServices.cs

@ -8,6 +8,7 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Squidex.Config.Domain;
using Squidex.Domain.Apps.Entities;
using Squidex.Pipeline.Plugins;
@ -21,6 +22,9 @@ namespace Squidex.Config.Web
{
public static void AddMyMvcWithPlugins(this IServiceCollection services, IConfiguration config)
{
services.AddSingletonAs(c => new ExposedValues(c.GetRequiredService<IOptions<ExposedConfiguration>>().Value, config))
.AsSelf();
services.AddSingletonAs<FileCallbackResultExecutor>()
.AsSelf();

11
src/Squidex/appsettings.json

@ -120,8 +120,8 @@
/*
* The text for the robots.txt file
*/
"text": "User-agent: *\nAllow: /api/assets/*"
},
"text": "User-agent: *\nAllow: /api/assets/*"
},
"healthz": {
"gc": {
@ -470,5 +470,12 @@
* Set to true to rebuild indexes.
*/
"indexes": false
},
/*
* A list of configuration valeus that should be exposed from the info endpoint and in the UI.
*/
"exposedConfiguration": {
"MY_KEY": "MY_VALUE"
}
}

77
tests/Squidex.Web.Tests/ExposedValuesTests.cs

@ -0,0 +1,77 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using FakeItEasy;
using Microsoft.Extensions.Configuration;
using Xunit;
namespace Squidex.Web
{
public class ExposedValuesTests
{
[Fact]
public void Should_create_from_configuration()
{
var source = new ExposedConfiguration
{
["name1"] = "config1",
["name2"] = "config2",
["name3"] = "config3",
};
var configuration = A.Fake<IConfiguration>();
SetupConfiguration(configuration, "config1", "value1");
SetupConfiguration(configuration, "config2", "value2");
var values = new ExposedValues(source, configuration);
Assert.Equal(2, values.Count);
Assert.Equal("value1", values["name1"]);
Assert.Equal("value2", values["name2"]);
}
[Fact]
public void Should_format_empty_values()
{
var values = new ExposedValues();
Assert.Empty(values.ToString());
}
[Fact]
public void Should_format_from_single_value()
{
var values = new ExposedValues
{
["name1"] = "value1"
};
Assert.Equal("name1: value1", values.ToString());
}
[Fact]
public void Should_format_from_multiple_values()
{
var values = new ExposedValues
{
["name1"] = "value1",
["name2"] = "value2"
};
Assert.Equal("name1: value1, name2: value2", values.ToString());
}
private static void SetupConfiguration(IConfiguration configuration, string key, string value)
{
var configSection = A.Fake<IConfigurationSection>();
A.CallTo(() => configSection.Value).Returns(value);
A.CallTo(() => configuration.GetSection(key)).Returns(configSection);
}
}
}
Loading…
Cancel
Save