diff --git a/SimpleCDN.sln b/SimpleCDN.sln index a808cd1..7684e89 100644 --- a/SimpleCDN.sln +++ b/SimpleCDN.sln @@ -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 @@ -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 diff --git a/SimpleCDN/Dockerfile b/SimpleCDN/Dockerfile index c0beb6f..ded5e47 100644 --- a/SimpleCDN/Dockerfile +++ b/SimpleCDN/Dockerfile @@ -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 @@ -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"] \ No newline at end of file +ENTRYPOINT ["./SimpleCDN"] \ No newline at end of file diff --git a/SimpleCDN/Extensions.cs b/SimpleCDN/Extensions.cs index 6a31e0d..383f85e 100644 --- a/SimpleCDN/Extensions.cs +++ b/SimpleCDN/Extensions.cs @@ -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) diff --git a/SimpleCDN/Program.cs b/SimpleCDN/Program.cs index 2b70e2a..1789d56 100644 --- a/SimpleCDN/Program.cs +++ b/SimpleCDN/Program.cs @@ -2,7 +2,15 @@ var builder = WebApplication.CreateSlimBuilder(args); -builder.Services.AddOptions(); +builder.Services.AddOptions() + .Configure((settings, configuration) => + { + if (configuration["CDN_DATA_ROOT"] != null) + { + settings.DataRoot = configuration["CDN_DATA_ROOT"]; + } + }) + .BindConfiguration("CDN"); builder.Services.AddSingleton(); @@ -10,6 +18,18 @@ 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(); diff --git a/SimpleCDN/SimpleCDN.csproj b/SimpleCDN/SimpleCDN.csproj index 0d1ba94..97eae2d 100644 --- a/SimpleCDN/SimpleCDN.csproj +++ b/SimpleCDN/SimpleCDN.csproj @@ -6,6 +6,7 @@ enable Linux true + ..\docker-compose.dcproj diff --git a/docker-compose.dcproj b/docker-compose.dcproj new file mode 100644 index 0000000..d162663 --- /dev/null +++ b/docker-compose.dcproj @@ -0,0 +1,19 @@ + + + + 2.1 + Linux + False + 81dded9d-158b-e303-5f62-77a2896d2a5a + LaunchBrowser + {Scheme}://localhost:{ServicePort}/weatherforecast + simplecdn + + + + docker-compose.yml + + + + + \ No newline at end of file diff --git a/docker-compose.override.yml b/docker-compose.override.yml new file mode 100644 index 0000000..b13b3bb --- /dev/null +++ b/docker-compose.override.yml @@ -0,0 +1,7 @@ +services: + simplecdn: + environment: + - ASPNETCORE_ENVIRONMENT=Development + - ASPNETCORE_HTTP_PORTS=8080 + ports: + - "8080" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9d161f1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +services: + simplecdn: + image: ${DOCKER_REGISTRY-}simplecdn + build: + context: . + dockerfile: SimpleCDN/Dockerfile diff --git a/launchSettings.json b/launchSettings.json new file mode 100644 index 0000000..00d9350 --- /dev/null +++ b/launchSettings.json @@ -0,0 +1,11 @@ +{ + "profiles": { + "Docker Compose": { + "commandName": "DockerCompose", + "commandVersion": "1.0", + "serviceActions": { + "simplecdn": "StartDebugging" + } + } + } +} \ No newline at end of file