-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c890aba
commit 81333b6
Showing
64 changed files
with
3,558 additions
and
0 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
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 |
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
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 |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Scaffolding.Extensions.Cors; | ||
|
||
public static class CorsMiddlewareExtension | ||
{ | ||
public static void AllowCors(this IApplicationBuilder app) | ||
{ | ||
app.UseCors(CorsServiceExtension.CorsName); | ||
} | ||
} |
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 |
---|---|---|
@@ -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(); | ||
})); | ||
} | ||
} |
Oops, something went wrong.