Skip to content

Commit

Permalink
feat: initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardosbcabral committed Sep 15, 2023
1 parent c890aba commit 23b0c6e
Show file tree
Hide file tree
Showing 66 changed files with 3,564 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: ci

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
strategy:
fail-fast: false
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup .NET Core SDK ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ matrix.dotnet-version }}

- name: Install dependencies
run: dotnet restore src/Scaffolding.sln

- name: Build
run: dotnet build --configuration Release --no-restore src/Scaffolding.sln
27 changes: 27 additions & 0 deletions .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: pre-release

on:
release:
types: [prereleased]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup .NET SDK
uses: actions/setup-dotnet@v3

- name: Set current date as env variable
run: echo "NOW=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV
- name: Echo current date
run: echo $NOW

- run: dotnet build -c Release src/Scaffolding.sln

- name: Create the Scaffolding package
run: dotnet pack -c Release --no-build -p:Version="${{github.ref_name}}-alpha.${{ env.NOW }}" -o src/Scaffolding/bin/Release src/Scaffolding/Scaffolding.csproj

- name: Publish the Scaffolding package
run: dotnet nuget push src/Scaffolding/bin/Release/*.nupkg --api-key ${{secrets.NUGET_API_KEY}} --source https://api.nuget.org/v3/index.json
22 changes: 22 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: release

on:
release:
types: [released]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup .NET SDK
uses: actions/setup-dotnet@v3

- run: dotnet build -c Release src/Scaffolding.sln

- name: Create the Scaffolding package
run: dotnet pack -c Release --no-build -p:Version=${{github.ref_name}} -o src/Scaffolding/bin/Release src/Scaffolding/Scaffolding.csproj

- name: Publish the Scaffolding package
run: dotnet nuget push src/Scaffolding/bin/Release/*.nupkg --api-key ${{secrets.NUGET_API_KEY}} --source https://api.nuget.org/v3/index.json
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Scaffolding
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions src/Scaffolding.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33122.133
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scaffolding", "Scaffolding\Scaffolding.csproj", "{4BD115C2-4838-41FA-840A-7F57CCCC5B7A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4BD115C2-4838-41FA-840A-7F57CCCC5B7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BD115C2-4838-41FA-840A-7F57CCCC5B7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BD115C2-4838-41FA-840A-7F57CCCC5B7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BD115C2-4838-41FA-840A-7F57CCCC5B7A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {292857D8-236D-439B-8525-35046611AFE8}
EndGlobalSection
EndGlobal
108 changes: 108 additions & 0 deletions src/Scaffolding/Api.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using Scaffolding.Extensions.Cors;
using Scaffolding.Extensions.CultureInfo;
using Scaffolding.Extensions.Docs;
using Scaffolding.Extensions.ExceptionHandler;
using Scaffolding.Extensions.GracefulShutdown;
using Scaffolding.Extensions.Healthcheck;
using Scaffolding.Extensions.Json;
using Scaffolding.Extensions.Logging;
using Scaffolding.Extensions.RateLimiting;
using Scaffolding.Extensions.RequestKey;
using Scaffolding.Extensions.TimeElapsed;
using Scaffolding.Models;
using Scaffolding.Utilities;
using Serilog;
using Serilog.Context;
using System.Collections.Specialized;
using System.Reflection;

namespace Scaffolding;

public static class Api
{
public static WebApplicationBuilder Initialize(string[] args = null, string customUrls = null, params Assembly[] executingAssemblies)
{
var builder = WebApplication.CreateBuilder(args);

var apiSettings = builder.Configuration.GetSection("ApiSettings").Get<ApiSettings>();
if (apiSettings == null)
{
throw new Exception("'ApiSettings' section in the appsettings.json is required.");
}

builder.Configuration
.AddJsonFile($"appsettings.{EnvironmentUtility.GetCurrentEnvironment()}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables(apiSettings.EnvironmentVariablesPrefix);

builder.WebHost.UseUrls(customUrls ?? $"http://*:{apiSettings.Port}");
builder.Services.AddSingleton(apiSettings);
// It is needed because we cannot inject a service before
// the configuration is built. This context is used by
// the newtonsoft json settings configuration.
var httpContextAccessor = new HttpContextAccessor();
builder.Services.AddSingleton<IHttpContextAccessor>(httpContextAccessor);
builder.Services.AddOptions();

builder.SetupRateLimiting();
builder.SetupSwaggerDocs();
builder.SetupAllowCors();
builder.SetupGracefulShutdown();
builder.SetupRequestKey(apiSettings.RequestKeyProperty);
builder.SetupTimeElapsed(apiSettings.TimeElapsedProperty);
builder.ConfigureJsonSettings();

var mvc = builder.Services.AddControllers(x =>
{
x.EnableEndpointRouting = false;
});

foreach (var assembly in executingAssemblies)
{
mvc.AddApplicationPart(assembly);
}

mvc.SetupScaffoldingJsonSettings(apiSettings, httpContextAccessor);

return builder;
}

/// <summary>
/// Use scaffolding logging
/// </summary>
/// <param name="customLogger">Custom logger to combine with the existing one from scaffolding.</param>
public static void UseLogging(this WebApplicationBuilder builder, Serilog.ILogger customLogger = null)
{
builder.SetupScaffoldingSerilog(customLogger);
}

public static void UseDefaultConfiguration(this WebApplication app)
{
var apiSettings = app.Services.GetService<ApiSettings>();

app.UseScaffoldingDocumentation();
app.UseGracefulShutdown();
app.UseScaffoldingRateLimiting();
app.UseScaffoldingHealthchecks();
app.UseScaffoldingSerilog();
app.UseTimeElapsed();
app.UseScaffoldingRequestLocalization();
app.UseScaffoldingExceptionHandler();
app.UsePathBase(new(apiSettings.GetFullPath()));
app.UseRouting();
app.UseCors();
app.UseMvc();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}

public static async Task RunAsync(WebApplication app)
{
var apiSettings = app.Services.GetService<ApiSettings>();
LogContext.PushProperty("Application", apiSettings.Name);
LogContext.PushProperty("Version", apiSettings.BuildVersion);
Log.Logger.Information($"[{apiSettings.Name}] is running with version {apiSettings.BuildVersion}");
await app.RunAsync();
}
}
88 changes: 88 additions & 0 deletions src/Scaffolding/Controllers/AppInformationController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Microsoft.AspNetCore.Mvc;
using Scaffolding.Extensions.GracefulShutdown;
using Scaffolding.Extensions.Json;
using Scaffolding.Extensions.Logging;
using Scaffolding.Models;
using Scaffolding.Utilities;
using Scaffolding.Utilities.Converters;

namespace Scaffolding.Controllers;

[ApiController]
[Route("/")]
public class AppInformationController : ControllerBase
{
private readonly ApiSettings _apiSettings;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly GracefulShutdownState GracefulShutdownState;

public AppInformationController(
ApiSettings apiSettings,
ShutdownSettings shutdownSettings,
GracefulShutdownState gracefulShutdownState,
IHttpContextAccessor httpContextAccessor)
{
this.HandleGracefulShutdown(shutdownSettings);
_apiSettings = apiSettings;
_httpContextAccessor = httpContextAccessor;
this.GracefulShutdownState = gracefulShutdownState;
}

[HttpGet]
[Produces(typeof(HomeDetails))]
public IActionResult GetAppInfo()
{
this.DisableLogging();

return Ok(new HomeDetails
{
RequestsInProgress = GracefulShutdownState.RequestsInProgress,
BuildVersion = _apiSettings?.BuildVersion,
Environment = EnvironmentUtility.GetCurrentEnvironment(),
Application = _apiSettings.Name,
Domain = _apiSettings.Domain,
JsonSerializer = _apiSettings.JsonSerializer,
EnvironmentPrefix = _apiSettings.EnvironmentVariablesPrefix,
TimezoneInfo = new TimezoneInfo(_apiSettings, _httpContextAccessor),
CurrentTime = DateTime.UtcNow,
});
}

public class HomeDetails
{
public string Application { get; set; }
public string Domain { get; set; }
public string BuildVersion { get; set; }
public string Environment { get; set; }
public string EnvironmentPrefix { get; set; }
public long RequestsInProgress { get; set; }
public JsonSerializerEnum JsonSerializer { get; set; }
public TimezoneInfo TimezoneInfo { get; set; }
public DateTime CurrentTime { get; set; }
}

public class TimezoneInfo
{
private readonly ApiSettings _apiSettings;

public TimezoneInfo(ApiSettings apiSettings, IHttpContextAccessor httpContextAccessor)
{
CurrentTimezone = DateTimeConverter.GetTimeZoneByAspNetHeader(
httpContextAccessor,
apiSettings.TimezoneHeader).Id;

_apiSettings = apiSettings;
}

public static string UtcNow => DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss");

public static DateTime CurrentNow => DateTime.UtcNow;

public string DefaultTimezone => _apiSettings.TimezoneDefaultInfo.Id;

public static string CurrentTimezone { get; set; }

public string TimezoneHeader => _apiSettings.TimezoneHeader;
}

}
42 changes: 42 additions & 0 deletions src/Scaffolding/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Scaffolding.Extensions.Cors;
using WebApi.Models.Response;

namespace Scaffolding.Controllers;

[EnableCors(CorsServiceExtension.CorsName)]
public class BaseController : ControllerBase
{
public BaseController()
{
}

protected IActionResult CreateJsonResponse(ApiResponse response)
{
IActionResult result;

if (response.Content != null)
{
result = new JsonResult(response.Content)
{
StatusCode = (int)response.StatusCode
};
}
else
{
result = new StatusCodeResult((int)response.StatusCode);
Response.ContentType = "application/json";
}

if (response.Headers != null)
{
foreach (var header in response.Headers)
{
Response.Headers[header.Key] = header.Value;
}
}

return result;
}
}
9 changes: 9 additions & 0 deletions src/Scaffolding/Extensions/Cors/CorsMiddlewareExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Scaffolding.Extensions.Cors;

public static class CorsMiddlewareExtension
{
public static void AllowCors(this IApplicationBuilder app)
{
app.UseCors(CorsServiceExtension.CorsName);
}
}
14 changes: 14 additions & 0 deletions src/Scaffolding/Extensions/Cors/CorsServiceExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Scaffolding.Extensions.Cors;

public static class CorsServiceExtension
{
public const string CorsName = "EnableAll";

public static void SetupAllowCors(this WebApplicationBuilder builder)
{
builder.Services.AddCors(o => o.AddPolicy(CorsName, builder =>
{
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
}));
}
}
Loading

0 comments on commit 23b0c6e

Please sign in to comment.