-
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
8 changed files
with
192 additions
and
2 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
29 changes: 29 additions & 0 deletions
29
src/EthernaACR/Middlewares/PrintConfigurationPage/PrintConfigurationPageExtensions.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,29 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
|
||
namespace Etherna.ACR.Middlewares.PrintConfigurationPage | ||
{ | ||
public static class ApplicationBuilderExtensions | ||
{ | ||
public static IApplicationBuilder UsePrintConfigurationPage(this IApplicationBuilder app) | ||
{ | ||
if (app == null) | ||
throw new ArgumentNullException(nameof(app)); | ||
|
||
return app.UseMiddleware<PrintConfigurationPageMiddleware>(); | ||
} | ||
|
||
public static IApplicationBuilder UsePrintConfigurationPage( | ||
this IApplicationBuilder app, | ||
PrintConfigurationPageOptions options) | ||
{ | ||
if (app == null) | ||
throw new ArgumentNullException(nameof(app)); | ||
if (options == null) | ||
throw new ArgumentNullException(nameof(options)); | ||
|
||
return app.UseMiddleware<PrintConfigurationPageMiddleware>(Options.Create(options)); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/EthernaACR/Middlewares/PrintConfigurationPage/PrintConfigurationPageMiddleware.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,68 @@ | ||
using Etherna.ACR.Middlewares.PrintConfigurationPage.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.PrintConfigurationPage | ||
{ | ||
public class PrintConfigurationPageMiddleware | ||
{ | ||
private readonly RequestDelegate next; | ||
private readonly PrintConfigurationPageOptions options; | ||
|
||
public PrintConfigurationPageMiddleware( | ||
RequestDelegate next, | ||
IOptions<PrintConfigurationPageOptions> 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.PagePath) | ||
{ | ||
var viewResult = new ViewResult() | ||
{ | ||
ViewName = "~/Middlewares/PrintConfigurationPage/Views/PrintConfigurationPage.cshtml" | ||
}; | ||
|
||
var viewDataDictionary = new ViewDataDictionary( | ||
new EmptyModelMetadataProvider(), | ||
new ModelStateDictionary()) | ||
{ | ||
Model = new PrintConfigurationPageModel( | ||
context.RequestServices.GetRequiredService<IConfiguration>(), options) | ||
}; | ||
viewResult.ViewData = viewDataDictionary; | ||
|
||
var executor = context.RequestServices.GetRequiredService<IActionResultExecutor<ViewResult>>(); | ||
var actionContext = new ActionContext( | ||
context, | ||
context.GetRouteData() ?? new RouteData(), | ||
new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor()); | ||
|
||
await executor.ExecuteAsync(actionContext, viewResult); | ||
} | ||
else | ||
{ | ||
// Call the next delegate/middleware in the pipeline. | ||
await next(context); | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/EthernaACR/Middlewares/PrintConfigurationPage/PrintConfigurationPageOptions.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,14 @@ | ||
namespace Etherna.ACR.Middlewares.PrintConfigurationPage | ||
{ | ||
public class PrintConfigurationPageOptions | ||
{ | ||
public PrintConfigurationPageOptions() | ||
{ | ||
PagePath = "/debug/config"; | ||
PageTitle = "Configuration print"; | ||
} | ||
|
||
public string PagePath { get; set; } | ||
public string PageTitle { get; set; } | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/EthernaACR/Middlewares/PrintConfigurationPage/Views/PrintConfigurationPage.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,21 @@ | ||
@using System | ||
@using System.Globalization | ||
@using System.Linq | ||
@using System.Net | ||
@using System.Reflection | ||
@using Microsoft.AspNetCore.Diagnostics.RazorViews | ||
@using Microsoft.AspNetCore.Diagnostics | ||
@model PrintConfigurationPageModel | ||
|
||
<!DOCTYPE html> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>@Model.Title</title> | ||
</head> | ||
<body> | ||
|
||
<partial name="_PrintConfigurationSectionPartial" model="Model.Configuration" /> | ||
|
||
</body> | ||
</html> |
26 changes: 26 additions & 0 deletions
26
src/EthernaACR/Middlewares/PrintConfigurationPage/Views/PrintConfigurationPageModel.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,26 @@ | ||
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 | ||
{ | ||
public class PrintConfigurationPageModel | ||
{ | ||
public PrintConfigurationPageModel( | ||
IConfiguration configuration, | ||
PrintConfigurationPageOptions options) | ||
{ | ||
if (options is null) | ||
throw new ArgumentNullException(nameof(options)); | ||
|
||
Configuration = configuration; | ||
Title = options.PageTitle; | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
public string Title { get; } | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/EthernaACR/Middlewares/PrintConfigurationPage/Views/_PrintConfigurationPartial.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,15 @@ | ||
@using Microsoft.Extensions.Configuration; | ||
@model IConfiguration | ||
|
||
@if (Model.GetChildren().Any()) | ||
{ | ||
<ul> | ||
@foreach (var configSection in Model.GetChildren()) | ||
{ | ||
<li> | ||
@configSection.Key|@configSection.Value | ||
</li> | ||
<partial name="_PrintConfigurationPartial" model="configSection" /> | ||
} | ||
</ul> | ||
} |
17 changes: 17 additions & 0 deletions
17
src/EthernaACR/Middlewares/PrintConfigurationPage/Views/_ViewImports.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,17 @@ | ||
@* * Copyright 2021-present Etherna Sagl | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*@ | ||
|
||
@namespace Etherna.ACR.Middlewares.PrintConfigurationPage.Views | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |