Skip to content

Commit

Permalink
Merge pull request #178 from martincostello/AspNetCore-2.1
Browse files Browse the repository at this point in the history
Migrate to ASP.NET Core 2.1
  • Loading branch information
martincostello committed May 31, 2018
2 parents fe55939 + 224db60 commit b599761
Show file tree
Hide file tree
Showing 36 changed files with 3,114 additions and 4,092 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -26,8 +26,8 @@ addons:
- libunwind8

install:
- npm install -g npm
- npm install -g gulp
- npm install -g npm@6.1.0
- npm install -g gulp@3.9.1

script:
- ./build.sh --restore-packages
4 changes: 2 additions & 2 deletions Directory.Build.props
Expand Up @@ -21,7 +21,7 @@
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<LangVersion>latest</LangVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<NoWarn>$(NoWarn);CA1054;CA1055;CA1056;CA1812;CA1819;CS1591;CA2007</NoWarn>
<NoWarn>$(NoWarn);CA1016;CA1054;CA1055;CA1056;CA1812;CA1819;CS1591;CA2007</NoWarn>
<PackageIconUrl>https://martincostello.com/favicon.ico</PackageIconUrl>
<PackageProjectUrl>https://github.com/martincostello/alexa-london-travel-site</PackageProjectUrl>
<PackageLicenseUrl>$(PackageProjectUrl)/blob/master/LICENSE</PackageLicenseUrl>
Expand All @@ -31,7 +31,7 @@
<RepositoryType>git</RepositoryType>
<RepositoryUrl>$(PackageProjectUrl).git</RepositoryUrl>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
<PropertyGroup Condition=" '$(CI)' != '' or '$(TF_BUILD)' != '' ">
Expand Down
6 changes: 3 additions & 3 deletions appveyor.yml
@@ -1,5 +1,5 @@
os: Visual Studio 2017
version: 2.0.{build}
version: 2.1.{build}

environment:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
Expand All @@ -15,8 +15,8 @@ cache:
- '%APPDATA%\npm-cache'

install:
- ps: npm install -g npm@5.3.0 --loglevel=error
- ps: npm install -g gulp --loglevel=error
- ps: npm install -g npm@6.1.0 --loglevel=error
- ps: npm install -g gulp@3.9.1 --loglevel=error

build_script:
- ps: .\Build.ps1
Expand Down
2 changes: 1 addition & 1 deletion global.json
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "2.1.101"
"version": "2.1.300"
}
}
44 changes: 44 additions & 0 deletions src/LondonTravel.Site/AzureEnvironmentSecretManager.cs
@@ -0,0 +1,44 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

namespace MartinCostello.LondonTravel.Site
{
using System;
using Microsoft.Azure.KeyVault.Models;
using Microsoft.Extensions.Configuration.AzureKeyVault;

/// <summary>
/// A class representing an implementation of <see cref="IKeyVaultSecretManager"/>
/// that selects keys based on the Azure environment name. This class cannot be inherited.
/// </summary>
internal sealed class AzureEnvironmentSecretManager : IKeyVaultSecretManager
{
/// <summary>
/// The secret prefix to use for the environment.
/// </summary>
private readonly string _prefix;

/// <summary>
/// Initializes a new instance of the <see cref="AzureEnvironmentSecretManager"/> class.
/// </summary>
/// <param name="azureEnvironment">The name of the Azure environment.</param>
public AzureEnvironmentSecretManager(string azureEnvironment)
{
_prefix = $"LondonTravel-{azureEnvironment}-";
}

/// <inheritdoc />
public string GetKey(SecretBundle secret)
{
return secret.SecretIdentifier.Name.Substring(_prefix.Length)
.Replace("--", "_", StringComparison.Ordinal)
.Replace("-", ":", StringComparison.Ordinal);
}

/// <inheritdoc />
public bool Load(SecretItem secret)
{
return secret.Identifier.Name.StartsWith(_prefix, StringComparison.Ordinal);
}
}
}
2 changes: 1 addition & 1 deletion src/LondonTravel.Site/Controllers/AccountController.cs
Expand Up @@ -356,7 +356,7 @@ private bool IsUrlRoute(string url, string routeName)
}
else
{
int indexOfQuery = url.IndexOf('?');
int indexOfQuery = url.IndexOf('?', StringComparison.Ordinal);

if (indexOfQuery > -1)
{
Expand Down
10 changes: 3 additions & 7 deletions src/LondonTravel.Site/Controllers/HomeController.cs
@@ -1,4 +1,4 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

namespace MartinCostello.LondonTravel.Site.Controllers
Expand Down Expand Up @@ -105,12 +105,8 @@ private async Task MapPreferencesAsync(LinePreferencesViewModel model, Cancellat
model.IsAuthenticated = true;
model.IsLinkedToAlexa = !string.IsNullOrWhiteSpace(user.AlexaToken);

ICollection<LineInfo> lines;

using (ITflService service = _tflFactory.CreateService())
{
lines = await service.GetLinesAsync(cancellationToken);
}
ITflService service = _tflFactory.CreateService();
ICollection<LineInfo> lines = await service.GetLinesAsync(cancellationToken);

MapFavoriteLines(model, lines, user.FavoriteLines);

Expand Down
8 changes: 3 additions & 5 deletions src/LondonTravel.Site/Controllers/ManageController.cs
Expand Up @@ -387,12 +387,10 @@ private async Task<bool> AreLinesValidAsync(UpdateLinePreferencesViewModel model
{
if (model.FavoriteLines != null)
{
IList<string> validLines;
ITflService service = _tflServiceFactory.CreateService();
ICollection<LineInfo> lines = await service.GetLinesAsync(cancellationToken);

using (var service = _tflServiceFactory.CreateService())
{
validLines = (await service.GetLinesAsync(cancellationToken)).Select((p) => p.Id).ToList();
}
IList<string> validLines = lines.Select((p) => p.Id).ToList();

return model.FavoriteLines.All((p) => validLines.Contains(p));
}
Expand Down
@@ -0,0 +1,54 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

namespace MartinCostello.LondonTravel.Site.Extensions
{
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

/// <summary>
/// A class containing extension methods for the <see cref="IConfigurationBuilder"/> interface. This class cannot be inherited.
/// </summary>
public static class IConfigurationBuilderExtensions
{
/// <summary>
/// Configures the application.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to configure.</param>
/// <param name="context">The <see cref="WebHostBuilderContext"/> to use.</param>
/// <returns>
/// The <see cref="IConfigurationBuilder"/> passed as the value of <paramref name="builder"/>.
/// </returns>
public static IConfigurationBuilder ConfigureApplication(this IConfigurationBuilder builder, WebHostBuilderContext context)
{
builder.AddApplicationInsightsSettings(developerMode: context.HostingEnvironment.IsDevelopment());

// Build the configuration so far
IConfiguration config = builder.Build();

// Get the settings for Azure Key Vault
string vault = config["AzureKeyVault:Uri"];
string clientId = config["AzureKeyVault:ClientId"];
string clientSecret = config["AzureKeyVault:ClientSecret"];

bool canUseKeyVault =
!string.IsNullOrEmpty(vault) &&
!string.IsNullOrEmpty(clientId) &&
!string.IsNullOrEmpty(clientSecret);

if (canUseKeyVault)
{
var manager = new AzureEnvironmentSecretManager(config.AzureEnvironment());

builder.AddAzureKeyVault(
vault,
clientId,
clientSecret,
manager);
}

return builder;
}
}
}
50 changes: 50 additions & 0 deletions src/LondonTravel.Site/Extensions/ILoggingBuilderExtensions.cs
@@ -0,0 +1,50 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

namespace MartinCostello.LondonTravel.Site.Extensions
{
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;

/// <summary>
/// A class containing extension methods for the <see cref="ILoggingBuilder"/> interface. This class cannot be inherited.
/// </summary>
public static class ILoggingBuilderExtensions
{
/// <summary>
/// Configures logging for the application.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to configure.</param>
/// <param name="context">The <see cref="WebHostBuilderContext"/> to use.</param>
/// <returns>
/// The <see cref="ILoggingBuilder"/> passed as the value of <paramref name="builder"/>.
/// </returns>
public static ILoggingBuilder ConfigureLogging(this ILoggingBuilder builder, WebHostBuilderContext context)
{
var loggerConfig = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithProperty("AspNetCoreEnvironment", context.HostingEnvironment.EnvironmentName)
.Enrich.WithProperty("AzureDatacenter", context.Configuration.AzureDatacenter())
.Enrich.WithProperty("AzureEnvironment", context.Configuration.AzureEnvironment())
.Enrich.WithProperty("Version", GitMetadata.Commit)
.ReadFrom.Configuration(context.Configuration)
.WriteTo.ApplicationInsightsEvents(context.Configuration.ApplicationInsightsKey());

if (context.HostingEnvironment.IsDevelopment())
{
loggerConfig = loggerConfig.WriteTo.LiterateConsole();
}

string papertrailHostname = context.Configuration.PapertrailHostname();

if (!string.IsNullOrWhiteSpace(papertrailHostname))
{
loggerConfig.WriteTo.Papertrail(papertrailHostname, context.Configuration.PapertrailPort());
}

Log.Logger = loggerConfig.CreateLogger();
return builder.AddSerilog(dispose: true);
}
}
}

0 comments on commit b599761

Please sign in to comment.