Skip to content

Commit

Permalink
Add a bridge service
Browse files Browse the repository at this point in the history
Fixes #131

Signed-off-by: jan.jansen <[email protected]>
  • Loading branch information
farodin91 committed Mar 16, 2021
1 parent 3d29e51 commit d818c9d
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 1 deletion.
47 changes: 47 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ jobs:
run: dotnet test --no-restore Motor.NET.sln
- name: Pack
run: dotnet pack -v minimal -c Release --no-restore -o ./artifacts Motor.NET.sln
- name: Publish Bridge
run: dotnet publish -v minimal -c Release --no-restore -o ./artifacts-bridge ./src/Motor.Extensions.Hosting.Bridge/Motor.Extensions.Hosting.Bridge.csproj
- name: Upload Artifact
uses: actions/upload-artifact@v2
with:
name: artifacts
path: ./artifacts/*.nupkg
- name: Upload Artifact Bridge
uses: actions/upload-artifact@v2
with:
name: artifacts-bridge
path: ./artifacts-bridge/*

deploy:
needs: build
Expand All @@ -64,3 +71,43 @@ jobs:
dotnet-version: 5.0.x
- name: Push to NuGet Feed
run: dotnet nuget push './artifacts/*.nupkg' --skip-duplicate --source $NUGET_FEED --api-key $NUGET_KEY

build-bridge:
needs: build
runs-on: ubuntu-latest
steps:
- name: Download Artifact
uses: actions/download-artifact@v1
with:
name: artifacts-bridge
- name: Prepare
id: prep
run: |
DOCKER_IMAGE=docker.pkg.github.com/gdatasoftwareag/motornet/bridge
VERSION=edge
if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}
elif [[ $GITHUB_REF == refs/heads/* ]]; then
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
elif [[ $GITHUB_REF == refs/pull/* ]]; then
VERSION=pr-${{ github.event.number }}
fi
TAGS="${DOCKER_IMAGE}:${VERSION},${DOCKER_IMAGE}:${VERSION}-${GITHUB_SHA::8}"
echo ::set-output name=version::${VERSION}
echo ::set-output name=tags::${TAGS}
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
- name: Login to Github Registry
uses: docker/login-action@v1
with:
registry: docker.pkg.github.com
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push to GitHub Packages
uses: docker/build-push-action@v2
with:
push: github.event_name == 'release'
context: ./artifacts-bridge
tags: ${{ steps.prep.outputs.tags }}
labels: |
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
15 changes: 15 additions & 0 deletions Motor.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Motor.Extensions.Hosting.SQ
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeSQS", "examples\ConsumeSQS\ConsumeSQS.csproj", "{76AF0280-E7F6-4853-A69F-71194F07EA9E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Motor.Extensions.Hosting.Bridge", "src\Motor.Extensions.Hosting.Bridge\Motor.Extensions.Hosting.Bridge.csproj", "{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -629,6 +631,18 @@ Global
{76AF0280-E7F6-4853-A69F-71194F07EA9E}.Release|x64.Build.0 = Release|Any CPU
{76AF0280-E7F6-4853-A69F-71194F07EA9E}.Release|x86.ActiveCfg = Release|Any CPU
{76AF0280-E7F6-4853-A69F-71194F07EA9E}.Release|x86.Build.0 = Release|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Debug|x64.ActiveCfg = Debug|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Debug|x64.Build.0 = Debug|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Debug|x86.ActiveCfg = Debug|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Debug|x86.Build.0 = Debug|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Release|Any CPU.Build.0 = Release|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Release|x64.ActiveCfg = Release|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Release|x64.Build.0 = Release|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Release|x86.ActiveCfg = Release|Any CPU
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -677,6 +691,7 @@ Global
{7A316DFB-5ECF-463E-88E7-8D3B7B5DE793} = {749B1421-3177-4C7A-A66B-541BD4E925B0}
{3FF292B7-E167-4687-8813-FCBC05B139B9} = {ADD2EBBA-A839-4E4A-9253-CDE29A372F07}
{76AF0280-E7F6-4853-A69F-71194F07EA9E} = {3DC7D216-6908-4759-B86F-759FDAE393D9}
{62234AB3-F8B0-41A0-B3AE-988D7A26F91C} = {749B1421-3177-4C7A-A66B-541BD4E925B0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5E91C34C-3AEC-4084-BA02-753C9236AA34}
Expand Down
2 changes: 1 addition & 1 deletion shared.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Version>0.6.4</Version>
<Version>0.6.5</Version>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8625;CS8618;CS8604;CS8601</WarningsAsErrors>
Expand Down
9 changes: 9 additions & 0 deletions src/Motor.Extensions.Hosting.Bridge/BridgeConsumerType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Motor.Extensions.Hosting.Bridge
{
public enum BridgeConsumerType
{
RabbitMQ,
Kafka,
SQS
}
}
8 changes: 8 additions & 0 deletions src/Motor.Extensions.Hosting.Bridge/BridgePublisherType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Motor.Extensions.Hosting.Bridge
{
public enum BridgePublisherType
{
RabbitMQ,
Kafka
}
}
16 changes: 16 additions & 0 deletions src/Motor.Extensions.Hosting.Bridge/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM mcr.microsoft.com/dotnet/aspnet:5.0-alpine
COPY . /data
WORKDIR /data

ARG DOTNET_ENVIRONMENT
ENV DOTNET_ENVIRONMENT=$DOTNET_ENVIRONMENT

HEALTHCHECK CMD wget --quiet --tries=1 --spider http://localhost:9110/health || exit 1

ENTRYPOINT dotnet /data/Motor.Extensions.Hosting.Bridge.dll

LABEL org.opencontainers.image.title="Motor.NET Bridge Docker Image" \
org.opencontainers.image.description="Motor.NET" \
org.opencontainers.image.url="https://github.com/GDATASoftwareAG/motornet" \
org.opencontainers.image.source="https://github.com/GDATASoftwareAG/motornet/tree/master/src/Motor.Extensions.Hosting.Bridge/" \
org.opencontainers.image.license="MIT"
4 changes: 4 additions & 0 deletions src/Motor.Extensions.Hosting.Bridge/Internals/ByteData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Motor.Extensions.Hosting.Bridge.Internals
{
public record ByteData(byte[] data);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Motor.Extensions.Conversion.Abstractions;

namespace Motor.Extensions.Hosting.Bridge.Internals
{
public class ByteDataConversions : IMessageDeserializer<ByteData>, IMessageSerializer<ByteData>
{
public ByteData Deserialize(byte[] message)
{
if (message is null || message.Length == 0)
{
throw new ArgumentNullException(nameof(message));
}
return new ByteData(message);
}

public byte[] Serialize(ByteData message)
{
return message.data;
}
}
}
25 changes: 25 additions & 0 deletions src/Motor.Extensions.Hosting.Bridge/Internals/PassThoughService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Threading;
using System.Threading.Tasks;
using Motor.Extensions.Diagnostics.Metrics.Abstractions;
using Motor.Extensions.Hosting.Abstractions;
using Prometheus.Client;

namespace Motor.Extensions.Hosting.Bridge.Internals
{
public class PassThoughService : ISingleOutputService<ByteData, ByteData>
{
private readonly ISummary summary;

public PassThoughService(IMetricsFactory<PassThoughService> metricsFactory)
{
summary = metricsFactory.CreateSummary("message_pass_tough_bytes", "");
}

public Task<MotorCloudEvent<ByteData>?> ConvertMessageAsync(MotorCloudEvent<ByteData> dataCloudEvent,
CancellationToken token = default)
{
summary.Observe(dataCloudEvent.TypedData.data.Length);
return Task.FromResult<MotorCloudEvent<ByteData>?>(dataCloudEvent.CreateNew(dataCloudEvent.TypedData));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Motor.Extensions.Conversion.Abstractions\Motor.Extensions.Conversion.Abstractions.csproj" />
<ProjectReference Include="..\Motor.Extensions.Hosting.Kafka\Motor.Extensions.Hosting.Kafka.csproj" />
<ProjectReference Include="..\Motor.Extensions.Hosting.RabbitMQ\Motor.Extensions.Hosting.RabbitMQ.csproj" />
<ProjectReference Include="..\Motor.Extensions.Hosting.SQS\Motor.Extensions.Hosting.SQS.csproj" />
<ProjectReference Include="..\Motor.Extensions.Utilities\Motor.Extensions.Utilities.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Dockerfile">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<Import Project="$(MSBuildThisFileDirectory)../../shared.csproj" />

</Project>
60 changes: 60 additions & 0 deletions src/Motor.Extensions.Hosting.Bridge/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Motor.Extensions.Hosting.Abstractions;
using Motor.Extensions.Hosting.Bridge;
using Motor.Extensions.Hosting.Bridge.Internals;
using Motor.Extensions.Hosting.Consumer;
using Motor.Extensions.Hosting.Kafka;
using Motor.Extensions.Hosting.Publisher;
using Motor.Extensions.Hosting.RabbitMQ;
using Motor.Extensions.Hosting.SQS;
using Motor.Extensions.Utilities;


await MotorHost.CreateDefaultBuilder()
.ConfigureSingleOutputService<ByteData, ByteData>()
.ConfigureServices((_, collection) =>
{
collection.AddTransient<ISingleOutputService<ByteData, ByteData>, PassThoughService>();
})
.ConfigurePublisher<ByteData>((ctx, builder) =>
{
var s = ctx.Configuration["PublisherType"];
var publisherType = Enum.Parse<BridgePublisherType>(s, true);
switch (publisherType)
{
case BridgePublisherType.RabbitMQ:
builder.AddRabbitMQ("Publisher");
break;
case BridgePublisherType.Kafka:
builder.AddKafka("Publisher");
break;
default:
throw new ArgumentOutOfRangeException();
}

builder.AddSerializer<ByteDataConversions>();
})
.ConfigureConsumer<ByteData>((ctx, builder) =>
{
var s = ctx.Configuration["ConsumerType"];
var consumerType = Enum.Parse<BridgeConsumerType>(s, true);
switch (consumerType)
{
case BridgeConsumerType.RabbitMQ:
builder.AddRabbitMQ("Consumer");
break;
case BridgeConsumerType.Kafka:
builder.AddKafka("Consumer");
break;
case BridgeConsumerType.SQS:
builder.AddSQS("Consumer");
break;
default:
throw new ArgumentOutOfRangeException();
}

builder.AddDeserializer<ByteDataConversions>();
})
.RunConsoleAsync();
6 changes: 6 additions & 0 deletions src/Motor.Extensions.Hosting.Bridge/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ConsumerType": "RabbitMQ",
"Consumer": {},
"PublisherType": "RabbitMQ",
"Publisher": {}
}

0 comments on commit d818c9d

Please sign in to comment.