-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #527 from PeterKneale/feature/476-Add_sample_appli…
…cation Add sample application
- Loading branch information
Showing
21 changed files
with
657 additions
and
0 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
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
28 changes: 28 additions & 0 deletions
28
samples/JustSaying.Sample.Restaurant.KitchenConsole/ConfigurationExtensions.cs
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,28 @@ | ||
using System; | ||
using Amazon; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace JustSaying.Sample.Restaurant.KitchenConsole | ||
{ | ||
public static class ConfigurationExtensions | ||
{ | ||
private const string AWSServiceUrlKey = "AWSServiceUrl"; | ||
|
||
private const string AWSRegionKey = "AWSRegion"; | ||
|
||
public static bool HasAWSServiceUrl(this IConfiguration configuration) | ||
{ | ||
return !string.IsNullOrWhiteSpace(configuration[AWSServiceUrlKey]); | ||
} | ||
|
||
public static Uri GetAWSServiceUri(this IConfiguration configuration) | ||
{ | ||
return new Uri(configuration[AWSServiceUrlKey]); | ||
} | ||
|
||
public static RegionEndpoint GetAWSRegion(this IConfiguration configuration) | ||
{ | ||
return RegionEndpoint.GetBySystemName(configuration[AWSRegionKey]); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...aying.Sample.Restaurant.KitchenConsole/JustSaying.Sample.Restaurant.KitchenConsole.csproj
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,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
<NoWarn>$(NoWarn);CA1031;CA2007</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="appsettings.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="appsettings.json" CopyToPublishDirectory="PreserveNewest" CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\JustSaying.Extensions.DependencyInjection.Microsoft\JustSaying.Extensions.DependencyInjection.Microsoft.csproj" /> | ||
<ProjectReference Include="..\..\JustSaying\JustSaying.csproj" /> | ||
<ProjectReference Include="..\JustSaying.Sample.Restaurant.Models\JustSaying.Sample.Restaurant.Models.csproj" /> | ||
</ItemGroup> | ||
</Project> |
59 changes: 59 additions & 0 deletions
59
samples/JustSaying.Sample.Restaurant.KitchenConsole/OrderPlacedEventHandler.cs
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,59 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using JustSaying.Messaging; | ||
using JustSaying.Messaging.MessageHandling; | ||
using JustSaying.Sample.Restaurant.Models; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace JustSaying.Sample.Restaurant.KitchenConsole | ||
{ | ||
public class OrderPlacedEventHandler : IHandlerAsync<OrderPlacedEvent> | ||
{ | ||
private readonly IMessagePublisher _publisher; | ||
private readonly ILogger<OrderPlacedEventHandler> _logger; | ||
|
||
/// <summary> | ||
/// Handles messages of type OrderPlacedEvent | ||
/// Takes a dependency on IMessagePublisher so that further messages can be published | ||
/// </summary> | ||
public OrderPlacedEventHandler(IMessagePublisher publisher, ILogger<OrderPlacedEventHandler> log) | ||
{ | ||
_publisher = publisher; | ||
_logger = log; | ||
} | ||
|
||
public async Task<bool> Handle(OrderPlacedEvent message) | ||
{ | ||
// Returning true would indicate: | ||
// The message was handled successfully | ||
// The message can be removed from the queue. | ||
// Returning false would indicate: | ||
// The message was not handled successfully | ||
// The message handling should be retried (configured by default) | ||
// The message should be moved to the error queue if all retries fail | ||
|
||
try | ||
{ | ||
_logger.LogInformation("Order {orderId} for {description} received", message.OrderId, message.Description); | ||
|
||
// This is where you would actually handle the order placement | ||
// Intentionally left empty for the sake of this being a sample application | ||
|
||
_logger.LogInformation("Order {orderId} ready", message.OrderId); | ||
|
||
var orderReadyEvent = new OrderReadyEvent | ||
{ | ||
OrderId = message.OrderId | ||
}; | ||
|
||
await _publisher.PublishAsync(orderReadyEvent).ConfigureAwait(false); | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Failed to handle message for {orderId}", message.OrderId); | ||
return false; | ||
} | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
samples/JustSaying.Sample.Restaurant.KitchenConsole/Program.cs
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,83 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using JustSaying.Sample.Restaurant.Models; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace JustSaying.Sample.Restaurant.KitchenConsole | ||
{ | ||
internal class Program | ||
{ | ||
public static async Task Main() | ||
{ | ||
Console.Title = "KitchenConsole"; | ||
|
||
await new HostBuilder() | ||
.ConfigureAppConfiguration((hostContext, config) => | ||
{ | ||
config.AddJsonFile("appsettings.json", optional: false); | ||
config.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true); | ||
config.AddEnvironmentVariables(); | ||
}) | ||
.ConfigureLogging(loggingBuilder => loggingBuilder.AddConsole()) | ||
.ConfigureServices((hostContext, services) => | ||
{ | ||
var configuration = hostContext.Configuration; | ||
services.AddJustSaying(config => | ||
{ | ||
config.Client(x => | ||
{ | ||
if (configuration.HasAWSServiceUrl()) | ||
{ | ||
// The AWS client SDK allows specifying a custom HTTP endpoint. | ||
// For testing purposes it is useful to specify a value that | ||
// points to a docker image such as `p4tin/goaws` or `localstack/localstack` | ||
x.WithServiceUri(configuration.GetAWSServiceUri()) | ||
.WithAnonymousCredentials(); | ||
} | ||
else | ||
{ | ||
// The real AWS environment will require some means of authentication | ||
//x.WithBasicCredentials("###", "###"); | ||
//x.WithSessionCredentials("###", "###", "###"); | ||
} | ||
}); | ||
|
||
config.Messaging(x => | ||
{ | ||
// Configures which AWS Region to operate in | ||
x.WithRegion(configuration.GetAWSRegion()); | ||
}); | ||
|
||
config.Subscriptions(x => | ||
{ | ||
// Creates the following if they do not already exist | ||
// - a SQS queue of name `orderplacedevent` | ||
// - a SQS queue of name `orderplacedevent_error` | ||
// - a SNS topic of name `orderplacedevent` | ||
// - a SNS topic subscription on topic 'orderplacedevent' and queue 'orderplacedevent' | ||
x.ForTopic<OrderPlacedEvent>(); | ||
}); | ||
|
||
config.Publications(x => | ||
{ | ||
// Creates the following if they do not already exist | ||
// - a SNS topic of name `orderreadyevent` | ||
x.WithTopic<OrderReadyEvent>(); | ||
}); | ||
}); | ||
|
||
// Added a message handler for message type for 'OrderPlacedEvent' on topic 'orderplacedevent' and queue 'orderplacedevent' | ||
services.AddJustSayingHandler<OrderPlacedEvent, OrderPlacedEventHandler>(); | ||
|
||
// Add a background service that is listening for messages related to the above subscriptions | ||
services.AddHostedService<Subscriber>(); | ||
}) | ||
.UseConsoleLifetime() | ||
.Build() | ||
.RunAsync(); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
samples/JustSaying.Sample.Restaurant.KitchenConsole/Subscriber.cs
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,32 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace JustSaying.Sample.Restaurant.KitchenConsole | ||
{ | ||
/// <summary> | ||
/// A background service responsible for starting the bus which listens for | ||
/// messages on the configured queues | ||
/// </summary> | ||
public class Subscriber : BackgroundService | ||
{ | ||
private readonly IMessagingBus _bus; | ||
private readonly ILogger<Subscriber> _logger; | ||
|
||
public Subscriber(IMessagingBus bus, ILogger<Subscriber> logger) | ||
{ | ||
_bus = bus; | ||
_logger = logger; | ||
} | ||
|
||
protected override Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
_logger.LogInformation("Kitchen subscriber running"); | ||
|
||
_bus.Start(stoppingToken); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
samples/JustSaying.Sample.Restaurant.KitchenConsole/appsettings.json
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,11 @@ | ||
{ | ||
"AWSRegion": "eu-west-1", | ||
"AWSServiceUrl": "http://localhost:4100", | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"System": "Warning", | ||
"Microsoft": "Warning" | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
samples/JustSaying.Sample.Restaurant.Models/JustSaying.Sample.Restaurant.Models.csproj
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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\JustSaying.Models\JustSaying.Models.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
11 changes: 11 additions & 0 deletions
11
samples/JustSaying.Sample.Restaurant.Models/OrderPlacedEvent.cs
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,11 @@ | ||
using JustSaying.Models; | ||
|
||
namespace JustSaying.Sample.Restaurant.Models | ||
{ | ||
public class OrderPlacedEvent : Message | ||
{ | ||
public int OrderId { get; set; } | ||
|
||
public string Description { get; set; } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
samples/JustSaying.Sample.Restaurant.Models/OrderReadyEvent.cs
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,9 @@ | ||
using JustSaying.Models; | ||
|
||
namespace JustSaying.Sample.Restaurant.Models | ||
{ | ||
public class OrderReadyEvent : Message | ||
{ | ||
public int OrderId { get; set; } | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
samples/JustSaying.Sample.Restaurant.OrderingApi/ConfigurationExtensions.cs
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,28 @@ | ||
using System; | ||
using Amazon; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace JustSaying.Sample.Restaurant.OrderingApi | ||
{ | ||
public static class ConfigurationExtensions | ||
{ | ||
private const string AWSServiceUrlKey = "AWSServiceUrl"; | ||
|
||
private const string AWSRegionKey = "AWSRegion"; | ||
|
||
public static bool HasAWSServiceUrl(this IConfiguration configuration) | ||
{ | ||
return !string.IsNullOrWhiteSpace(configuration[AWSServiceUrlKey]); | ||
} | ||
|
||
public static Uri GetAWSServiceUri(this IConfiguration configuration) | ||
{ | ||
return new Uri(configuration[AWSServiceUrlKey]); | ||
} | ||
|
||
public static RegionEndpoint GetAWSRegion(this IConfiguration configuration) | ||
{ | ||
return RegionEndpoint.GetBySystemName(configuration[AWSRegionKey]); | ||
} | ||
} | ||
} |
Oops, something went wrong.