commit e9c0a10ac12a85d6cfe026483a240df7f2062982 Author: Mateusz Skoczek Date: Fri Jan 16 00:34:49 2026 +0100 initial commit diff --git a/.github/config/gitversion.yml b/.github/config/gitversion.yml new file mode 100644 index 0000000..57caa9e --- /dev/null +++ b/.github/config/gitversion.yml @@ -0,0 +1,9 @@ +next-version: 1.0.0 +mode: ContinuousDeployment +assembly-versioning-scheme: MajorMinorPatch +assembly-file-versioning-scheme: MajorMinorPatch + +branches: + main: + regex: ^main$ + increment: Patch \ No newline at end of file diff --git a/.github/workflows/pull_request_dev.yml b/.github/workflows/pull_request_dev.yml new file mode 100644 index 0000000..ba8559c --- /dev/null +++ b/.github/workflows/pull_request_dev.yml @@ -0,0 +1,24 @@ +name: Build NuGet package on dev branch pull request + +on: + pull_request: + branches: + - "dev" + paths: + - "TimetableDesigner.Backend.Events.Providers.RabbitMQ**" + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 10.0.x + - name: Add source + run: dotnet nuget add source --username TimetableDesigner --password ${{secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/TimetableDesigner/index.json" + - name: Build + run: dotnet build diff --git a/.github/workflows/pull_request_main.yml b/.github/workflows/pull_request_main.yml new file mode 100644 index 0000000..b0715ae --- /dev/null +++ b/.github/workflows/pull_request_main.yml @@ -0,0 +1,24 @@ +name: Build NuGet package on main branch pull request + +on: + pull_request: + branches: + - "main" + paths: + - "TimetableDesigner.Backend.Events.Providers.RabbitMQ**" + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 10.0.x + - name: Add source + run: dotnet nuget add source --username TimetableDesigner --password ${{secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/TimetableDesigner/index.json" + - name: Build + run: dotnet build diff --git a/.github/workflows/push_dev.yml b/.github/workflows/push_dev.yml new file mode 100644 index 0000000..0584be8 --- /dev/null +++ b/.github/workflows/push_dev.yml @@ -0,0 +1,24 @@ +name: Build NuGet package on dev branch push + +on: + push: + branches: + - "dev" + paths: + - "TimetableDesigner.Backend.Events.Providers.RabbitMQ**" + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 10.0.x + - name: Add source + run: dotnet nuget add source --username TimetableDesigner --password ${{secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/TimetableDesigner/index.json" + - name: Build + run: dotnet build diff --git a/.github/workflows/push_main.yml b/.github/workflows/push_main.yml new file mode 100644 index 0000000..b916d36 --- /dev/null +++ b/.github/workflows/push_main.yml @@ -0,0 +1,74 @@ +name: Build NuGet package on main branch push + +on: + push: + branches: + - "main" + paths: + - "TimetableDesigner.Backend.Events.Providers.RabbitMQ**" + +jobs: + build: + name: Build and pack + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + with: + fetch-depth: 0 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 10.0.x + - name: Add source + run: dotnet nuget add source --username TimetableDesigner --password ${{secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/TimetableDesigner/index.json" + - name: Build + run: dotnet build + - name: Setup GitVersion + uses: gittools/actions/gitversion/setup@v4.2.0 + with: + versionSpec: 6.4.x + - name: Determine version + uses: gittools/actions/gitversion/execute@v4.2.0 + id: gitversion + with: + configFilePath: ./.github/config/gitversion.yml + - name: Create NuGet package + run: dotnet pack -c Release -p:Version=${{steps.gitversion.outputs.SemVer}} -p:PackageVersion=${{steps.gitversion.outputs.SemVer}} --output ${{ github.workspace}}/nuget + - name: Upload artifact + uses: actions/upload-artifact@v6 + with: + name: nuget + path: ${{ github.workspace}}/nuget/*.nupkg + outputs: + version: ${{ steps.gitversion.outputs.SemVer }} + publish: + name: Publish + permissions: write-all + runs-on: ubuntu-latest + needs: build + steps: + - name: Download artifact + uses: actions/download-artifact@v6 + with: + name: nuget + path: ${{ github.workspace}}/nuget + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 10.0.x + - name: Add source + run: dotnet nuget add source --username TimetableDesigner --password ${{secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/TimetableDesigner/index.json" + - name: Publish + run: dotnet nuget push ${{ github.workspace}}/nuget/*.nupkg --source "github" --api-key ${{ secrets.NUGET_REGISTRY_API_KEY }} --skip-duplicate + - name: Create Release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{needs.build.outputs.version}} + release_name: ${{needs.build.outputs.version}} + draft: false + prerelease: false + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5d7fe6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,289 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ +**/Properties/launchSettings.json + +# VS Code +.vscode/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Typescript v1 declaration files +typings/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs diff --git a/TimetableDesigner.Backend.Events.Providers.RabbitMQ.slnx b/TimetableDesigner.Backend.Events.Providers.RabbitMQ.slnx new file mode 100644 index 0000000..e3891da --- /dev/null +++ b/TimetableDesigner.Backend.Events.Providers.RabbitMQ.slnx @@ -0,0 +1,3 @@ + + + diff --git a/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueue.cs b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueue.cs new file mode 100644 index 0000000..a86fb17 --- /dev/null +++ b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueue.cs @@ -0,0 +1,31 @@ +using Microsoft.Extensions.DependencyInjection; +using RabbitMQ.Client; + +namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ; + +public class RabbitMQEventQueue : EventQueue +{ + public string Hostname { get; set; } = null!; + public int Port { get; set; } + public string Username { get; set; } = null!; + public string Password { get; set; } = null!; + public string ExchangeName { get; set; } = null!; + public string QueuePrefix { get; set; } = null!; + + public override void Setup(IServiceCollection services) + { + ConnectionFactory factory = new ConnectionFactory + { + HostName = Hostname, + Port = Port, + UserName = Username, + Password = Password, + }; + + Task createConnectionTask = factory.CreateConnectionAsync(); + createConnectionTask.Wait(); + services.AddSingleton(createConnectionTask.Result); + services.AddSingleton(sp => new RabbitMQEventQueuePublisher(sp.GetRequiredService(), ExchangeName)); + services.AddSingleton(sp => new RabbitMQEventQueueSubscriber(sp.GetRequiredService(), ExchangeName, QueuePrefix)); + } +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueuePublisher.cs b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueuePublisher.cs new file mode 100644 index 0000000..d3e693a --- /dev/null +++ b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueuePublisher.cs @@ -0,0 +1,34 @@ +using System.Text.Json; +using RabbitMQ.Client; + +namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ; + +public class RabbitMQEventQueuePublisher : IEventQueuePublisher +{ + private readonly IConnection _connection; + private readonly string _exchangeName; + + public RabbitMQEventQueuePublisher(IConnection connection, string exchangeName) + { + _connection = connection; + _exchangeName = exchangeName; + } + + public async Task PublishAsync(T eventData) where T : class + { + string routingKey = typeof(T).FullName!; + BasicProperties properties = new BasicProperties + { + ContentType = "application/json", + DeliveryMode = DeliveryModes.Persistent, + Type = typeof(T).FullName, + + }; + ReadOnlyMemory body = JsonSerializer.SerializeToUtf8Bytes(eventData); + await using (IChannel channel = await _connection.CreateChannelAsync()) + { + await channel.ExchangeDeclareAsync(_exchangeName, ExchangeType.Direct); + await channel.BasicPublishAsync(_exchangeName, routingKey, true, properties, body); + } + } +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueueSubscriber.cs b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueueSubscriber.cs new file mode 100644 index 0000000..fb29b88 --- /dev/null +++ b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueueSubscriber.cs @@ -0,0 +1,55 @@ +using System.Text.Json; +using RabbitMQ.Client; +using RabbitMQ.Client.Events; + +namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ; + +public class RabbitMQEventQueueSubscriber : IEventQueueSubscriber +{ + private readonly IConnection _connection; + private readonly string _exchangeName; + private readonly string _queuePrefix; + + public RabbitMQEventQueueSubscriber(IConnection connection, string exchangeName, string queuePrefix) + { + _connection = connection; + _exchangeName = exchangeName; + _queuePrefix = queuePrefix; + } + + public void Subscribe(Func handler) where T : class + { + Task createChannelTask = _connection.CreateChannelAsync(); + createChannelTask.Wait(); + IChannel channel = createChannelTask.Result; + + string queueName = $"{_queuePrefix}-{typeof(T).FullName!}"; + Task exchangeDeclareTask = channel.ExchangeDeclareAsync(_exchangeName, ExchangeType.Direct); + Task queueDeclareTask = channel.QueueDeclareAsync(queueName, true, false, false); + Task.WaitAll(exchangeDeclareTask, queueDeclareTask); + channel.QueueBindAsync(queueName, _exchangeName, typeof(T).FullName!).Wait(); + + AsyncEventingBasicConsumer consumer = new AsyncEventingBasicConsumer(channel); + consumer.ReceivedAsync += async (_, args) => + { + try + { + T? eventData = JsonSerializer.Deserialize(args.Body.Span); + if (eventData is null) + { + throw new InvalidOperationException("Received null event"); + } + + await handler(eventData); + + await channel.BasicAckAsync(args.DeliveryTag, false); + } + catch + { + await channel.BasicNackAsync(args.DeliveryTag, false, false); + } + }; + + channel.BasicConsumeAsync(queueName, false, consumer).Wait(); + } +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Events.Providers.RabbitMQ/TimetableDesigner.Backend.Events.Providers.RabbitMQ.csproj b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/TimetableDesigner.Backend.Events.Providers.RabbitMQ.csproj new file mode 100644 index 0000000..8af1270 --- /dev/null +++ b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/TimetableDesigner.Backend.Events.Providers.RabbitMQ.csproj @@ -0,0 +1,14 @@ + + + + net10.0 + enable + enable + + + + + + + + diff --git a/TimetableDesigner.Backend.Events.Providers.RabbitMQ/icon.png b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/icon.png new file mode 100644 index 0000000..98f0c54 Binary files /dev/null and b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/icon.png differ