Skip to content

Commit

Permalink
use recommended build for AOT & add docker-compose project
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBout committed Nov 25, 2024
1 parent ae61bd1 commit 2195811
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 28 deletions.
6 changes: 6 additions & 0 deletions SimpleCDN.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCDN", "SimpleCDN\Simp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCDN.Tests", "SimpleCDN.Tests\SimpleCDN.Tests.csproj", "{6D8833B9-7EC8-4212-83DD-ABCCD677309E}"
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 All @@ -20,6 +22,10 @@ Global
{6D8833B9-7EC8-4212-83DD-ABCCD677309E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6D8833B9-7EC8-4212-83DD-ABCCD677309E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6D8833B9-7EC8-4212-83DD-ABCCD677309E}.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
29 changes: 22 additions & 7 deletions SimpleCDN/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 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.

# These ARGs allow for swapping out the base used to make the final image when debugging from VS
ARG LAUNCHING_FROM_VS
# This sets the base image for final, but only if LAUNCHING_FROM_VS has been defined
ARG FINAL_BASE_IMAGE=${LAUNCHING_FROM_VS:+aotdebug}

# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER $APP_UID
Expand All @@ -9,27 +14,37 @@ EXPOSE 8080

# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
# Install clang/zlib1g-dev dependencies for publishing to native
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
clang zlib1g-dev
ARG BUILD_CONFIGURATION=Release
ARG DOTNET_BUILD_PLATFORM=linux-x64
ARG BUILDPLATFORM
ARG TARGETPLATFORM
WORKDIR /src
COPY ["SimpleCDN/SimpleCDN.csproj", "SimpleCDN/"]
RUN dotnet restore "./SimpleCDN/SimpleCDN.csproj"
COPY . .
WORKDIR "/src/SimpleCDN"

RUN apt update && apt install -y clang zlib1g-dev binutils-aarch64-linux-gnu
RUN dotnet build "./SimpleCDN.csproj" -c $BUILD_CONFIGURATION -r $DOTNET_BUILD_PLATFORM -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
ARG DOTNET_BUILD_PLATFORM=linux-x64
RUN dotnet publish "./SimpleCDN.csproj" -c $BUILD_CONFIGURATION -r $DOTNET_BUILD_PLATFORM -o /app/publish /p:UseAppHost=false
RUN dotnet publish "./SimpleCDN.csproj" -c $BUILD_CONFIGURATION -r $DOTNET_BUILD_PLATFORM -o /app/publish /p:UseAppHost=true

# This stage is used as the base for the final stage when launching from VS to support debugging in regular mode (Default when not using the Debug configuration)
FROM base AS aotdebug
USER root
# Install GDB to support native debugging
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
gdb
USER app

# 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
FROM ${FINAL_BASE_IMAGE:-mcr.microsoft.com/dotnet/runtime-deps:9.0} AS final
WORKDIR /app
EXPOSE 8080
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SimpleCDN.dll"]
ENTRYPOINT ["./SimpleCDN"]
19 changes: 0 additions & 19 deletions SimpleCDN/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,6 @@ namespace SimpleCDN
{
public static class Extensions
{
public static WebApplication MapCDNEndpoints(this WebApplication builder)
{
builder.MapGet("/{*route}", (CDNLoader loader, string route = "") =>
{
if (loader.GetFile(route) is CDNFile file)
{
return Results.File(file.Content, file.MediaType, lastModified: file.LastModified);
}

return Results.NotFound();
}).CacheOutput(policy =>
{
policy.Cache()
.Expire(TimeSpan.FromMinutes(1));
});

return builder;
}

static readonly string[] sizeNames = ["", "k", "M", "G", "T"];

public static string FormatByteCount(this long number)
Expand Down
24 changes: 22 additions & 2 deletions SimpleCDN/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@

var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.AddOptions<CDNConfiguration>();
builder.Services.AddOptions<CDNConfiguration>()
.Configure<IConfiguration>((settings, configuration) =>
{
if (configuration["CDN_DATA_ROOT"] != null)
{
settings.DataRoot = configuration["CDN_DATA_ROOT"];
}
})
.BindConfiguration("CDN");

builder.Services.AddSingleton<CDNLoader>();

builder.Services.AddMemoryCache();

var app = builder.Build();

app.MapCDNEndpoints();
app.MapGet("/{*route}", (CDNLoader loader, string route = "") =>
{
if (loader.GetFile(route) is CDNFile file)
{
return Results.File(file.Content, file.MediaType, lastModified: file.LastModified);
}

return Results.NotFound();
}).CacheOutput(policy =>
{
policy.Cache()
.Expire(TimeSpan.FromMinutes(1));
});

app.Run();
1 change: 1 addition & 0 deletions SimpleCDN/SimpleCDN.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<PublishAot>true</PublishAot>
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup>

<ItemGroup>
Expand Down
19 changes: 19 additions & 0 deletions docker-compose.dcproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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>
<DockerPublishLocally>False</DockerPublishLocally>
<ProjectGuid>81dded9d-158b-e303-5f62-77a2896d2a5a</ProjectGuid>
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
<DockerServiceUrl>{Scheme}://localhost:{ServicePort}/weatherforecast</DockerServiceUrl>
<DockerServiceName>simplecdn</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:
simplecdn:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_HTTP_PORTS=8080
ports:
- "8080"
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
simplecdn:
image: ${DOCKER_REGISTRY-}simplecdn
build:
context: .
dockerfile: SimpleCDN/Dockerfile
11 changes: 11 additions & 0 deletions launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"Docker Compose": {
"commandName": "DockerCompose",
"commandVersion": "1.0",
"serviceActions": {
"simplecdn": "StartDebugging"
}
}
}
}

0 comments on commit 2195811

Please sign in to comment.