Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**
6 changes: 6 additions & 0 deletions DomainDrivenTutorial.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EShoppingTutorial.Core.Appl
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EShoppingTutorial.Infrastructure", "EShoppingTutorial\EShoppingTutorial.Infrastructure\EShoppingTutorial.Infrastructure.csproj", "{F31C106C-CAA0-48B8-8046-8C2A6D394874}"
EndProject
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{81DDED9D-158B-E303-5F62-77A2896D2A5A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -67,6 +69,10 @@ Global
{F31C106C-CAA0-48B8-8046-8C2A6D394874}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F31C106C-CAA0-48B8-8046-8C2A6D394874}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F31C106C-CAA0-48B8-8046-8C2A6D394874}.Release|Any CPU.Build.0 = Release|Any CPU
{81DDED9D-158B-E303-5F62-77A2896D2A5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{81DDED9D-158B-E303-5F62-77A2896D2A5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{81DDED9D-158B-E303-5F62-77A2896D2A5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{81DDED9D-158B-E303-5F62-77A2896D2A5A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
36 changes: 36 additions & 0 deletions EShoppingTutorialWebAPI/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080


# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["EShoppingTutorialWebAPI/EShoppingTutorialWebAPI.csproj", "EShoppingTutorialWebAPI/"]
COPY ["EShoppingTutorial/EShoppingTutorial.Core.Application/EShoppingTutorial.Core.Application.csproj", "EShoppingTutorial/EShoppingTutorial.Core.Application/"]
COPY ["EShoppingTutorial/EShoppingTutorial.Core/EShoppingTutorial.Core.Domain.csproj", "EShoppingTutorial/EShoppingTutorial.Core/"]
COPY ["Framework.Core/GenericRepository/GenericRepository.Abstractions.csproj", "Framework.Core/GenericRepository/"]
COPY ["Framework.Core/SharedKernel/SharedKernel.csproj", "Framework.Core/SharedKernel/"]
COPY ["EShoppingTutorial/EShoppingTutorial.Core.Persistence/EShoppingTutorial.Core.Persistence.csproj", "EShoppingTutorial/EShoppingTutorial.Core.Persistence/"]
COPY ["Framework.Core/GenericRepositoryEntityFramework/GenericRepository.EntityFramework.csproj", "Framework.Core/GenericRepositoryEntityFramework/"]
COPY ["EShoppingTutorial/EShoppingTutorial.Infrastructure/EShoppingTutorial.Infrastructure.csproj", "EShoppingTutorial/EShoppingTutorial.Infrastructure/"]
RUN dotnet restore "./EShoppingTutorialWebAPI/EShoppingTutorialWebAPI.csproj"
COPY . .
WORKDIR "/src/EShoppingTutorialWebAPI"
RUN dotnet build "./EShoppingTutorialWebAPI.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./EShoppingTutorialWebAPI.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "EShoppingTutorialWebAPI.dll"]
5 changes: 4 additions & 1 deletion EShoppingTutorialWebAPI/EShoppingTutorialWebAPI.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<PackageId>$(AssemblyName)</PackageId>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,6 +15,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.23.0" />
<PackageReference Include="NSwag.AspNetCore" Version="14.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="10.1.0" />
Expand Down
32 changes: 31 additions & 1 deletion EShoppingTutorialWebAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ public static void Main(string[] args)
var builder = WebApplication.CreateBuilder(args);

// Add Database Connection
builder.Services.AddDbContext<EShoppingTutorialDbContext>(opts => opts.UseSqlServer(builder.Configuration["ConnectionStrings:EShoppingTutorialDB"]));
// Use GetConnectionString which is safer for environment variable overrides
var connectionString = builder.Configuration.GetConnectionString("EShoppingTutorialDB");

if (string.IsNullOrEmpty(connectionString))
{
// LOG it instead of THROWING it
Console.WriteLine("CRITICAL ERROR: Connection string 'EShoppingTutorialDB' was not found!");
}

builder.Services.AddDbContext<EShoppingTutorialDbContext>(opts => opts.UseSqlServer(connectionString));

// Add Core Application (Assembly Scanning)
builder.Services.AddCoreApplicationConfigs();
Expand Down Expand Up @@ -47,6 +56,27 @@ public static void Main(string[] args)
app.MapControllers();
app.UseStaticFiles();
app.UseRouting();

// ---- Database Migration Logic Necessary for Docker/Kubernetes----
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<EShoppingTutorialDbContext>();
// This is the "Magic" line: It creates the DB and tables if they don't exist
// and applies any pending migrations.
context.Database.Migrate();
Console.WriteLine("Database migration completed successfully.");
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while migrating the database.");
}
}
// ----------------------------------

app.Run();
}
}
36 changes: 23 additions & 13 deletions EShoppingTutorialWebAPI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60087",
"sslPort": 0
}
},
{
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
Expand All @@ -19,10 +10,29 @@
"EShoppingTutorialWebAPI": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"applicationUrl": "http://localhost:5000"
},
"Container (Dockerfile)": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"environmentVariables": {
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true,
"useSSL": false
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60087",
"sslPort": 0
}
}
}
}
4 changes: 4 additions & 0 deletions EShoppingTutorialWebAPI/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"ConnectionStrings": {
// Use this connection string if you are using Windows Authentication
"EShoppingTutorialDB": "Data Source=.;Initial Catalog=EShoppingTutorialDB;Integrated Security=True;Pooling=False;TrustServerCertificate=True"

// Use this connection string if you are using SQL Server Authentication
//"EShoppingTutorialDB": "Server=db;Database=EShoppingTutorialDB;User Id=sa;Password=YourStrong@Passw0rd;TrustServerCertificate=True"
}
}
18 changes: 18 additions & 0 deletions docker-compose.dcproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
<PropertyGroup Label="Globals">
<ProjectVersion>2.1</ProjectVersion>
<DockerTargetOS>Linux</DockerTargetOS>
<ProjectGuid>81dded9d-158b-e303-5f62-77a2896d2a5a</ProjectGuid>
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
<DockerServiceUrl>{Scheme}://localhost:{ServicePort}</DockerServiceUrl>
<DockerServiceName>eshoppingtutorialwebapi</DockerServiceName>
</PropertyGroup>
<ItemGroup>
<None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.yml" />
<None Include=".dockerignore" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
eshoppingtutorialwebapi:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_HTTP_PORTS=8080
ports:
- "8080"
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3.4'

services:
eshoppingtutorialwebapi:
image: ${DOCKER_REGISTRY-}eshoppingtutorialwebapi
build:
context: .
dockerfile: EShoppingTutorialWebAPI/Dockerfile
depends_on:
- db
# SQL Server for Linux
db:
image: mcr.microsoft.com/mssql/server:2022-latest
environment:
- ACCEPT_EULA=Y
- MSSQL_SA_PASSWORD=YourStrong@Passw0rd
ports:
- "1433:1433"
128 changes: 128 additions & 0 deletions full-stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# --- THE API SERVICE (Load Balancer) ---
apiVersion: v1
kind: Service
metadata:
name: eshopping-api-service
spec:
type: LoadBalancer
selector:
app: eshopping-api
ports:
- protocol: TCP
port: 8081 # Port you use in the browser
targetPort: 8080 # Port inside the .NET container
---
# --- THE INGRESS (Domain Router) ---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: eshopping-ingress
spec:
ingressClassName: nginx
rules:
- host: eshopping.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: eshopping-api-service
port:
number: 8081
---
# --- THE DATABASE SERVICE ---
apiVersion: v1
kind: Service
metadata:
name: sql-service
spec:
selector:
app: mssql
ports:
- protocol: TCP
port: 1433
targetPort: 1433
---
# --- THE DATABASE (StatefulSet with Persistent Storage) ---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mssql
spec:
serviceName: "sql-service"
replicas: 1
selector:
matchLabels:
app: mssql
template:
metadata:
labels:
app: mssql
spec:
containers:
- name: mssql
image: mcr.microsoft.com/mssql/server:2022-latest
ports:
- containerPort: 1433
env:
- name: ACCEPT_EULA
value: "Y"
- name: MSSQL_SA_PASSWORD
value: "YourStrong@Passw0rd"
volumeMounts:
- name: mssql-storage
mountPath: /var/opt/mssql # SQL Server's internal data directory
volumeClaimTemplates:
- metadata:
name: mssql-storage
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi # This creates a permanent disk on your host
---
# --- THE API DEPLOYMENT (3 Replicas) ---
apiVersion: apps/v1
kind: Deployment
metadata:
name: eshopping-api
spec:
replicas: 3
selector:
matchLabels:
app: eshopping-api
template:
metadata:
labels:
app: eshopping-api
spec:
initContainers:
- name: wait-for-sql
image: busybox:latest
command: ['sh', '-c', "until nc -z sql-service 1433; do echo waiting for sql; sleep 2; done;"]
containers:
- name: api
image: eshopping-api:v1 # Uses the tag we successfully built
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
env:
- name: ASPNETCORE_ENVIRONMENT
value: "Development"
- name: ASPNETCORE_URLS
value: "http://0.0.0.0:8080"
- name: ConnectionStrings__EShoppingTutorialDB
value: "Server=sql-service;Database=EShoppingTutorialDB;User Id=sa;Password=YourStrong@Passw0rd;TrustServerCertificate=True"
livenessProbe:
httpGet:
path: /index.html
port: 8080
initialDelaySeconds: 40
periodSeconds: 15
readinessProbe:
httpGet:
path: /index.html
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
Loading
Loading