Skip to content

Commit

Permalink
Refactor (#4)
Browse files Browse the repository at this point in the history
* Rename MessageBus to ServiceBus; apply file-scoped namespace

* S1940 Use the opposite operator ('<') instead

* comment DO NOT enable `app.UseHsts()` by mistake

* Cleanup

* Get multi version of certificates

* Add multi connections query

* Add Diagnostics/GetInfo with application assembly info and timezone info

* Consolidate Directory.Build.targets

* Update ApiExplorer

* Use nuget NetLah.Extensions.HttpOverrides
  • Loading branch information
thohng authored Aug 31, 2022
1 parent b47f649 commit 0a09c9c
Show file tree
Hide file tree
Showing 23 changed files with 589 additions and 727 deletions.
17 changes: 10 additions & 7 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<Project>

<PropertyGroup>
<CurrentFrameworkVersion>6.0.8</CurrentFrameworkVersion>
<FrameworkVersion>6.*</FrameworkVersion>
<SwashbuckleVersion>6.*</SwashbuckleVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,6 +18,7 @@
<PackageReference Update="NetLah.Abstractions" Version="[0.*,1.0)" />
<PackageReference Update="NetLah.Extensions.Configuration" Version="[0.*,1.0)" />
<PackageReference Update="NetLah.Extensions.EventAggregator" Version="[0.*,1.0)" />
<PackageReference Update="NetLah.Extensions.HttpOverrides" Version="[0.2.*-*,0.3.0)" />
<PackageReference Update="NetLah.Extensions.Logging.Serilog" Version="[0.2.*-*,0.3.0)" />
<PackageReference Update="NetLah.Extensions.Logging.Serilog.AspNetCore" Version="[0.2.*-*,0.3.0)" />
<PackageReference Update="Testing.Dynamic.Json" Version="1.*-*" />
Expand All @@ -33,9 +36,9 @@

<!--serilog and nlog-->
<PackageReference Update="Serilog" Version="[2.11.*,3)" />
<PackageReference Update="Serilog.AspNetCore" Version="[5.*-*,6)" />
<PackageReference Update="Serilog.AspNetCore" Version="[6.*-*,7)" />
<PackageReference Update="Serilog.Enrichers.Thread" Version="[3.*-*,4)" />
<PackageReference Update="Serilog.Extensions.Hosting" Version="[4.*-*,5)" />
<PackageReference Update="Serilog.Extensions.Hosting" Version="[5.*-*,6)" />
<PackageReference Update="Serilog.Extensions.Logging" Version="[3.*-*,4)" />
<PackageReference Update="Serilog.Formatting.Compact" Version="[1.*-*,2)" />
<PackageReference Update="Serilog.Settings.Configuration" Version="[3.*-*,4)" />
Expand Down Expand Up @@ -120,7 +123,7 @@
<PackageReference Update="System.Drawing.Common" Version="$(FrameworkVersion)" />
<PackageReference Update="System.Net.Http.Json" Version="$(FrameworkVersion)" />
<PackageReference Update="System.Text.Json" Condition="'$(TargetFramework)' == 'net5.0'" Version="5.*" />
<PackageReference Update="System.Text.Json" Condition="'$(TargetFramework)' != 'net5.0'" Version="$(FrameworkVersion)" />
<PackageReference Update="System.Text.Json" Condition="'$(TargetFramework)' != 'net5.0'" Version="6.*" />

<!--microsoft asp.net core-->
<PackageReference Update="Microsoft.AspNetCore.Authentication.Facebook" Version="$(FrameworkVersion)" />
Expand Down Expand Up @@ -190,10 +193,10 @@
<!--open api-->
<PackageReference Update="Microsoft.AspNetCore.Mvc.Versioning" Version="5.*" />
<PackageReference Update="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.*" />
<PackageReference Update="Swashbuckle.AspNetCore" Version="6.*" />
<PackageReference Update="Swashbuckle.AspNetCore.Swagger" Version="6.*" />
<PackageReference Update="Swashbuckle.AspNetCore.SwaggerGen" Version="6.*" />
<PackageReference Update="Swashbuckle.AspNetCore.SwaggerUI" Version="6.*" />
<PackageReference Update="Swashbuckle.AspNetCore" Version="$(SwashbuckleVersion)" />
<PackageReference Update="Swashbuckle.AspNetCore.Swagger" Version="$(SwashbuckleVersion)" />
<PackageReference Update="Swashbuckle.AspNetCore.SwaggerGen" Version="$(SwashbuckleVersion)" />
<PackageReference Update="Swashbuckle.AspNetCore.SwaggerUI" Version="$(SwashbuckleVersion)" />

<!--wcf-->
<PackageReference Update="SoapCore" Version="1.1.*" />
Expand Down
31 changes: 24 additions & 7 deletions EchoServiceApi/Controllers/DiagnosticsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
namespace EchoServiceApi.Controllers
{
[Route("[controller]/[action]")]
[ApiController]
public class DiagnosticsController : ControllerBase
{
public IActionResult Connection([FromServices] HttpContextInfo httpContextInfo)
public IActionResult GetInfo([FromServices] NetLah.Diagnostics.IAssemblyInfo appInfo)
{
try
{
return Ok($"AppTitle:{appInfo.Title}; Version:{appInfo.InformationalVersion} BuildTime:{appInfo.BuildTimestampLocal}; Framework:{appInfo.FrameworkName}; TimeZoneInfo.Local:{TimeZoneInfo.Local.DisplayName} / {TimeZoneInfo.Local.BaseUtcOffset}");
}
catch (Exception ex)
{
return Ok(VerifyResult.Failed(ex));
}
}

public IActionResult Connection([FromServices] HttpContextInfo httpContextInfo, string? endpoint)
{
try
{
Expand All @@ -18,7 +29,8 @@ public IActionResult Connection([FromServices] HttpContextInfo httpContextInfo)
{
remoteIpAddress = $"[{remoteIpAddress}]";
}
var connectionInfo = $"Server:{request.Scheme}://{httpContextInfo.Host}:{httpContextInfo.Port} Client:{remoteIpAddress}:{remote?.RemotePort}";
var endpointInfo = string.IsNullOrEmpty(endpoint) ? null : $"[{endpoint}]";
var connectionInfo = $"Server{endpointInfo}:{request.Scheme}://{httpContextInfo.Host}:{httpContextInfo.Port} Client:{remoteIpAddress}:{remote?.RemotePort}";
return Ok(connectionInfo);
}
catch (Exception ex)
Expand All @@ -27,6 +39,11 @@ public IActionResult Connection([FromServices] HttpContextInfo httpContextInfo)
}
}

// Add multi connections query
public IActionResult Connection1([FromServices] HttpContextInfo httpContextInfo) => Connection(httpContextInfo, "Connection1");
public IActionResult Connection2([FromServices] HttpContextInfo httpContextInfo) => Connection(httpContextInfo, "Connection2");
public IActionResult Connection3([FromServices] HttpContextInfo httpContextInfo) => Connection(httpContextInfo, "Connection3");

public async Task<IActionResult> CosmosCacheAsync([FromServices] CosmosCacheVerifier cosmosVerifier, string name)
{
try
Expand Down Expand Up @@ -66,11 +83,11 @@ public async Task<IActionResult> PostgreSqlAsync([FromServices] PosgreSqlVerifie
}
}

public async Task<IActionResult> KeyVaultCertificateAsync([FromServices] KeyVaultCertificateVerifier keyVaultCertificateVerifier, string name, bool privateKey)
public async Task<IActionResult> KeyVaultCertificateAsync([FromServices] KeyVaultCertificateVerifier keyVaultCertificateVerifier, string name, bool privateKey, bool all)
{
try
{
var result = await keyVaultCertificateVerifier.VerifyAsync(name, privateKey);
var result = await keyVaultCertificateVerifier.VerifyAsync(name, privateKey, all);
return Ok(result);
}
catch (Exception ex)
Expand Down Expand Up @@ -128,11 +145,11 @@ public async Task<IActionResult> DirAsync([FromServices] DirVerifier dirVerifier
}
}

public async Task<IActionResult> MessageBusAsync([FromServices] MessageBusVerifier messageBusVerifier, string name, bool send, bool receive, string? queueName)
public async Task<IActionResult> ServiceBusAsync([FromServices] ServiceBusVerifier serviceBusVerifier, string name, bool send, bool receive, string? queueName)
{
try
{
var result = await messageBusVerifier.VerifyAsync(name, send, receive, queueName);
var result = await serviceBusVerifier.VerifyAsync(name, send, receive, queueName);
return Ok(result);
}
catch (Exception ex)
Expand Down
1 change: 0 additions & 1 deletion EchoServiceApi/Controllers/DumpController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace EchoServiceApi.Controllers;

[Route("[controller]/[action]")]
[ApiController]
public class DumpController : ControllerBase
{
private static IDictionary<string, string?> GetEnvironmentVariables()
Expand Down
2 changes: 1 addition & 1 deletion EchoServiceApi/Controllers/EchoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace EchoServiceApi.Controllers;

[Route("e")]
[ApiController]
public class EchoController : ControllerBase
{
[Route("400")]
Expand Down Expand Up @@ -36,6 +35,7 @@ public class EchoController : ControllerBase
};

[Route("{*url}")]
[ApiExplorerSettings(IgnoreApi = true)]
public async Task<ActionResult<MyResult>> Action(string url)
{
var request = HttpContext.Request;
Expand Down
1 change: 1 addition & 0 deletions EchoServiceApi/EchoServiceApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
<PackageReference Include="NetLah.Abstractions" />
<PackageReference Include="NetLah.Extensions.Configuration" />
<PackageReference Include="NetLah.Extensions.HttpOverrides" />
<PackageReference Include="NetLah.Extensions.Logging.Serilog.AspNetCore" />
<PackageReference Include="Npgsql" />
<PackageReference Include="Serilog" />
Expand Down
83 changes: 41 additions & 42 deletions EchoServiceApi/HttpContextInfo.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
namespace EchoServiceApi
namespace EchoServiceApi;

public class HttpContextInfo
{
public class HttpContextInfo
private readonly HttpContext _httpContext;
private readonly Lazy<HostAndPort> _getHostAndPort;

public HttpContextInfo(IHttpContextAccessor httpContextAccessor)
{
private readonly HttpContext _httpContext;
private readonly Lazy<HostAndPort> _getHostAndPort;
_httpContext = httpContextAccessor?.HttpContext ?? throw new ArgumentNullException(nameof(httpContextAccessor), "HttpContext is required");
_getHostAndPort = new Lazy<HostAndPort>(GetHostAndPort);
}

public HttpContextInfo(IHttpContextAccessor httpContextAccessor)
private HostAndPort GetHostAndPort()
{
var request = _httpContext.Request;
var hostMayWithPort = request.Host;
var host = hostMayWithPort.Host;
if (hostMayWithPort.Port is { } port)
{
_httpContext = httpContextAccessor?.HttpContext ?? throw new ArgumentNullException(nameof(httpContextAccessor), "HttpContext is required");
_getHostAndPort = new Lazy<HostAndPort>(GetHostAndPort);
return new HostAndPort(host, port);
}

private HostAndPort GetHostAndPort()
if (request.Headers["X-FORWARDED-PORT"] is { } headerValues &&
headerValues.FirstOrDefault() is { } headerValue &&
int.TryParse(headerValue, out var portValue2))
{
var request = _httpContext.Request;
var hostMayWithPort = request.Host;
var host = hostMayWithPort.Host;
if (hostMayWithPort.Port is { } port)
{
return new HostAndPort(host, port);
}

if (request.Headers["X-FORWARDED-PORT"] is { } headerValues &&
headerValues.FirstOrDefault() is { } headerValue &&
int.TryParse(headerValue, out var portValue2))
{
return new HostAndPort(host, portValue2);
}
else
{
var connection = _httpContext.Connection;
var localPort = connection.LocalPort;
var unexpectedPort = request.Scheme == "https" ? 80 : 443;
var defaultPortScheme = request.Scheme == "https" ? 443 : 80;
var portValue3 = localPort == unexpectedPort ? defaultPortScheme : localPort;
return new HostAndPort(host, portValue3);
}
return new HostAndPort(host, portValue2);
}
else
{
var connection = _httpContext.Connection;
var localPort = connection.LocalPort;
var unexpectedPort = request.Scheme == "https" ? 80 : 443;
var defaultPortScheme = request.Scheme == "https" ? 443 : 80;
var portValue3 = localPort == unexpectedPort ? defaultPortScheme : localPort;
return new HostAndPort(host, portValue3);
}
}

public string Host => _getHostAndPort.Value.Host;
public int Port => _getHostAndPort.Value.Port;
public string Host => _getHostAndPort.Value.Host;
public int Port => _getHostAndPort.Value.Port;

private class HostAndPort
private sealed class HostAndPort
{
public HostAndPort(string host, int port)
{
public HostAndPort(string host, int port)
{
Host = host;
Port = port;
}

public string Host { get; }
public int Port { get; }
Host = host;
Port = port;
}

public string Host { get; }
public int Port { get; }
}
}
Loading

0 comments on commit 0a09c9c

Please sign in to comment.