Skip to content

Commit

Permalink
merge hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Nov 30, 2022
2 parents 3e68a9d + 293114b commit 124f7f8
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
using Microsoft.Extensions.Options;
using System;

namespace Etherna.ACR.Middlewares.PrintConfigurationPage
namespace Etherna.ACR.Middlewares.DebugPages
{
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UsePrintConfigurationPage(this IApplicationBuilder app)
public static IApplicationBuilder UseEthernaAcrDebugPages(this IApplicationBuilder app)
{
if (app == null)
throw new ArgumentNullException(nameof(app));

return app.UseMiddleware<PrintConfigurationPageMiddleware>();
return app.UseMiddleware<DebugPagesMiddleware>();
}

public static IApplicationBuilder UsePrintConfigurationPage(
public static IApplicationBuilder UseEthernaAcrDebugPages(
this IApplicationBuilder app,
PrintConfigurationPageOptions options)
DebugPagesOptions options)
{
if (app == null)
throw new ArgumentNullException(nameof(app));
if (options == null)
throw new ArgumentNullException(nameof(options));

return app.UseMiddleware<PrintConfigurationPageMiddleware>(Options.Create(options));
return app.UseMiddleware<DebugPagesMiddleware>(Options.Create(options));
}
}
}
85 changes: 85 additions & 0 deletions src/EthernaACR/Middlewares/DebugPages/DebugPagesMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Etherna.ACR.Middlewares.DebugPages.Views;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;

namespace Etherna.ACR.Middlewares.DebugPages
{
public class DebugPagesMiddleware
{
private readonly RequestDelegate next;
private readonly DebugPagesOptions options;

public DebugPagesMiddleware(
RequestDelegate next,
IOptions<DebugPagesOptions> options)
{
if (options is null)
throw new ArgumentNullException(nameof(options));

this.next = next;
this.options = options.Value;
}

public async Task InvokeAsync(HttpContext context)
{
if (context is null)
throw new ArgumentNullException(nameof(context));

if (context.Request.Path == options.ConfigurationPagePath)
{
var executor = context.RequestServices.GetRequiredService<IActionResultExecutor<ViewResult>>();
var actionContext = new ActionContext(
context,
context.GetRouteData() ?? new RouteData(),
new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor());
var viewResult = new ViewResult()
{
ViewData = new ViewDataDictionary(
new EmptyModelMetadataProvider(),
new ModelStateDictionary())
{
Model = new ConfigurationPageModel(
context.RequestServices.GetRequiredService<IConfiguration>(), options)
},
ViewName = "~/Middlewares/DebugPages/Views/ConfigurationPage.cshtml"
};

await executor.ExecuteAsync(actionContext, viewResult);
}
else if (context.Request.Path == options.RequestPagePath)
{
var executor = context.RequestServices.GetRequiredService<IActionResultExecutor<ViewResult>>();
var actionContext = new ActionContext(
context,
context.GetRouteData() ?? new RouteData(),
new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor());
var viewResult = new ViewResult()
{
ViewData = new ViewDataDictionary(
new EmptyModelMetadataProvider(),
new ModelStateDictionary())
{
Model = new RequestPageModel(context.Request, options)
},
ViewName = "~/Middlewares/DebugPages/Views/RequestPage.cshtml"
};

await executor.ExecuteAsync(actionContext, viewResult);
}
else
{
// Call the next delegate/middleware in the pipeline.
await next(context);
}
}
}
}
19 changes: 19 additions & 0 deletions src/EthernaACR/Middlewares/DebugPages/DebugPagesOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Etherna.ACR.Middlewares.DebugPages
{
public class DebugPagesOptions
{
public DebugPagesOptions()
{
ConfigurationPagePath = "/debug/config";
ConfigurationPageTitle = "Configuration";

RequestPagePath = "/debug/request";
RequestPageTitle = "Request";
}

public string ConfigurationPagePath { get; set; }
public string ConfigurationPageTitle { get; set; }
public string RequestPagePath { get; set; }
public string RequestPageTitle { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@model ConfigurationPageModel

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>@Model.Title</title>
</head>
<body>

<partial name="~/Middlewares/DebugPages/Views/_PrintConfigurationPartial.cshtml" model="Model.Configuration" />

</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Etherna.ACR.Middlewares.PrintConfigurationPage.Views
namespace Etherna.ACR.Middlewares.DebugPages.Views
{
public class PrintConfigurationPageModel
public class ConfigurationPageModel
{
public PrintConfigurationPageModel(
// Constructor.
public ConfigurationPageModel(
IConfiguration configuration,
PrintConfigurationPageOptions options)
DebugPagesOptions options)
{
if (options is null)
throw new ArgumentNullException(nameof(options));

Configuration = configuration;
Title = options.PageTitle;
Title = options.ConfigurationPageTitle;
}

// Properties.
public IConfiguration Configuration { get; }
public string Title { get; }
}
Expand Down
32 changes: 32 additions & 0 deletions src/EthernaACR/Middlewares/DebugPages/Views/RequestPage.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@model RequestPageModel

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>@Model.Title</title>
</head>
<body>

<h2>Headers</h2>
<ul>
@foreach (var header in Model.Request.Headers)
{
<li>
@header.Key|@header.Value
</li>
}
</ul>

<h2>Values</h2>
<ul>
<li>Protocol: @Model.Request.Protocol</li>
<li>Method: @Model.Request.Method</li>
<li>Scheme: @Model.Request.Scheme</li>
<li>Host: @Model.Request.Host</li>
<li>Path: @Model.Request.Path</li>
<li>Query: @Model.Request.QueryString</li>
</ul>

</body>
</html>
22 changes: 22 additions & 0 deletions src/EthernaACR/Middlewares/DebugPages/Views/RequestPageModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.AspNetCore.Http;
using System;

namespace Etherna.ACR.Middlewares.DebugPages.Views
{
public class RequestPageModel
{
// Constructor.
public RequestPageModel(HttpRequest request, DebugPagesOptions options)
{
if (options is null)
throw new ArgumentNullException(nameof(options));

Request = request;
Title = options.RequestPageTitle;
}

// Properties.
public HttpRequest Request { get; }
public string Title { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<li>
@configSection.Key|@configSection.Value
</li>
<partial name="~/Middlewares/PrintConfigurationPage/Views/_PrintConfigurationPartial.cshtml" model="configSection" />
<partial name="~/Middlewares/DebugPages/Views/_PrintConfigurationPartial.cshtml" model="configSection" />
}
</ul>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
* limitations under the License.
*@

@namespace Etherna.ACR.Middlewares.PrintConfigurationPage.Views
@namespace Etherna.ACR.Middlewares.DebugPages.Views
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 124f7f8

Please sign in to comment.