-
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
Showing
12 changed files
with
187 additions
and
120 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
85 changes: 85 additions & 0 deletions
85
src/EthernaACR/Middlewares/DebugPages/DebugPagesMiddleware.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,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
19
src/EthernaACR/Middlewares/DebugPages/DebugPagesOptions.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,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; } | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/EthernaACR/Middlewares/DebugPages/Views/ConfigurationPage.cshtml
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 @@ | ||
@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> |
16 changes: 7 additions & 9 deletions
16
...Page/Views/PrintConfigurationPageModel.cs → ...ebugPages/Views/ConfigurationPageModel.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
32 changes: 32 additions & 0 deletions
32
src/EthernaACR/Middlewares/DebugPages/Views/RequestPage.cshtml
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,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
22
src/EthernaACR/Middlewares/DebugPages/Views/RequestPageModel.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,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; } | ||
} | ||
} |
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
68 changes: 0 additions & 68 deletions
68
src/EthernaACR/Middlewares/PrintConfigurationPage/PrintConfigurationPageMiddleware.cs
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
src/EthernaACR/Middlewares/PrintConfigurationPage/PrintConfigurationPageOptions.cs
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
src/EthernaACR/Middlewares/PrintConfigurationPage/Views/PrintConfigurationPage.cshtml
This file was deleted.
Oops, something went wrong.