Skip to content

Commit

Permalink
Moved SignalR projection ourside of the Helpdesk API
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Jul 30, 2023
1 parent 3997c8f commit 1db967a
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 36 deletions.
62 changes: 62 additions & 0 deletions Sample/Helpdesk.ScaledUpProjections/Dockerfile.SignalR
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
ARG dotnet_version=7.0
########################################
# First stage of multistage build
########################################
# Use Build image with label `builder
########################################
FROM mcr.microsoft.com/dotnet/sdk:${dotnet_version}-alpine AS builder
# Project file name, mandatory parameter, e.g. Helpdesk.SignalR
ARG run_codegen=true

# Setup working directory for project
WORKDIR /app

COPY ./Helpdesk/Helpdesk.csproj ./Helpdesk/Helpdesk.csproj
COPY ./Helpdesk.SignalR/Helpdesk.SignalR.csproj ./Helpdesk.SignalR/Helpdesk.SignalR.csproj

# Restore nuget packages
RUN dotnet restore ./Helpdesk.SignalR/Helpdesk.SignalR.csproj

# Copy project files
COPY ./ ./

# Run code generation depending on the build argument
RUN if [ "run_codegen" = true ] ; then dotnet run -- codegen write & dotnet run -- codegen test; else echo "skipping code generation"; fi

# Build project with Release configuration
# and no restore, as we did it already
RUN dotnet build -c Release --no-restore ./Helpdesk.SignalR/Helpdesk.SignalR.csproj

## Test project with Release configuration
## and no build, as we did it already
#RUN dotnet test -c Release --no-build ./Sample/Tickets/Tickets.SignalR/Tickets.SignalR.csproj

# Publish project to output folder
# and no build, as we did it already
WORKDIR /app/
RUN ls
RUN dotnet publish -c Release --no-build -o out ./Helpdesk.SignalR/Helpdesk.SignalR.csproj

########################################
# Second stage of multistage build
########################################
# Use other build image as the final one
# that won't have source codes
########################################
FROM mcr.microsoft.com/dotnet/aspnet:${dotnet_version}-alpine
ARG dotnet_version=7.0

# Setup working directory for project
WORKDIR /app/

# Copy published in previous stage binaries
# from the `builder` image
COPY --from=builder /app/Helpdesk.SignalR/out .

# Set URL that App will be exposed
ENV ASPNETCORE_URLS="http://*:5000"
ENV PROJECT_DLL="Helpdesk.SignalR.dll"

# sets entry point command to automatically
# run application on `docker run`
ENTRYPOINT dotnet $PROJECT_DLL
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@

<ItemGroup>
<PackageReference Include="Marten.CommandLine" Version="6.0.4" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Marten.AspNetCore" Version="6.0.4" />
<PackageReference Include="Marten" Version="6.0.4" />
<PackageReference Include="Confluent.Kafka" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
31 changes: 3 additions & 28 deletions Sample/Helpdesk.ScaledUpProjections/Helpdesk.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Helpdesk.Api.Core.Http;
using Helpdesk.Api.Core.Kafka;
using Helpdesk.Api.Core.Marten;
using Helpdesk.Api.Core.SignalR;
using Helpdesk.Incidents;
using Helpdesk.Incidents.GetCustomerIncidentsSummary;
using Helpdesk.Incidents.GetIncidentDetails;
Expand All @@ -17,7 +16,6 @@
using Marten.Schema.Identity;
using Marten.Services.Json;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Oakton;
using Weasel.Core;
using static Microsoft.AspNetCore.Http.TypedResults;
Expand All @@ -28,14 +26,11 @@

var builder = WebApplication.CreateBuilder(args);


builder.Services
.AddEndpointsApiExplorer()
.AddSwaggerGen()
.AddMarten(sp =>
.AddMarten(options =>
{
var options = new StoreOptions();
var schemaName = Environment.GetEnvironmentVariable("SchemaName") ?? "Helpdesk";
options.Events.DatabaseSchemaName = schemaName;
options.DatabaseSchemaName = schemaName;
Expand All @@ -54,30 +49,15 @@
options.Projections.Add<IncidentShortInfoProjection>(ProjectionLifecycle.Inline);
options.Projections.Add<CustomerIncidentsSummaryProjection>(ProjectionLifecycle.Async);
options.Projections.Add(new KafkaProducer(builder.Configuration), ProjectionLifecycle.Async);
options.Projections.Add(
new SignalRProducer((IHubContext)sp.GetRequiredService<IHubContext<IncidentsHub>>()),
ProjectionLifecycle.Async
);
return options;
})
.OptimizeArtifactWorkflow(TypeLoadMode.Static)
.UseLightweightSessions()
.AddAsyncDaemon(DaemonMode.Solo);
.AddAsyncDaemon(DaemonMode.HotCold);

builder.Services
.AddCors(options =>
options.AddPolicy("ClientPermission", policy =>
policy.WithOrigins("http://localhost:3000")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
)
)
.Configure<JsonOptions>(o => o.SerializerOptions.Converters.Add(new JsonStringEnumConverter()))
.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(o =>
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()))
.AddSignalR();
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));

builder.Host.ApplyOaktonExtensions();

Expand Down Expand Up @@ -246,14 +226,9 @@ CancellationToken ct


app.UseCors("ClientPermission");
app.MapHub<IncidentsHub>("/hubs/incidents");

return await app.RunOaktonCommands(args);

public class IncidentsHub: Hub
{
}

public record LogIncidentRequest(
Contact Contact,
string Description
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
**/bin/
**/obj/
**/out/
**/TestResults/
**/Internal/Generated
Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Marten.CommandLine" Version="6.0.4" />
<PackageReference Include="Marten" Version="6.0.4" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Helpdesk\Helpdesk.csproj" />
</ItemGroup>
</Project>
72 changes: 72 additions & 0 deletions Sample/Helpdesk.ScaledUpProjections/Helpdesk.SignalR/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Text.Json.Serialization;
using Helpdesk.Api.Core.SignalR;
using JasperFx.CodeGeneration;
using Marten;
using Marten.Events.Daemon.Resiliency;
using Marten.Events.Projections;
using Marten.Services.Json;
using Microsoft.AspNetCore.SignalR;
using Oakton;
using Weasel.Core;
using JsonOptions = Microsoft.AspNetCore.Http.Json.JsonOptions;

var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddMarten(sp =>
{
var options = new StoreOptions();
var schemaName = Environment.GetEnvironmentVariable("SchemaName") ?? "Helpdesk";
options.Events.DatabaseSchemaName = schemaName;
options.DatabaseSchemaName = schemaName;
options.Connection(builder.Configuration.GetConnectionString("Incidents") ??
throw new InvalidOperationException());
options.UseDefaultSerialization(
EnumStorage.AsString,
nonPublicMembersStorage: NonPublicMembersStorage.All,
serializerType: SerializerType.SystemTextJson
);
options.Projections.Add(
new SignalRProducer((IHubContext)sp.GetRequiredService<IHubContext<IncidentsHub>>()),
ProjectionLifecycle.Async
);
return options;
})
.OptimizeArtifactWorkflow(TypeLoadMode.Static)
.UseLightweightSessions()
.AddAsyncDaemon(DaemonMode.HotCold);

builder.Services
.AddCors(options =>
options.AddPolicy("ClientPermission", policy =>
policy.WithOrigins("http://localhost:3000")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
)
)
.Configure<JsonOptions>(o => o.SerializerOptions.Converters.Add(new JsonStringEnumConverter()))
.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(o =>
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()))
.AddSignalR();

builder.Host.ApplyOaktonExtensions();

var app = builder.Build();

app.UseCors("ClientPermission");
app.MapHub<IncidentsHub>("/hubs/incidents");

return await app.RunOaktonCommands(args);

public class IncidentsHub: Hub
{
}

public partial class Program
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"profiles": {
"Helpdesk.Api": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger/index.html",
"applicationUrl": "http://localhost:5248",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Incidents": "PORT = 5432; HOST = postgres; TIMEOUT = 15; POOLING = True; MINPOOLSIZE = 1; MAXPOOLSIZE = 100; COMMANDTIMEOUT = 20; DATABASE = 'postgres'; PASSWORD = 'Password12!'; USER ID = 'postgres'"
},
"KafkaProducer": {
"ProducerConfig": {
"BootstrapServers": "kafka:9092"
},
"Topic": "Incidents"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Incidents": "PORT = 5432; HOST = localhost; TIMEOUT = 15; POOLING = True; MINPOOLSIZE = 1; MAXPOOLSIZE = 100; COMMANDTIMEOUT = 20; DATABASE = 'postgres'; PASSWORD = 'Password12!'; USER ID = 'postgres'"
},
"KafkaProducer": {
"ProducerConfig": {
"BootstrapServers": "localhost:9092"
},
"Topic": "Incidents"
}
}
6 changes: 6 additions & 0 deletions Sample/Helpdesk.ScaledUpProjections/Helpdesk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Helpdesk.Api.Tests", "Helpd
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Helpdesk", "Helpdesk\Helpdesk.csproj", "{C2996855-AD6D-45F3-BFB9-71D3D4A1C56C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Helpdesk.SignalR", "Helpdesk.SignalR\Helpdesk.SignalR.csproj", "{FA50346F-88F5-47B9-8047-3A668C4826CF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -35,6 +37,10 @@ Global
{C2996855-AD6D-45F3-BFB9-71D3D4A1C56C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2996855-AD6D-45F3-BFB9-71D3D4A1C56C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2996855-AD6D-45F3-BFB9-71D3D4A1C56C}.Release|Any CPU.Build.0 = Release|Any CPU
{FA50346F-88F5-47B9-8047-3A668C4826CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA50346F-88F5-47B9-8047-3A668C4826CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA50346F-88F5-47B9-8047-3A668C4826CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA50346F-88F5-47B9-8047-3A668C4826CF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
EndGlobalSection
Expand Down
3 changes: 0 additions & 3 deletions Sample/Helpdesk.ScaledUpProjections/Helpdesk/Helpdesk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
</PropertyGroup>

<ItemGroup>
<!-- <PackageReference Include="Marten.AspNetCore" Version="6.0.4" />-->
<PackageReference Include="Marten" Version="6.0.4" />
<!-- <PackageReference Include="Confluent.Kafka" Version="2.2.0" />-->
<!-- <PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />-->
</ItemGroup>

</Project>

0 comments on commit 1db967a

Please sign in to comment.