Added project files from lamda-dotnet-console

This commit is contained in:
Alex Hyett 2018-12-12 09:47:36 +00:00
parent 144a482f75
commit 9b4d0c6ea3
11 changed files with 307 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
**/bin
**/obj

48
ContactForm.sln Normal file
View file

@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContactForm", "src\ContactForm\ContactForm.csproj", "{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContactForm.Tests", "test\ContactForm.Tests\ContactForm.Tests.csproj", "{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Debug|x64.ActiveCfg = Debug|Any CPU
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Debug|x64.Build.0 = Debug|Any CPU
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Debug|x86.ActiveCfg = Debug|Any CPU
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Debug|x86.Build.0 = Debug|Any CPU
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Release|Any CPU.Build.0 = Release|Any CPU
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Release|x64.ActiveCfg = Release|Any CPU
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Release|x64.Build.0 = Release|Any CPU
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Release|x86.ActiveCfg = Release|Any CPU
{8A89BCD5-9930-4BC9-9AE5-6239C9A27846}.Release|x86.Build.0 = Release|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Debug|x64.ActiveCfg = Debug|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Debug|x64.Build.0 = Debug|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Debug|x86.ActiveCfg = Debug|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Debug|x86.Build.0 = Debug|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Release|Any CPU.Build.0 = Release|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Release|x64.ActiveCfg = Release|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Release|x64.Build.0 = Release|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Release|x86.ActiveCfg = Release|Any CPU
{D1742E79-E2B1-4F9E-AF74-6407051ADA4F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

31
src/ContactForm/App.cs Normal file
View file

@ -0,0 +1,31 @@
using System;
using Serilog;
namespace ContactForm
{
public class App
{
private readonly AppSettings _settings;
private readonly ILogger _logger;
public App(
AppSettings settings,
ILogger logger
)
{
_settings = settings ??
throw new ArgumentNullException(nameof(settings));
_logger = logger ??
throw new ArgumentNullException(nameof(settings));
}
public string Run(string input)
{
_logger.Information("Lambda Function Starting");
var result = $"{_settings.Prefix}{input?.ToUpper()}";
_logger.Information("Lambda Function Finished");
return result;
}
}
}

View file

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0"/>
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.4.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0"/>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.0"/>
<PackageReference Include="Serilog" Version="2.7.1"/>
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2"/>
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1"/>
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0"/>
</ItemGroup>
<ItemGroup>
<None Include="appsettings.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="PreserveNewest"/>
</ItemGroup>
</Project>

View file

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using Serilog;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly : LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace ContactForm
{
public class Function
{
private AppSettings _appSettings;
private ILogger _logger;
/// <summary>
/// Lamda Function
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns>string</returns>
public string FunctionHandler(string input, ILambdaContext context)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional : false, reloadOnChange : true)
.AddEnvironmentVariables(prefix: "LAMBDA_")
.Build();
_appSettings = new AppSettings();
configuration.GetSection("App").Bind(_appSettings);
_logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Destructure.AsScalar<JObject>()
.Destructure.AsScalar<JArray>()
.CreateLogger();
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();
var result = serviceProvider.GetService<App>().Run(input);
Log.CloseAndFlush();
return result;
}
private void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddTransient<App>();
serviceCollection.AddSingleton<AppSettings>(_appSettings);
serviceCollection.AddSingleton<ILogger>(_logger);
serviceCollection.AddLogging(logBuilder => logBuilder.AddSerilog(dispose: true));
}
}
}

View file

@ -0,0 +1,7 @@
namespace ContactForm
{
public class AppSettings
{
public string Prefix { get; set; }
}
}

View file

@ -0,0 +1,28 @@
{
"App": {
"Prefix": "Test"
},
"Serilog": {
"Using": ["Serilog.Sinks.Seq"],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:5341",
"restrictedToMinimumLevel": "Information"
}
}
],
"Enrich": ["FromLogContext"],
"Properties": {
"ApplicationName": "Lambda.Example"
}
}
}

View file

@ -0,0 +1,19 @@
{
"Information": [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
"dotnet lambda help",
"All the command line options for the Lambda command can be specified in this file."
],
"profile": "default",
"region": "eu-west-1",
"configuration": "Release",
"framework": "netcoreapp2.1",
"function-runtime": "dotnetcore2.1",
"function-memory-size": 256,
"function-timeout": 30,
"function-handler": "lambda-dotnet-console::ContactForm.Function::FunctionHandler"
}

View file

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ContactForm\ContactForm.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="appsettings.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="PreserveNewest"/>
</ItemGroup>
</Project>

View file

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.Lambda.TestUtilities;
using ContactForm;
using Xunit;
namespace ContactForm.Tests
{
public class FunctionTest
{
[Fact]
public void TestToUpperFunction()
{
// Invoke the lambda function and confirm the string was upper cased.
var function = new Function();
var context = new TestLambdaContext();
var upperCase = function.FunctionHandler("hello world", context);
Assert.Equal("TestHELLO WORLD", upperCase);
}
}
}

View file

@ -0,0 +1,28 @@
{
"App": {
"Prefix": "Test"
},
"Serilog": {
"Using": ["Serilog.Sinks.Seq"],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:5341",
"restrictedToMinimumLevel": "Information"
}
}
],
"Enrich": ["FromLogContext"],
"Properties": {
"ApplicationName": "Lambda.Example"
}
}
}