From 9b4d0c6ea3a90b2ebc0024a5a5a818a089c14670 Mon Sep 17 00:00:00 2001 From: Alex Hyett Date: Wed, 12 Dec 2018 09:47:36 +0000 Subject: [PATCH] Added project files from lamda-dotnet-console --- .gitignore | 2 + ContactForm.sln | 48 +++++++++++++ src/ContactForm/App.cs | 31 +++++++++ src/ContactForm/ContactForm.csproj | 25 +++++++ src/ContactForm/Function.cs | 67 +++++++++++++++++++ src/ContactForm/Settings/AppSettings.cs | 7 ++ src/ContactForm/appsettings.json | 28 ++++++++ .../aws-lambda-tools-defaults.json | 19 ++++++ .../ContactForm.Tests.csproj | 23 +++++++ test/ContactForm.Tests/FunctionTest.cs | 29 ++++++++ test/ContactForm.Tests/appsettings.json | 28 ++++++++ 11 files changed, 307 insertions(+) create mode 100644 .gitignore create mode 100644 ContactForm.sln create mode 100644 src/ContactForm/App.cs create mode 100644 src/ContactForm/ContactForm.csproj create mode 100644 src/ContactForm/Function.cs create mode 100644 src/ContactForm/Settings/AppSettings.cs create mode 100644 src/ContactForm/appsettings.json create mode 100644 src/ContactForm/aws-lambda-tools-defaults.json create mode 100644 test/ContactForm.Tests/ContactForm.Tests.csproj create mode 100644 test/ContactForm.Tests/FunctionTest.cs create mode 100644 test/ContactForm.Tests/appsettings.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31db540 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +**/bin +**/obj \ No newline at end of file diff --git a/ContactForm.sln b/ContactForm.sln new file mode 100644 index 0000000..04d3709 --- /dev/null +++ b/ContactForm.sln @@ -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 diff --git a/src/ContactForm/App.cs b/src/ContactForm/App.cs new file mode 100644 index 0000000..d05c5a7 --- /dev/null +++ b/src/ContactForm/App.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/src/ContactForm/ContactForm.csproj b/src/ContactForm/ContactForm.csproj new file mode 100644 index 0000000..b6d202d --- /dev/null +++ b/src/ContactForm/ContactForm.csproj @@ -0,0 +1,25 @@ + + + netcoreapp2.1 + true + Lambda + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ContactForm/Function.cs b/src/ContactForm/Function.cs new file mode 100644 index 0000000..53bb713 --- /dev/null +++ b/src/ContactForm/Function.cs @@ -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; + + /// + /// Lamda Function + /// + /// + /// + /// string + 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() + .Destructure.AsScalar() + .CreateLogger(); + + var serviceCollection = new ServiceCollection(); + ConfigureServices(serviceCollection); + + var serviceProvider = serviceCollection.BuildServiceProvider(); + var result = serviceProvider.GetService().Run(input); + Log.CloseAndFlush(); + return result; + } + + private void ConfigureServices(IServiceCollection serviceCollection) + { + serviceCollection.AddTransient(); + serviceCollection.AddSingleton(_appSettings); + serviceCollection.AddSingleton(_logger); + serviceCollection.AddLogging(logBuilder => logBuilder.AddSerilog(dispose: true)); + } + } + +} \ No newline at end of file diff --git a/src/ContactForm/Settings/AppSettings.cs b/src/ContactForm/Settings/AppSettings.cs new file mode 100644 index 0000000..358f15b --- /dev/null +++ b/src/ContactForm/Settings/AppSettings.cs @@ -0,0 +1,7 @@ +namespace ContactForm +{ + public class AppSettings + { + public string Prefix { get; set; } + } +} \ No newline at end of file diff --git a/src/ContactForm/appsettings.json b/src/ContactForm/appsettings.json new file mode 100644 index 0000000..7a7dc0c --- /dev/null +++ b/src/ContactForm/appsettings.json @@ -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" + } + } +} diff --git a/src/ContactForm/aws-lambda-tools-defaults.json b/src/ContactForm/aws-lambda-tools-defaults.json new file mode 100644 index 0000000..1aa09ea --- /dev/null +++ b/src/ContactForm/aws-lambda-tools-defaults.json @@ -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" +} diff --git a/test/ContactForm.Tests/ContactForm.Tests.csproj b/test/ContactForm.Tests/ContactForm.Tests.csproj new file mode 100644 index 0000000..3bc3944 --- /dev/null +++ b/test/ContactForm.Tests/ContactForm.Tests.csproj @@ -0,0 +1,23 @@ + + + + netcoreapp2.1 + + + + + + + + + + + + + + + + + + + diff --git a/test/ContactForm.Tests/FunctionTest.cs b/test/ContactForm.Tests/FunctionTest.cs new file mode 100644 index 0000000..77d51cd --- /dev/null +++ b/test/ContactForm.Tests/FunctionTest.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/test/ContactForm.Tests/appsettings.json b/test/ContactForm.Tests/appsettings.json new file mode 100644 index 0000000..7a7dc0c --- /dev/null +++ b/test/ContactForm.Tests/appsettings.json @@ -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" + } + } +}