-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #620 from caioavidal/feature/webapi-improvements
feat: webapi improvements
- Loading branch information
Showing
14 changed files
with
240 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
ARG ASPNET_VERSION="9.0" | ||
ARG SDK_VERSION="9.0" | ||
ARG BASE_ADRESS="mcr.microsoft.com/dotnet" | ||
# Use an official .NET 9 SDK as a parent image | ||
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build | ||
|
||
FROM $BASE_ADRESS/aspnet:$ASPNET_VERSION AS base | ||
# Set the working directory to /app | ||
WORKDIR /app | ||
|
||
EXPOSE 80 | ||
# Copy the project files to the container | ||
COPY src/. . | ||
|
||
FROM $BASE_ADRESS/sdk:$SDK_VERSION AS build | ||
# Restore dependencies | ||
RUN dotnet restore WebAPI/NeoServer.Web.API.csproj | ||
|
||
WORKDIR /src | ||
# Build the project | ||
RUN dotnet publish WebAPI/NeoServer.Web.API.csproj -c Release -o out | ||
|
||
COPY *.csproj ./WebAPI | ||
# Use an official .NET 9 runtime as a parent image | ||
FROM mcr.microsoft.com/dotnet/aspnet:9.0 | ||
|
||
RUN dotnet restore -v m ./WebAPI | ||
|
||
WORKDIR /src/WebAPI | ||
RUN dotnet build NeoServer.Web.API.csproj -c Release -o /app/build --no-restore | ||
# Set the working directory to /app | ||
WORKDIR /app | ||
|
||
FROM build AS publish | ||
RUN dotnet publish NeoServer.Web.API.csproj -c Release -o /app/publish --no-restore | ||
# Copy the published app to the container | ||
COPY --from=build /app/out . | ||
|
||
FROM base AS final | ||
WORKDIR /app | ||
COPY --from=publish /app/publish . | ||
# Set the entrypoint to run the console app | ||
ENTRYPOINT ["dotnet", "NeoServer.Web.API.dll"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> | ||
<UserSecretsId>c71a823c-0b62-4db1-9942-ad2bf4c080b0</UserSecretsId> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> | ||
<UserSecretsId>c71a823c-0b62-4db1-9942-ad2bf4c080b0</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper" Version="13.0.1"/> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.0"/> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0"/> | ||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0"/> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0"/> | ||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0"/> | ||
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0"/> | ||
<PackageReference Include="Swashbuckle.AspNetCore.JsonMultipartFormDataSupport" Version="2.0.0"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<InternalsVisibleTo Include="NeoServer.WebApi.Tests" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper" Version="13.0.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" /> | ||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" /> | ||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" /> | ||
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore.JsonMultipartFormDataSupport" Version="2.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="..\.dockerignore"> | ||
<Link>.dockerignore</Link> | ||
</Content> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Content Include="..\.dockerignore"> | ||
<Link>.dockerignore</Link> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Shared\NeoServer.IoC\NeoServer.Shared.IoC.csproj"/> | ||
<ProjectReference Include="..\Shared\NeoServer.Web.Shared\NeoServer.Web.Shared.csproj"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Shared\NeoServer.IoC\NeoServer.Shared.IoC.csproj" /> | ||
<ProjectReference Include="..\Shared\NeoServer.Web.Shared\NeoServer.Web.Shared.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="Certificates\aspnetapp.pfx"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,104 @@ | ||
using Serilog; | ||
using System.Net; | ||
using Microsoft.OpenApi.Models; | ||
using NeoServer.Shared.IoC.Modules; | ||
using NeoServer.Web.API.HttpFilters; | ||
using NeoServer.Web.API.IoC.Modules; | ||
using Newtonsoft.Json; | ||
using Swashbuckle.AspNetCore.JsonMultipartFormDataSupport.Extensions; | ||
using Swashbuckle.AspNetCore.JsonMultipartFormDataSupport.Integrations; | ||
|
||
namespace NeoServer.Web.API; | ||
var builder = WebApplication.CreateBuilder(args); | ||
var configuration = builder.Configuration; | ||
var environment = builder.Environment; | ||
|
||
public static class Program | ||
// Configure appsettings | ||
builder.Configuration | ||
.SetBasePath(environment.ContentRootPath) | ||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) | ||
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true) | ||
.AddEnvironmentVariables(); | ||
|
||
// Add services to the container | ||
var services = builder.Services; | ||
|
||
services.AddHttpContextAccessor(); | ||
services.AddBehaviours(); | ||
services.AddServicesApi(); | ||
services.AddAutoMapperProfiles(); | ||
|
||
services.Configure<ForwardedHeadersOptions>(options => | ||
{ | ||
options.ForwardLimit = 2; | ||
options.KnownProxies.Add(IPAddress.Parse("127.0.0.1")); | ||
options.KnownProxies.Add(IPAddress.Parse("0.0.0.0")); | ||
options.ForwardedForHeaderName = "X-Forwarded-For-My-Custom-Header-Name"; | ||
}); | ||
|
||
services.AddSwaggerGen(c => | ||
{ | ||
public static void Main(string[] args) | ||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "NeoServer.API", Version = "v1" }); | ||
|
||
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", | ||
Name = "Authorization", | ||
In = ParameterLocation.Header, | ||
Type = SecuritySchemeType.ApiKey, | ||
Scheme = "Bearer" | ||
}); | ||
|
||
private static IHostBuilder CreateHostBuilder(string[] args) | ||
c.AddSecurityRequirement(new OpenApiSecurityRequirement | ||
{ | ||
return Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) | ||
.ConfigureLogging(logging => | ||
{ | ||
new OpenApiSecurityScheme | ||
{ | ||
logging.ClearProviders(); | ||
logging.SetMinimumLevel(LogLevel.Trace); | ||
}) | ||
.UseSerilog(); | ||
} | ||
} | ||
Reference = new OpenApiReference | ||
{ | ||
Type = ReferenceType.SecurityScheme, | ||
Id = "Bearer" | ||
}, | ||
Scheme = "oauth2", | ||
Name = "Bearer", | ||
In = ParameterLocation.Header | ||
}, | ||
new List<string>() | ||
} | ||
}); | ||
|
||
c.OperationFilter<SwaggerJsonIgnoreFilter>(); | ||
}); | ||
|
||
services.AddJsonMultipartFormDataSupport(JsonSerializerChoice.Newtonsoft); | ||
|
||
services.AddControllersWithViews() | ||
.AddNewtonsoftJson(options => | ||
{ | ||
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; | ||
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc; | ||
}); | ||
|
||
services.AddLogger(configuration); | ||
services.AddDatabases(configuration); | ||
services.AddRepositories(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
|
||
app.UseRouting(); | ||
|
||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
|
||
app.UseCors(x => x | ||
.AllowAnyOrigin() | ||
.AllowAnyMethod() | ||
.AllowAnyHeader()); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); | ||
|
||
public partial class Program { } |
Oops, something went wrong.