forked from moattarwork/Easify
-
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
50fc3ae
commit 89ef2e3
Showing
18 changed files
with
465 additions
and
70 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/LittleBlocks.AspNetCore.Bootstrap/Extensions/ConfigurationExtensions.cs
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,45 @@ | ||
using LittleBlocks.Logging.SeriLog; | ||
|
||
namespace LittleBlocks.AspNetCore.Bootstrap.Extensions; | ||
|
||
public static class ConfigurationExtensions | ||
{ | ||
public static AppInfo GetApplicationInfo(this WebApplicationBuilder builder) | ||
{ | ||
var appName = builder.Configuration["Application:Name"] ?? builder.Environment.ApplicationName; | ||
var appVersion = builder.Configuration["Application:Version"] ?? "1.0.0"; | ||
var appDescription = builder.Configuration["Application:Description"] ?? ""; | ||
var appEnvironment = builder.Environment.EnvironmentName; | ||
return new AppInfo(appName, appVersion, appEnvironment, appDescription); | ||
} | ||
|
||
public static LoggingContext GetLoggingContext(this WebApplicationBuilder builder) | ||
{ | ||
var applicationInfo = builder.GetApplicationInfo(); | ||
return new LoggingContext(applicationInfo, applicationInfo.ToTags()) | ||
{ | ||
Host = "ASPNetCore", | ||
ExcludeEventFilter = "ClientAgent = 'AlwaysOn' or (RequestPath = '/health' and StatusCode < 400)" | ||
}; | ||
} | ||
|
||
public static HostInfo GetHostInfo(this WebApplicationBuilder builder) | ||
{ | ||
var hostType = Enum.TryParse(typeof(HostType), builder.Configuration["Host:Type"], out var result) | ||
? (HostType) result | ||
: HostType.Kestrel; | ||
return new HostInfo(hostType, hostType == HostType.Kestrel); | ||
} | ||
|
||
private static string[] ToTags(this AppInfo appInfo) | ||
{ | ||
ArgumentNullException.ThrowIfNull(appInfo); | ||
|
||
return new[] | ||
{ | ||
$"app:{appInfo.Name}", | ||
$"version:{appInfo.Version}", | ||
$"env:{appInfo.Environment}" | ||
}; | ||
} | ||
} |
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,3 @@ | ||
namespace LittleBlocks.AspNetCore.Bootstrap.Extensions; | ||
|
||
public record HostInfo(HostType Type, bool RequiredSslRedirect); |
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,7 @@ | ||
namespace LittleBlocks.AspNetCore.Bootstrap.Extensions; | ||
|
||
public enum HostType | ||
{ | ||
Kestrel, | ||
Docker, | ||
} |
70 changes: 70 additions & 0 deletions
70
src/LittleBlocks.AspNetCore.Bootstrap/Extensions/ServiceCollectionExtensions.cs
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,70 @@ | ||
using MicroElements.Swashbuckle.FluentValidation.AspNetCore; | ||
using Microsoft.FeatureManagement; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace LittleBlocks.AspNetCore.Bootstrap.Extensions; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddCorsWithDefaultPolicy(this IServiceCollection services) | ||
{ | ||
if (services == null) throw new ArgumentNullException(nameof(services)); | ||
|
||
services.AddCors(c => | ||
c.AddDefaultPolicy(p => p.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin())); | ||
return services; | ||
} | ||
|
||
public static IServiceCollection AddTypeMapping(this IServiceCollection services, | ||
Action<IMapperConfigurationExpression> configure) | ||
{ | ||
if (services == null) throw new ArgumentNullException(nameof(services)); | ||
if (configure == null) throw new ArgumentNullException(nameof(configure)); | ||
|
||
services.AddTransient<IMapper>(sp => new MapperConfiguration(configure).CreateMapper()); | ||
|
||
return services; | ||
} | ||
|
||
public static IServiceCollection AddOpenApiDocumentation(this IServiceCollection services, | ||
AppInfo appInfo) | ||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
ArgumentNullException.ThrowIfNull(appInfo); | ||
|
||
services.AddSwaggerGen(d => | ||
{ | ||
d.SwaggerDoc("v1", new OpenApiInfo | ||
{ | ||
Title = $"{appInfo.Name} - {appInfo.Environment}", | ||
Version = appInfo.Version, | ||
Description = appInfo.Description | ||
}); | ||
|
||
var xmlFiles = Directory.GetFiles(AppContext.BaseDirectory, "Ardevora.Feeds.Bloomberg.*.xml", | ||
SearchOption.TopDirectoryOnly).ToList(); | ||
xmlFiles.ForEach(xmlFile => d.IncludeXmlComments(xmlFile)); | ||
}); | ||
services.AddFluentValidationRulesToSwagger(); | ||
|
||
return services; | ||
} | ||
|
||
public static IServiceCollection AddFeatures<T>(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
if (configuration == null) throw new ArgumentNullException(nameof(configuration)); | ||
|
||
var name = typeof(T).Name; | ||
services.AddFeatureManagement(configuration.GetSection(name)); | ||
|
||
return services; | ||
} | ||
|
||
public static IServiceCollection Remove<T>(this IServiceCollection services) | ||
{ | ||
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(T)); | ||
services.Remove(descriptor); | ||
|
||
return services; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/LittleBlocks.AspNetCore.Bootstrap/Extensions/WebApplicationExtensions.cs
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,79 @@ | ||
using System.Net; | ||
using GlobalExceptionHandler.WebApi; | ||
using LittleBlocks.Exceptions; | ||
using Newtonsoft.Json; | ||
|
||
namespace LittleBlocks.AspNetCore.Bootstrap.Extensions; | ||
|
||
public static class WebApplicationExtensions | ||
{ | ||
public static void UseGlobalExceptionAndLoggingHandler(this WebApplication app) | ||
{ | ||
ArgumentNullException.ThrowIfNull(app); | ||
|
||
app.UseGlobalExceptionHandler(x => | ||
{ | ||
x.ContentType = "application/json"; | ||
x.ResponseBody(s => JsonConvert.SerializeObject(new | ||
{ | ||
Message = "An error occurred whilst processing your request" | ||
})); | ||
x.Map<AppException>() | ||
.ToStatusCode(HttpStatusCode.InternalServerError) | ||
.WithBody((e, context) => JsonConvert.SerializeObject(new {e.Message})); | ||
|
||
x.OnError((e, context) => | ||
{ | ||
app.Logger.LogError((Exception) e, "Error in processing the request to {Path}", context.Request.Path); | ||
return Task.CompletedTask; | ||
}); | ||
}); | ||
} | ||
|
||
public static void UseHttpsRedirection(this WebApplication app, HostInfo hostInfo) | ||
{ | ||
ArgumentNullException.ThrowIfNull(hostInfo); | ||
if (hostInfo.RequiredSslRedirect) | ||
app.UseHttpsRedirection(); | ||
} | ||
|
||
public static void UseOpenApiDocumentation(this WebApplication app, AppInfo appInfo) | ||
{ | ||
ArgumentNullException.ThrowIfNull(app); | ||
ArgumentNullException.ThrowIfNull(appInfo); | ||
|
||
var name = $"{appInfo.Name} v{appInfo.Version}"; | ||
|
||
app.UseSwagger(); | ||
app.UseSwaggerUI(c => | ||
{ | ||
c.SwaggerEndpoint("/swagger/v1/swagger.json", name); | ||
c.DocumentTitle = name; | ||
}); | ||
|
||
app.UseReDoc(c => | ||
{ | ||
c.DocumentTitle = name; | ||
c.SpecUrl("/swagger/v1/swagger.json"); | ||
}); | ||
} | ||
|
||
public static void MapDependencyHealthChecks(this WebApplication app) | ||
{ | ||
ArgumentNullException.ThrowIfNull(app); | ||
|
||
app.MapHealthChecks("/health", | ||
new HealthCheckOptions {ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse}); | ||
} | ||
|
||
public static IServiceCollection AddDependencyHealthChecks(this IServiceCollection services, | ||
Action<IHealthChecksBuilder>? configure = null) | ||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
|
||
var builder = services.AddHealthChecks(); | ||
configure?.Invoke(builder); | ||
|
||
return services; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/LittleBlocks.AspNetCore.Bootstrap/Extensions/WebHostBuilderExtensions.cs
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,15 @@ | ||
// namespace LittleBlocks.AspNetCore.Bootstrap.Extensions; | ||
// | ||
// public static class WebHostBuilderExtensions | ||
// { | ||
// public static void ConfigureAzureKeyVault(this IWebHostBuilder builder, | ||
// Action<AzureKeyVaultOptions>? configure = null) | ||
// { | ||
// if (builder == null) throw new ArgumentNullException(nameof(builder)); | ||
// | ||
// var options = new AzureKeyVaultOptions(); | ||
// configure?.Invoke(options); | ||
// | ||
// builder.ConfigureAppConfiguration((ctx, c) => { c.ConfigureAzureKeyVault(options); }); | ||
// } | ||
// } |
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
28 changes: 28 additions & 0 deletions
28
src/LittleBlocks.AspNetCore/RequestCorrelation/CorrelationExtensions.cs
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,28 @@ | ||
using CorrelationId; | ||
using CorrelationId.DependencyInjection; | ||
|
||
namespace LittleBlocks.AspNetCore.RequestCorrelation; | ||
|
||
public static class CorrelationExtensions | ||
{ | ||
public static IServiceCollection AddRequestCorrelation(this IServiceCollection service, | ||
Action<CorrelationIdOptions> configure = null) | ||
{ | ||
return service.AddDefaultCorrelationId(options => | ||
{ | ||
options.CorrelationIdGenerator = () => Guid.NewGuid().ToString(); | ||
options.AddToLoggingScope = true; | ||
options.EnforceHeader = false; | ||
options.IgnoreRequestHeader = false; | ||
options.IncludeInResponse = true; | ||
options.UpdateTraceIdentifier = true; | ||
|
||
configure?.Invoke(options); | ||
}); | ||
} | ||
|
||
public static void UseRequestCorrelation(this WebApplication app) | ||
{ | ||
app.UseCorrelationId(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using LittleBlocks.Configurations; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace LittleBlocks.Logging.SeriLog; | ||
|
||
public record LoggingContext(AppInfo AppInfo, string[] Tags) | ||
{ | ||
public string Host { get; init; } = ""; | ||
public string ExcludeEventFilter { get; init; } = ""; | ||
public bool EnableDebugging { get; init; } = false; | ||
public string DefaultFilePath { get; init; } = @"logs\log.txt"; | ||
public LogEventLevel DefaultLogLevel { get; init; } = LogEventLevel.Information; | ||
|
||
public bool IsDevelopment() | ||
{ | ||
return AppInfo?.Environment == Environments.Development; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace LittleBlocks.Exceptions; | ||
|
||
public class AppException : Exception | ||
{ | ||
public AppException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public AppException(string message, Exception innerException) : base(message, innerException) | ||
{ | ||
} | ||
} |
Oops, something went wrong.