-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathProgram.cs
122 lines (110 loc) · 4.83 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using JustSaying.Sample.Restaurant.KitchenConsole;
using JustSaying.Sample.Restaurant.KitchenConsole.Handlers;
using JustSaying.Sample.Restaurant.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
const string appName = "KitchenConsole";
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Debug()
.Enrich.WithProperty("AppName", appName)
.CreateLogger();
Console.Title = appName;
try
{
await Run();
}
catch (Exception e)
{
Log.Fatal(e, "Error occurred during startup: {Message}", e.Message);
}
finally
{
Log.CloseAndFlush();
}
static async Task Run()
{
await new HostBuilder()
.ConfigureAppConfiguration((hostContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: false);
config.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
config.AddEnvironmentVariables();
})
.UseSerilog()
.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 `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 =>
{
x.WithSubscriptionGroup("GroupA",
c => c.WithPrefetch(10)
.WithMultiplexerCapacity(10)
.WithConcurrencyLimit(5));
// 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' with two tags
// - "IsOrderEvent" with no value
// - "Subscriber" with the value "KitchenConsole"
// - a SNS topic subscription on topic 'orderonitswayevent' and queue 'orderonitswayevent'
x.ForTopic<OrderPlacedEvent>(cfg =>
cfg.WithTag("IsOrderEvent")
.WithTag("Subscriber", appName)
.WithReadConfiguration(rc =>
rc.WithSubscriptionGroup("GroupA")));
x.ForTopic<OrderOnItsWayEvent>(cfg =>
cfg.WithReadConfiguration(rc =>
rc.WithSubscriptionGroup("GroupB")));
});
config.Publications(x =>
{
// Creates the following if they do not already exist
// - an SNS topic of name `orderreadyevent` with two tags:
// - "IsOrderEvent" with no value
// - "Publisher" with the value "KitchenConsole"
x.WithTopic<OrderReadyEvent>(cfg =>
{
cfg.WithTag("IsOrderEvent")
.WithTag("Publisher", appName);
});
x.WithTopic<OrderDeliveredEvent>();
});
});
// Added a message handler for message type for 'OrderPlacedEvent' on topic 'orderplacedevent' and queue 'orderplacedevent'
services.AddJustSayingHandler<OrderPlacedEvent, OrderPlacedEventHandler>();
services.AddJustSayingHandler<OrderOnItsWayEvent, OrderOnItsWayEventHandler>();
// Add a background service that is listening for messages related to the above subscriptions
services.AddHostedService<Subscriber>();
})
.UseConsoleLifetime()
.Build()
.RunAsync();
}