Simplify UI config service interface

This commit is contained in:
Chris Cheetham 2021-06-21 12:50:46 -04:00
Родитель 3d351206f9
Коммит ef71c16c1e
13 изменённых файлов: 64 добавлений и 119 удалений

Просмотреть файл

@ -17,14 +17,6 @@ namespace Steeltoe.InitializrApi.Configuration
/// </summary>
public class UiConfigFile : InitializrApiServiceBase, IUiConfigService
{
/* ----------------------------------------------------------------- *
* fields *
* ----------------------------------------------------------------- */
private readonly InitializrApiOptions _apiOptions;
private UiConfig _uiConfig;
/* ----------------------------------------------------------------- *
* constructors *
* ----------------------------------------------------------------- */
@ -37,37 +29,29 @@ namespace Steeltoe.InitializrApi.Configuration
public UiConfigFile(IOptions<InitializrApiOptions> options, ILogger<UiConfigFile> logger)
: base(logger)
{
_apiOptions = options.Value;
}
/* ----------------------------------------------------------------- *
* methods *
* ----------------------------------------------------------------- */
/// <inheritdoc />
public void Initialize()
{
Logger.LogInformation("loading configuration: {Path}", _apiOptions.UiConfigPath);
var apiOptions = options.Value;
Logger.LogInformation("loading configuration: {Path}", apiOptions.UiConfigPath);
try
{
var configJson = File.ReadAllText(_apiOptions.UiConfig["Path"]);
_uiConfig = Serializer.DeserializeJson<UiConfig>(configJson);
var configJson = File.ReadAllText(apiOptions.UiConfig["Path"]);
UiConfig = Serializer.DeserializeJson<UiConfig>(configJson);
}
catch (FileNotFoundException)
{
throw new ArgumentException($"UI configuration file path does not exist: {_apiOptions.UiConfigPath}");
throw new ArgumentException($"UI configuration file path does not exist: {apiOptions.UiConfigPath}");
}
catch (UnauthorizedAccessException)
{
throw new ArgumentException(
$"UI configuration file path is not a file or cannot be read: {_apiOptions.UiConfigPath}");
$"UI configuration file path is not a file or cannot be read: {apiOptions.UiConfigPath}");
}
}
/// <inheritdoc />
public UiConfig GetUiConfig()
{
return _uiConfig;
}
/* ----------------------------------------------------------------- *
* properties *
* ----------------------------------------------------------------- */
/// <inheritdoc/>
public UiConfig UiConfig { get; }
}
}

Просмотреть файл

@ -15,12 +15,6 @@ namespace Steeltoe.InitializrApi.Configuration
/// </summary>
public class UiConfigService : InitializrApiServiceBase, IUiConfigService
{
/* ----------------------------------------------------------------- *
* fields *
* ----------------------------------------------------------------- */
private readonly UiConfig _uiConfig;
/* ----------------------------------------------------------------- *
* constructors *
* ----------------------------------------------------------------- */
@ -37,7 +31,7 @@ namespace Steeltoe.InitializrApi.Configuration
ILogger<UiConfigService> logger)
: base(logger)
{
_uiConfig = configuration.Value;
UiConfig = configuration.Value;
Logger.LogInformation(
"Config Server: uri={ConfigServer},env={Environment},label={Label}",
settings.Value.Uri,
@ -46,19 +40,10 @@ namespace Steeltoe.InitializrApi.Configuration
}
/* ----------------------------------------------------------------- *
* methods *
* properties *
* ----------------------------------------------------------------- */
/// <inheritdoc />
public void Initialize()
{
Logger.LogInformation("Initializing Initializr configuration");
}
/// <inheritdoc/>
public UiConfig GetUiConfig()
{
return _uiConfig;
}
public UiConfig UiConfig { get; }
}
}

Просмотреть файл

@ -9,6 +9,7 @@ using Steeltoe.InitializrApi.Models;
using Steeltoe.InitializrApi.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Steeltoe.InitializrApi.Controllers
@ -24,7 +25,7 @@ namespace Steeltoe.InitializrApi.Controllers
* fields *
* ----------------------------------------------------------------- */
private readonly IUiConfigService _configService;
private readonly IUiConfigService _uiConfigService;
private readonly IProjectGenerator _projectGenerator;
@ -35,16 +36,16 @@ namespace Steeltoe.InitializrApi.Controllers
/// <summary>
/// Initializes a new instance of the <see cref="ProjectController"/> class.
/// </summary>
/// <param name="configService">Injected Initializr configuration service.</param>
/// <param name="uiConfigService">Injected Initializr configuration service.</param>
/// <param name="projectGenerator">Injected project generator.</param>
/// <param name="logger">Injected logger.</param>
public ProjectController(
IUiConfigService configService,
IUiConfigService uiConfigService,
IProjectGenerator projectGenerator,
ILogger<ProjectController> logger)
: base(logger)
{
_configService = configService;
_uiConfigService = uiConfigService;
_projectGenerator = projectGenerator;
}
@ -60,7 +61,7 @@ namespace Steeltoe.InitializrApi.Controllers
[AcceptVerbs("GET")]
public async Task<ActionResult> GetProjectArchive([FromQuery] ProjectSpec spec)
{
var defaults = _configService.GetUiConfig();
var defaults = _uiConfigService.UiConfig;
var normalizedSpec = new ProjectSpec()
{
Name = spec.Name ?? defaults?.Name?.Default,
@ -85,26 +86,21 @@ namespace Steeltoe.InitializrApi.Controllers
var caseSensitiveDeps = new List<string>();
if (defaults.Dependencies?.Values != null)
{
foreach (var group in defaults.Dependencies.Values)
{
foreach (var dep in group.Values)
{
caseSensitiveDeps.Add(dep.Id);
}
}
caseSensitiveDeps.AddRange(
from @group in defaults.Dependencies.Values
from dep in @group.Values
select dep.Id);
}
var deps = normalizedSpec.Dependencies.Split(',');
for (int i = 0; i < deps.Length; ++i)
{
var found = false;
foreach (var caseSensitiveDep in caseSensitiveDeps)
foreach (var caseSensitiveDep in caseSensitiveDeps.Where(
caseSensitiveDep => caseSensitiveDep.Equals(deps[i], StringComparison.OrdinalIgnoreCase)))
{
if (caseSensitiveDep.Equals(deps[i], StringComparison.OrdinalIgnoreCase))
{
deps[i] = caseSensitiveDep;
found = true;
}
deps[i] = caseSensitiveDep;
found = true;
}
if (!found)

Просмотреть файл

@ -27,7 +27,7 @@ namespace Steeltoe.InitializrApi.Controllers
private readonly InitializrApiOptions _apiOptions;
private readonly IUiConfigService _configService;
private readonly IUiConfigService _uiConfigService;
/* ----------------------------------------------------------------- *
* constructors *
@ -37,16 +37,16 @@ namespace Steeltoe.InitializrApi.Controllers
/// Initializes a new instance of the <see cref="RootController"/> class.
/// </summary>
/// <param name="options">Injected Initializr options.</param>
/// <param name="configService">Injected Initializr configuration service.</param>
/// <param name="uiConfigService">Injected Initializr configuration service.</param>
/// <param name="logger">Injected logger.</param>
public RootController(
IOptions<InitializrApiOptions> options,
IUiConfigService configService,
IUiConfigService uiConfigService,
ILogger<RootController> logger)
: base(logger)
{
_apiOptions = options.Value;
_configService = configService;
_uiConfigService = uiConfigService;
}
/* ----------------------------------------------------------------- *
@ -61,7 +61,7 @@ namespace Steeltoe.InitializrApi.Controllers
public IActionResult GetHelp()
{
var help = new List<string>();
if (!(_apiOptions?.Logo is null))
if (_apiOptions?.Logo is not null)
{
try
{
@ -79,7 +79,7 @@ namespace Steeltoe.InitializrApi.Controllers
help.Add(" :: Steeltoe Initializr :: https://start.steeltoe.io");
help.Add(string.Empty);
var uiConfig = _configService.GetUiConfig();
var uiConfig = _uiConfigService.UiConfig;
help.Add("This service generates quickstart projects that can be easily customized.");
help.Add("Possible customizations include a project's dependencies and .NET target framework.");
help.Add(string.Empty);

Просмотреть файл

@ -19,7 +19,7 @@ namespace Steeltoe.InitializrApi.Controllers
* fields *
* ----------------------------------------------------------------- */
private readonly IUiConfigService _configService;
private readonly IUiConfigService _uiConfigService;
/* ----------------------------------------------------------------- *
* constructors *
@ -28,14 +28,14 @@ namespace Steeltoe.InitializrApi.Controllers
/// <summary>
/// Initializes a new instance of the <see cref="UiConfigController"/> class.
/// </summary>
/// <param name="configService">Injected configuration repository.</param>
/// <param name="uiConfigService">Injected configuration repository.</param>
/// <param name="logger">Injected logger.</param>
public UiConfigController(
IUiConfigService configService,
IUiConfigService uiConfigService,
ILogger<UiConfigController> logger)
: base(logger)
{
_configService = configService;
_uiConfigService = uiConfigService;
}
/* ----------------------------------------------------------------- *
@ -49,7 +49,7 @@ namespace Steeltoe.InitializrApi.Controllers
[HttpGet]
public IActionResult GetUiConfig()
{
return Ok(_configService.GetUiConfig());
return Ok(_uiConfigService.UiConfig);
}
/// <summary>
@ -60,7 +60,7 @@ namespace Steeltoe.InitializrApi.Controllers
[Route("steeltoeVersions")]
public IActionResult GetSteeltoeVersions()
{
return Ok(_configService.GetUiConfig().SteeltoeVersion.Values);
return Ok(_uiConfigService.UiConfig.SteeltoeVersion.Values);
}
/// <summary>
@ -71,7 +71,7 @@ namespace Steeltoe.InitializrApi.Controllers
[Route("dotNetFrameworks")]
public IActionResult GetDotNetFrameworks()
{
return Ok(_configService.GetUiConfig().DotNetFramework.Values);
return Ok(_uiConfigService.UiConfig.DotNetFramework.Values);
}
/// <summary>
@ -82,7 +82,7 @@ namespace Steeltoe.InitializrApi.Controllers
[Route("dotNetTemplates")]
public IActionResult GetDotNetTemplates()
{
return Ok(_configService.GetUiConfig().DotNetTemplate.Values);
return Ok(_uiConfigService.UiConfig.DotNetTemplate.Values);
}
/// <summary>
@ -93,7 +93,7 @@ namespace Steeltoe.InitializrApi.Controllers
[Route("languages")]
public IActionResult GetLanguages()
{
return Ok(_configService.GetUiConfig().Language.Values);
return Ok(_uiConfigService.UiConfig.Language.Values);
}
/// <summary>
@ -104,7 +104,7 @@ namespace Steeltoe.InitializrApi.Controllers
[Route("archiveTypes")]
public IActionResult GetArchiveTypes()
{
return Ok(_configService.GetUiConfig().Packaging.Values);
return Ok(_uiConfigService.UiConfig.Packaging.Values);
}
/// <summary>
@ -115,7 +115,7 @@ namespace Steeltoe.InitializrApi.Controllers
[Route("dependencies")]
public IActionResult GetDependencies()
{
return Ok(_configService.GetUiConfig().Dependencies.Values);
return Ok(_uiConfigService.UiConfig.Dependencies.Values);
}
}
}

Просмотреть файл

@ -3,11 +3,9 @@
// See the LICENSE file in the project root for more information.
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Steeltoe.Extensions.Configuration.ConfigServer;
using Steeltoe.InitializrApi.Models;
using Steeltoe.InitializrApi.Services;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
@ -61,9 +59,7 @@ namespace Steeltoe.InitializrApi
/// </summary>
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
host.Services.GetRequiredService<IUiConfigService>().Initialize();
host.Run();
CreateHostBuilder(args).Build().Run();
}
/// <summary>

Просмотреть файл

@ -1,18 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
namespace Steeltoe.InitializrApi.Services
{
/// <summary>
/// Contract for services that can be initialized and reinitialized.
/// </summary>
public interface IInitializeable
{
/// <summary>
/// Perform initialization.
/// Called when started or reconfigured.
/// </summary>
void Initialize();
}
}

Просмотреть файл

@ -9,12 +9,11 @@ namespace Steeltoe.InitializrApi.Services
/// <summary>
/// Contract for Initializr configuration service implementations.
/// </summary>
public interface IUiConfigService : IInitializeable
public interface IUiConfigService
{
/// <summary>
/// Gets the Initializr configuration.
/// </summary>
/// <returns>Returns an Initializr configuration.</returns>
public UiConfig GetUiConfig();
public UiConfig UiConfig { get; }
}
}

Просмотреть файл

@ -4,9 +4,7 @@ using System.Net.Http.Json;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Steeltoe.InitializrApi.Models;
using Steeltoe.InitializrApi.Services;
using Xunit;
namespace Steeltoe.InitializrApi.Test.Integration
@ -189,7 +187,6 @@ namespace Steeltoe.InitializrApi.Test.Integration
public HttpTests(WebApplicationFactory<Startup> fixture)
{
fixture.Services.GetRequiredService<IUiConfigService>().Initialize();
HttpClient = fixture.CreateClient();
}
}

Просмотреть файл

@ -31,10 +31,12 @@ namespace Steeltoe.InitializrApi.Test.Unit.Configuration
options.Setup(opts => opts.Value).Returns(new InitializrApiOptions
{ UiConfig = new Dictionary<string, string> { { "Path", "no_such_path" } } });
var logger = new NullLogger<UiConfigFile>();
var config = new UiConfigFile(options.Object, logger);
// Act
Action act = () => config.Initialize();
Action act = () =>
{
var _ = new UiConfigFile(options.Object, logger);
};
// Assert
act.Should().Throw<ArgumentException>()
@ -49,10 +51,12 @@ namespace Steeltoe.InitializrApi.Test.Unit.Configuration
options.Setup(opts => opts.Value).Returns(new InitializrApiOptions
{ UiConfig = new Dictionary<string, string> { { "Path", "." } } });
var logger = new NullLogger<UiConfigFile>();
var config = new UiConfigFile(options.Object, logger);
// Act
Action act = () => config.Initialize();
Action act = () =>
{
var _ = new UiConfigFile(options.Object, logger);
};
// Assert
act.Should().Throw<ArgumentException>()
@ -67,10 +71,12 @@ namespace Steeltoe.InitializrApi.Test.Unit.Configuration
options.Setup(opts => opts.Value).Returns(new InitializrApiOptions
{ UiConfig = new Dictionary<string, string> { { "Path", "Steeltoe.InitializrApi.dll" } } });
var logger = new NullLogger<UiConfigFile>();
var config = new UiConfigFile(options.Object, logger);
// Act
Action act = () => config.Initialize();
Action act = () =>
{
var _ = new UiConfigFile(options.Object, logger);
};
// Assert
act.Should().Throw<System.Text.Json.JsonException>();

Просмотреть файл

@ -188,7 +188,7 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers
_generator ??= new TestProjectGenerator();
var configurationService = new Mock<IUiConfigService>();
configurationService.Setup(svc => svc.GetUiConfig()).Returns(_uiConfig);
configurationService.Setup(svc => svc.UiConfig).Returns(_uiConfig);
var logger = new NullLogger<ProjectController>();
var projectController = new ProjectController(configurationService.Object, _generator, logger)
{

Просмотреть файл

@ -103,7 +103,7 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers
}
};
var configService = new Mock<IUiConfigService>();
configService.Setup(repo => repo.GetUiConfig()).Returns(config);
configService.Setup(svc => svc.UiConfig).Returns(config);
var controller = new RootController(initializrCfg.Object, configService.Object,
new NullLogger<RootController>());

Просмотреть файл

@ -25,7 +25,7 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers
// Arrange
var uiConfig = new UiConfig();
var configService = new Mock<IUiConfigService>();
configService.Setup(repo => repo.GetUiConfig()).Returns(uiConfig);
configService.Setup(svc => svc.UiConfig).Returns(uiConfig);
var controller = new UiConfigController(configService.Object, new NullLogger<UiConfigController>());
// Act