Skip to content

Commit

Permalink
feat: add API versioning to Dockerfile and Swagger configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
italopessoa committed Nov 20, 2024
1 parent b705fc8 commit bd51a0a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ jobs:
uses: docker/build-push-action@v5
with:
context: .
build-args: |
API_VERSION=${{ steps.sanitize-version.outputs.version }}
file: ./src/FIAP.TechChallenge.ByteMeBurger.Api/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
Expand Down
3 changes: 2 additions & 1 deletion src/FIAP.TechChallenge.ByteMeBurger.Api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ RUN dotnet restore src/FIAP.TechChallenge.ByteMeBurger.Api

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "src/FIAP.TechChallenge.ByteMeBurger.Api/FIAP.TechChallenge.ByteMeBurger.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
ARG API_VERSION
RUN dotnet publish "src/FIAP.TechChallenge.ByteMeBurger.Api/FIAP.TechChallenge.ByteMeBurger.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false /p:Version=$API_VERSION


FROM base AS final
Expand Down
11 changes: 8 additions & 3 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ public static void Main(string[] args)
// https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-8.0#log-automatic-400-responses
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddRouting(options => options.LowercaseUrls = true);
var version = Assembly.GetExecutingAssembly().GetName().Version;
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
c.SwaggerDoc($"v{version.Major}", new OpenApiInfo
{
Title = "Tech Challenge Restaurant API", Version = "v1", Extensions =
Title = "Tech Challenge Restaurant API", Version = $"v{version.Major}.{version.Minor}.{version.Build}", Extensions =
{
{
"x-logo",
Expand Down Expand Up @@ -107,7 +108,11 @@ public static void Main(string[] args)
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerUI(options =>
{
var version = Assembly.GetExecutingAssembly().GetName().Version.Major;
options.SwaggerEndpoint($"/swagger/v{version}/swagger.yaml", $"v{version}");
});
}

app.UseSerilogRequestLogging();
Expand Down

0 comments on commit bd51a0a

Please sign in to comment.