This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
119 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": 1, | ||
"isRoot": true, | ||
"tools": { | ||
"cake.tool": { | ||
"version": "0.37.0", | ||
"commands": [ | ||
"dotnet-cake" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
dotnet tool install Cake.Tool --global --version 0.31.0 | ||
dotnet tool restore | ||
dotnet cake build.cake --bootstrap | ||
dotnet cake build.cake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
dotnet tool install Cake.Tool --global --version 0.31.0 | ||
dotnet tool restore | ||
dotnet cake build.cake --bootstrap | ||
dotnet cake build.cake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<AssemblyName>BeanstalkWorker.SimpleRouting.Core</AssemblyName> | ||
<RootNamespace>BeanstalkWorker.SimpleRouting.Core</RootNamespace> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="AWSSDK.SQS" Version="3.3.3.44" /> | ||
<PackageReference Include="BeanstalkWorker.SimpleRouting" Version="1.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Options" Version="2.2.0" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" /> | ||
<PackageReference Include="AWSSDK.SQS" Version="3.3.102.118" /> | ||
<PackageReference Include="BeanstalkWorker.SimpleRouting" Version="1.0.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.4" /> | ||
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.4" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,23 @@ | ||
using System; | ||
using Amazon.SQS.Model; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Serilog; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace BeanstalkWorker.SimpleRouting.SampleWeb | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
BuildWebHost(args).Run(); | ||
BuildWebHost(args).Build().Run(); | ||
} | ||
|
||
private static IWebHost BuildWebHost(string[] args) | ||
private static IHostBuilder BuildWebHost(string[] args) | ||
{ | ||
var webHostBuilder = WebHost.CreateDefaultBuilder(args); | ||
|
||
var contentRoot = webHostBuilder.GetSetting("contentRoot"); | ||
var environment = webHostBuilder.GetSetting("ENVIRONMENT"); | ||
|
||
var isDevelopment = EnvironmentName.Development.Equals(environment); | ||
|
||
var configurationBuilder = new ConfigurationBuilder() | ||
.SetBasePath(contentRoot) | ||
.AddJsonFile("appsettings.json", false, false) | ||
.AddEnvironmentVariables(); | ||
|
||
if (isDevelopment) | ||
{ | ||
configurationBuilder.AddUserSecrets<Program>(); | ||
} | ||
|
||
var configuration = configurationBuilder.Build(); | ||
|
||
var serilogLevel = configuration.GetLoggingLevel("MinimumLevel:Default"); | ||
|
||
var loggerConfiguration = new LoggerConfiguration() | ||
.Destructure.ByTransforming<MessageAttributeValue>(a => new {a.DataType, a.StringValue}) | ||
.Enrich.WithDemystifiedStackTraces() | ||
.ReadFrom.Configuration(configuration); | ||
|
||
if (isDevelopment) | ||
{ | ||
loggerConfiguration = loggerConfiguration.WriteTo.Console(serilogLevel); | ||
} | ||
|
||
var logger = loggerConfiguration.CreateLogger(); | ||
|
||
try | ||
{ | ||
logger.Information("Starting Host..."); | ||
|
||
SetAwsEnvironmentVariables(configuration, logger); | ||
|
||
return webHostBuilder | ||
.UseStartup<Startup>() | ||
.UseConfiguration(configuration) | ||
.UseSerilog(logger, true) | ||
.Build(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.Fatal(ex, "Host terminated unexpectedly"); | ||
throw; | ||
} | ||
} | ||
|
||
private static void SetAwsEnvironmentVariables(IConfigurationRoot configuration, Logger logger) | ||
{ | ||
const string awsAccessKeyIdVariableName = "AWS_ACCESS_KEY_ID"; | ||
const string awsSecretAccessKeyVariableName = "AWS_SECRET_ACCESS_KEY"; | ||
|
||
var accessKeyIdFromEnvironmentVariable = Environment.GetEnvironmentVariable(awsAccessKeyIdVariableName); | ||
var secretAccessKeyFromEnvironmentVariable = Environment.GetEnvironmentVariable(awsSecretAccessKeyVariableName); | ||
|
||
if (!string.IsNullOrWhiteSpace(accessKeyIdFromEnvironmentVariable) && | ||
!string.IsNullOrWhiteSpace(secretAccessKeyFromEnvironmentVariable)) | ||
{ | ||
logger.Information($"'{awsAccessKeyIdVariableName}' and '{awsSecretAccessKeyVariableName}' set via environment variables"); | ||
|
||
return; | ||
} | ||
|
||
var accessKeyIdFromConfiguration = configuration.GetValue<string>(awsAccessKeyIdVariableName); | ||
var secretAccessKeyFromConfiguration = configuration.GetValue<string>(awsSecretAccessKeyVariableName); | ||
|
||
if (!string.IsNullOrWhiteSpace(accessKeyIdFromConfiguration) && | ||
!string.IsNullOrWhiteSpace(secretAccessKeyFromConfiguration)) | ||
{ | ||
logger.Information($"'{awsAccessKeyIdVariableName}' and '{awsSecretAccessKeyVariableName}' present in configuration, setting matching environment variables"); | ||
|
||
Environment.SetEnvironmentVariable( | ||
awsAccessKeyIdVariableName, | ||
accessKeyIdFromConfiguration, | ||
EnvironmentVariableTarget.Process); | ||
|
||
Environment.SetEnvironmentVariable( | ||
awsSecretAccessKeyVariableName, | ||
secretAccessKeyFromConfiguration, | ||
EnvironmentVariableTarget.Process); | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException($"'{awsAccessKeyIdVariableName}' and '{awsSecretAccessKeyVariableName}' should either be set as environment variables or configuration, please refer to the README: https://github.com/gabrielweyer/simple-routing/blob/master/README.md#configuration."); | ||
} | ||
} | ||
} | ||
|
||
internal static class ConfigurationRootExtensions | ||
{ | ||
internal static LogEventLevel GetLoggingLevel(this IConfigurationRoot configuration, string keyName, | ||
LogEventLevel defaultLevel = LogEventLevel.Warning) | ||
{ | ||
try | ||
{ | ||
return configuration.GetValue($"Serilog:{keyName}", LogEventLevel.Warning); | ||
} | ||
catch (Exception) | ||
{ | ||
return defaultLevel; | ||
} | ||
return Host | ||
.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.