-
Notifications
You must be signed in to change notification settings - Fork 15
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 #1 from jacqueskang/amazon-sns
Amazon sns
- Loading branch information
Showing
36 changed files
with
514 additions
and
180 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
15 changes: 15 additions & 0 deletions
15
src/JKang.EventBus.Abstractions/DependencyInjection/IEventBusBuilder.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,15 @@ | ||
using JKang.EventBus; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public interface IEventBusBuilder | ||
{ | ||
IServiceCollection Services { get; } | ||
|
||
IEventBusBuilder UseSerializer<TEventSerializer>() | ||
where TEventSerializer : class, IEventSerializer; | ||
|
||
IEventBusBuilder AddEventPublisher<TEventPublisher>() | ||
where TEventPublisher : class, IEventPublisher; | ||
} | ||
} |
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 @@ | ||
namespace JKang.EventBus | ||
{ | ||
public interface IEventBusSubscriber | ||
{ | ||
IEventBusSubscriber Subscribe<TEvent, TEventHandler>() | ||
where TEvent: class | ||
where TEventHandler: class, IEventHandler<TEvent>; | ||
|
||
IEventBusSubscriber SubscribeAllHandledEvents<TEventHandler>() | ||
where TEventHandler : class; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/JKang.EventBus.Abstractions/IEventPublisherProvider.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,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace JKang.EventBus | ||
{ | ||
public interface IEventPublisherProvider | ||
{ | ||
IEnumerable<IEventPublisher> GetEventPublishers(); | ||
} | ||
} |
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,7 @@ | ||
namespace JKang.EventBus | ||
{ | ||
public interface IEventSerializer | ||
{ | ||
string Serialize<TEvent>(TEvent @event); | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/JKang.EventBus.AmazonSns/AmazonSnsEventBusBuilderExtensions.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,20 @@ | ||
using JKang.EventBus; | ||
using JKang.EventBus.AmazonSns; | ||
using System; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public static class AmazonSnsEventBusBuilderExtensions | ||
{ | ||
public static IEventBusBuilder PublishToAmazonSns(this IEventBusBuilder builder, | ||
Action<AmazonSnsEventPublisherOptions> setupActions) | ||
{ | ||
builder.Services | ||
.Configure(setupActions) | ||
; | ||
|
||
return builder | ||
.AddEventPublisher<AmazonSnsEventPublisher>(); | ||
} | ||
} | ||
} |
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,46 @@ | ||
using Amazon; | ||
using Amazon.SimpleNotificationService; | ||
using Amazon.SimpleNotificationService.Model; | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace JKang.EventBus.AmazonSns | ||
{ | ||
public class AmazonSnsEventPublisher : IEventPublisher | ||
{ | ||
private readonly IEventSerializer _eventSerializer; | ||
private readonly IOptions<AmazonSnsEventPublisherOptions> _options; | ||
|
||
public AmazonSnsEventPublisher( | ||
IEventSerializer eventSerializer, | ||
IOptions<AmazonSnsEventPublisherOptions> options) | ||
{ | ||
_eventSerializer = eventSerializer; | ||
_options = options; | ||
} | ||
|
||
public async Task PublishEventAsync<TEvent>(TEvent @event) | ||
{ | ||
var endpoint = RegionEndpoint.GetBySystemName(_options.Value.Region); | ||
var client = new AmazonSimpleNotificationServiceClient(endpoint); | ||
|
||
Type eventType = typeof(TEvent); | ||
AmazonSnsTopicAttribute attr = eventType | ||
.GetCustomAttributes(typeof(AmazonSnsTopicAttribute), inherit: false) | ||
.OfType<AmazonSnsTopicAttribute>() | ||
.FirstOrDefault(); | ||
string topicName = attr == null | ||
? eventType.FullName.ToLower().Replace('.', '-') | ||
: attr.Name; | ||
var createTopicRequest = new CreateTopicRequest(topicName); | ||
CreateTopicResponse createTopicResponse = await client.CreateTopicAsync(createTopicRequest); | ||
string topicArn = createTopicResponse.TopicArn; | ||
|
||
string message = _eventSerializer.Serialize(@event); | ||
var publishRequest = new PublishRequest(topicArn, message); | ||
PublishResponse publishResponse = await client.PublishAsync(publishRequest); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/JKang.EventBus.AmazonSns/AmazonSnsEventPublisherOptions.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,7 @@ | ||
namespace JKang.EventBus.AmazonSns | ||
{ | ||
public class AmazonSnsEventPublisherOptions | ||
{ | ||
public string Region { get; set; } = "eu-west-1"; | ||
} | ||
} |
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,15 @@ | ||
using System; | ||
|
||
namespace JKang.EventBus.AmazonSns | ||
{ | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] | ||
public class AmazonSnsTopicAttribute: Attribute | ||
{ | ||
public AmazonSnsTopicAttribute(string name) | ||
{ | ||
Name = name; | ||
} | ||
|
||
public string Name { get; } | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/JKang.EventBus.AmazonSns/JKang.EventBus.AmazonSns.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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AWSSDK.SimpleNotificationService" Version="3.3.3.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\JKang.EventBus.Abstractions\JKang.EventBus.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
22 changes: 18 additions & 4 deletions
22
src/JKang.EventBus.Core/DependencyInjection/EventBusBuilder.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
Oops, something went wrong.