Skip to content

Commit

Permalink
Configured Nginx as a reverse proxy and load balancer for Docker Comp…
Browse files Browse the repository at this point in the history
…ose setup in Helpdesk sample

Cofigured it fully with Swagger and other Kestrel quirks
  • Loading branch information
oskardudycz committed May 24, 2024
1 parent ad4b01c commit 91de5b3
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Sample/Helpdesk/Helpdesk.Api/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
**/obj/
**/out/
**/TestResults/
**/Internal/Generated
**/Internal/
Dockerfile
9 changes: 6 additions & 3 deletions Sample/Helpdesk/Helpdesk.Api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ RUN dotnet restore ./${project_name}.csproj
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
RUN if [ "${run_codegen}" = true ] ; then dotnet run -- codegen write & dotnet run -- codegen test; else echo "skipping code generation"; fi

RUN ls
RUN ls -R Internal

# Build project with Release configuration
# and no restore, as we did it already
RUN dotnet build -c Release --no-restore ./${project_name}.csproj

## Test project with Release configuration
## and no build, as we did it already
#RUN dotnet test -c Release --no-build ./Sample/Tickets/Tickets.Api/Tickets.Api.csproj
# RUN dotnet test -c Release --no-build ./Sample/Tickets/Tickets.Api/Tickets.Api.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

########################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public CustomerIncidentsSummaryProjection()

public void Apply(IncidentLogged logged, CustomerIncidentsSummary current)
{
current.Id = logged.CustomerId;
current.Pending++;
}

Expand Down
23 changes: 16 additions & 7 deletions Sample/Helpdesk/Helpdesk.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Marten.Events.Projections;
using Marten.Pagination;
using Marten.Schema.Identity;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Oakton;
Expand All @@ -27,7 +28,7 @@

var builder = WebApplication.CreateBuilder(args);


builder.WebHost.UseUrls("http://*:5248");
builder.Services
.AddEndpointsApiExplorer()
.AddSwaggerGen()
Expand All @@ -53,15 +54,25 @@
options.Projections.Add<IncidentShortInfoProjection>(ProjectionLifecycle.Inline);
options.Projections.Add<CustomerIncidentsSummaryProjection>(ProjectionLifecycle.Async);
options.ApplicationAssembly = typeof(CustomerIncidentsSummaryProjection).Assembly;
return options;
})
.AddSubscriptionWithServices<KafkaProducer>(ServiceLifetime.Singleton)
.AddSubscriptionWithServices<SignalRProducer<IncidentsHub>>(ServiceLifetime.Singleton)
.OptimizeArtifactWorkflow(TypeLoadMode.Static)
.ApplyAllDatabaseChangesOnStartup()
.UseLightweightSessions()
.AddAsyncDaemon(DaemonMode.Solo);
.AddAsyncDaemon(DaemonMode.HotCold);


// Header forwarding to enable Swagger in Nginx
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});

builder.Services
.AddCors(options =>
options.AddPolicy("ClientPermission", policy =>
Expand Down Expand Up @@ -235,11 +246,9 @@ CancellationToken ct
querySession.Json.WriteById<CustomerIncidentsSummary>(customerId, context)
);

if (app.Environment.IsDevelopment())
{
app.UseSwagger()
.UseSwaggerUI();
}
app.UseSwagger()
.UseSwaggerUI()
.UseForwardedHeaders(); // Header forwarding to enable Swagger in Nginx


app.UseCors("ClientPermission");
Expand Down
40 changes: 40 additions & 0 deletions Sample/Helpdesk/Helpdesk.Api/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: "3.8"
services:
nginx:
restart: always
image: nginx:alpine
ports:
- 8089:80
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
# - ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- backend

backend:
build:
dockerfile: Dockerfile
context: .
args:
project_name: Helpdesk.Api
run_codegen: true
deploy:
replicas: 3
depends_on:
postgres:
condition: service_healthy
restart: always

postgres:
image: postgres:15.1-alpine
container_name: postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
environment:
- POSTGRES_DB=postgres
- POSTGRES_PASSWORD=Password12!
ports:
- "5432:5432"
26 changes: 26 additions & 0 deletions Sample/Helpdesk/Helpdesk.Api/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
worker_processes auto;

events {
worker_connections 1024;
}

http {
map $http_connection $connection_upgrade {
"~*Upgrade" $http_connection;
default keep-alive;
}

server {
listen 80;
location / {
proxy_pass http://backend:5248/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}

0 comments on commit 91de5b3

Please sign in to comment.