Skip to content

Commit

Permalink
Merge JosepFe/clean_arquitecture_template
Browse files Browse the repository at this point in the history
Clean arquitecture template
  • Loading branch information
JosepFe authored Dec 10, 2024
2 parents ce6c93e + 7abb06f commit 8c216d1
Show file tree
Hide file tree
Showing 112 changed files with 2,547 additions and 618 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/nuget-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Build and Publish NuGet Package

on:
workflow_dispatch:
inputs:
project:
description: 'Select the project to build'
required: true
default: 'Path/To/Project1.csproj'
options:
- 'Path/To/Project1.csproj'
- 'Path/To/Project2.csproj'
version_type:
description: 'Select the version type'
required: true
default: 'patch'
options:
- 'major'
- 'minor'
- 'patch'
- 'preview'

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup .NET Core
uses: actions/setup-dotnet@v2
with:
dotnet-version: '7.x'

- name: Get latest version from GitHub Packages
run: |
LATEST_VERSION=$(dotnet nuget list source --source "https://nuget.pkg.github.com/<YOUR_GITHUB_USERNAME>/index.json" | grep "<PackageName>" | head -n 1 | awk '{print $2}')
echo "Latest version: $LATEST_VERSION"
echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_ENV
- name: Determine new version
id: version
run: |
VERSION_TYPE="${{ github.event.inputs.version_type }}"
LATEST_VERSION="${{ env.LATEST_VERSION }}"
# Extract major, minor, patch from latest version (assumes format major.minor.patch or major.minor.patch-preview)
MAJOR=$(echo $LATEST_VERSION | cut -d. -f1)
MINOR=$(echo $LATEST_VERSION | cut -d. -f2)
PATCH=$(echo $LATEST_VERSION | cut -d. -f3 | cut -d- -f1) # Removes preview suffix if present
# Increment version based on user input
if [ "$VERSION_TYPE" == "major" ]; then
NEW_VERSION="$((MAJOR + 1)).0.0"
elif [ "$VERSION_TYPE" == "minor" ]; then
NEW_VERSION="$MAJOR.$((MINOR + 1)).0"
elif [ "$VERSION_TYPE" == "patch" ]; then
NEW_VERSION="$MAJOR.$MINOR.$((PATCH + 1))"
elif [ "$VERSION_TYPE" == "preview" ]; then
PREVIEW_SUFFIX="-preview"
NEW_VERSION="$MAJOR.$MINOR.$((PATCH + 1))$PREVIEW_SUFFIX"
fi
echo "New version: $NEW_VERSION"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
- name: Restore dependencies
run: dotnet restore ${{ github.event.inputs.project }}

- name: Build project
run: dotnet build ${{ github.event.inputs.project }} --configuration Release --no-restore

- name: Pack NuGet package
run: dotnet pack ${{ github.event.inputs.project }} --configuration Release --no-build --output ./nuget-packages /p:PackageVersion=${{ env.NEW_VERSION }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.suo
*.user
*.sln.docstates
*.env

# Build results
[Dd]ebug/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.1" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PackageReference Include="Amazon.Lambda.Core" Version="2.2.0" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.300.39" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.300" />
<PackageReference Include="System.Text.Json" Version="8.0.1" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="System.Text.Json" Version="8.0.1" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\assets\images\devonfw.png">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Devon4Net.Infrastructure.Common.Application.ApplicationTypes.API.Servers;
using Devon4Net.Infrastructure.Common.Application.ApplicationTypes.API.Configuration;
using Devon4Net.Infrastructure.Common.Application.Attributes;
using Devon4Net.Infrastructure.Common.Helpers;
using Devon4Net.Infrastructure.Common.Helpers.Interfaces;

namespace Devon4Net.Infrastructure.Common.Application.ApplicationTypes.API
{
Expand Down Expand Up @@ -39,6 +41,8 @@ public static void InitializeDevonfwApi(this IWebHostBuilder builder, IHostBuild

public static DevonfwOptions SetupDevonfw(this IServiceCollection services, IConfiguration configuration)
{
services?.SetupDevonfwServices();

DevonfwOptions = services?.GetTypedOptions<DevonfwOptions>(configuration, OptionsDefinition.DefaultSettingsNodeName);

if (DevonfwOptions == null)
Expand All @@ -53,11 +57,19 @@ public static DevonfwOptions SetupDevonfw(this IServiceCollection services, ICon
services?.AddMvc(options => options.Filters.Add(typeof(ModelStateCheckerAttribute)));
}

services.AddScoped<ExceptionHandlingFilterAttribute>();

if (DevonfwOptions.UseIIS) services?.ConfigureIIS(DevonfwOptions.IIS);

if (DevonfwOptions.UseXsrf) services?.ConfigureXsrf();

return DevonfwOptions;
}

private static void SetupDevonfwServices(this IServiceCollection services)
{
services.AddSingleton<IRecyclableMemoryHelper, RecyclableMemoryHelper>();
services.AddTransient(typeof(IJsonHelper), typeof(JsonHelper));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using Devon4Net.Infrastructure.Common.Exceptions;

namespace Devon4Net.Infrastructure.Common.Application.Attributes
{
public class ExceptionHandlingFilterAttribute : ExceptionFilterAttribute
{
public ExceptionHandlingFilterAttribute() { }

public override Task OnExceptionAsync(ExceptionContext context)
{
if (context.Exception.InnerException != null)
Devon4NetLogger.Debug(context.Exception.InnerException?.ToString()!);

context.Result = context.Exception switch
{
WebApiException webApiException => HandleContext(webApiException.StatusCode, webApiException.Message, webApiException.ShowMessage),
HttpRequestException httpRequestException => HandleContext((int?)httpRequestException.StatusCode, httpRequestException.Message),
_ => HandleContext(),
};

return Task.CompletedTask;
}

private IActionResult HandleContext(int? statusCode = null, string errorMessage = null, bool showMessage = false)
{
statusCode ??= StatusCodes.Status500InternalServerError;

if (!showMessage || statusCode == StatusCodes.Status204NoContent || string.IsNullOrEmpty(errorMessage))
{
return new ContentResult
{
StatusCode = (int)statusCode,
Content = string.Empty,
};
}

var response = new { error = errorMessage };

return new JsonResult(response)
{
StatusCode = (int)statusCode,
};
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,23 @@ public KillSwicthMiddleware(RequestDelegate next)

public async Task Invoke(HttpContext context, IOptionsMonitor<KillSwitchOptions> killSwitch)
{
try
if (killSwitch?.CurrentValue.UseKillSwitch == true)
{
if (killSwitch?.CurrentValue.UseKillSwitch == true)
if (killSwitch.CurrentValue?.EnableRequests == true)
{
if (killSwitch.CurrentValue?.EnableRequests == true)
{
await _next(context).ConfigureAwait(false);
}
else
{
context.Response.Headers.Clear();
context.Response.StatusCode = killSwitch.CurrentValue?.HttpStatusCode > 0 ? killSwitch.CurrentValue.HttpStatusCode : 403;
await context.Response.WriteAsync(string.Empty).ConfigureAwait(false);
}
await _next(context).ConfigureAwait(false);
}
else
{
await _next(context).ConfigureAwait(false);
context.Response.Headers.Clear();
context.Response.StatusCode = killSwitch.CurrentValue?.HttpStatusCode > 0 ? killSwitch.CurrentValue.HttpStatusCode : 403;
await context.Response.WriteAsync(string.Empty).ConfigureAwait(false);
}
}
#pragma warning disable CA1031 // #warning directive
catch (System.Exception ex)
#pragma warning restore CA1031 // #warning directive
else
{
await HandleException(ref context, ref ex).ConfigureAwait(false);
await _next(context).ConfigureAwait(false);
}
}

private static Task HandleException(ref HttpContext context, ref System.Exception exception)
{
Devon4NetLogger.Error(exception);

var exceptionTypeValue = exception.GetType();
var exceptionInterfaces = exceptionTypeValue.GetInterfaces().Select(i => i.Name).ToList();
exceptionInterfaces.Add(exceptionTypeValue.Name);

return exceptionInterfaces switch
{
{ } exceptionType when exceptionType.Contains("InvalidDataException") => HandleContext(ref context,
StatusCodes.Status422UnprocessableEntity),
{ } exceptionType when exceptionType.Contains("ArgumentException") ||
exceptionType.Contains("ArgumentNullException") ||
exceptionType.Contains("NotFoundException") ||
exceptionType.Contains("FileNotFoundException") => HandleContext(ref context,
StatusCodes.Status400BadRequest),
{ } exceptionType when exceptionType.Contains("IWebApiException") => HandleContext(ref context,
((IWebApiException) exception).StatusCode, exception.Message,
((IWebApiException) exception).ShowMessage),
_ => HandleContext(ref context, StatusCodes.Status500InternalServerError, exception.Message)
};
}

private static Task HandleContext(ref HttpContext context, int? statusCode = null, string errorMessage = null, bool showMessage = false)
{
context.Response.Headers.Clear();
context.Response.StatusCode = statusCode ?? StatusCodes.Status500InternalServerError;

if (!showMessage || statusCode == StatusCodes.Status204NoContent || string.IsNullOrEmpty(errorMessage) ) return Task.CompletedTask;

context.Response.ContentType = "application/json";
return context.Response.WriteAsync(JsonSerializer.Serialize(new { error = errorMessage }));
}
}
}
Loading

0 comments on commit 8c216d1

Please sign in to comment.