Skip to content

Commit 9dfeb23

Browse files
committed
Add options
1 parent 4e6c914 commit 9dfeb23

File tree

7 files changed

+205
-16
lines changed

7 files changed

+205
-16
lines changed

DebugViewEndpoint.sln

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1313
.editorconfig = .editorconfig
1414
.gitattributes = .gitattributes
1515
.gitignore = .gitignore
16-
appveyor.yml = appveyor.yml
17-
azure-pipelines.yml = azure-pipelines.yml
1816
build.cake = build.cake
1917
Directory.Build.props = Directory.Build.props
2018
Directory.Build.targets = Directory.Build.targets

README.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@ After:
2626
```csharp
2727
app.UseEndpoints(endpoints =>
2828
{
29-
endpoints.MapConfigurationDebugView("/config");
29+
endpoints.MapConfigurationDebugView("/config", (options) => options.AllowDevelopmentOnly = true);
3030
});
3131
```
3232

33-
## TODO
34-
35-
* core impl
36-
* options to control (?)
37-
* enable for env
38-
* samples based on `dotnet/samples`
33+
For more examples, please see: [tests](.\Tests\ConfigurationDebugViewEndpoint.Test\EndpointRouteBuilderExtensionsTests.cs)
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": ".NET Core Launch (web)",
6+
"type": "coreclr",
7+
"request": "launch",
8+
"preLaunchTask": "build",
9+
"program": "${workspaceFolder}/bin/Debug/net5.0/WebEmpty.dll",
10+
"args": [],
11+
"cwd": "${workspaceFolder}",
12+
"stopAtEntry": false,
13+
"serverReadyAction": {
14+
"action": "openExternally",
15+
"pattern": "\\\\bNow listening on:\\\\s+(https?://\\\\S+)"
16+
},
17+
"env": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
},
20+
"sourceFileMap": {
21+
"/Views": "${workspaceFolder}/Views"
22+
}
23+
},
24+
{
25+
"name": ".NET Core Attach",
26+
"type": "coreclr",
27+
"request": "attach",
28+
"processId": "${command:pickProcess}"
29+
},
30+
{
31+
"name": "Docker .NET Core Launch",
32+
"type": "docker",
33+
"request": "launch",
34+
"preLaunchTask": "docker-run: debug",
35+
"netCore": {
36+
"appProject": "${workspaceFolder}/WebEmpty.csproj"
37+
}
38+
}
39+
]
40+
}
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/WebEmpty.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/WebEmpty.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"${workspaceFolder}/WebEmpty.csproj",
36+
"/property:GenerateFullPaths=true",
37+
"/consoleloggerparameters:NoSummary"
38+
],
39+
"problemMatcher": "$msCompile"
40+
},
41+
{
42+
"type": "docker-build",
43+
"label": "docker-build: debug",
44+
"dependsOn": [
45+
"build"
46+
],
47+
"dockerBuild": {
48+
"tag": "webapinet5:dev",
49+
"target": "base",
50+
"dockerfile": "${workspaceFolder}/Dockerfile",
51+
"context": "${workspaceFolder}",
52+
"pull": true
53+
},
54+
"netCore": {
55+
"appProject": "${workspaceFolder}/WebEmpty.csproj"
56+
}
57+
},
58+
{
59+
"type": "docker-build",
60+
"label": "docker-build: release",
61+
"dependsOn": [
62+
"build"
63+
],
64+
"dockerBuild": {
65+
"tag": "webapinet5:latest",
66+
"dockerfile": "${workspaceFolder}/Dockerfile",
67+
"context": "${workspaceFolder}",
68+
"pull": true
69+
},
70+
"netCore": {
71+
"appProject": "${workspaceFolder}/WebEmpty.csproj"
72+
}
73+
},
74+
{
75+
"type": "docker-run",
76+
"label": "docker-run: debug",
77+
"dependsOn": [
78+
"docker-build: debug"
79+
],
80+
"dockerRun": {},
81+
"netCore": {
82+
"appProject": "${workspaceFolder}/WebEmpty.csproj",
83+
"enableDebugging": true
84+
}
85+
},
86+
{
87+
"type": "docker-run",
88+
"label": "docker-run: release",
89+
"dependsOn": [
90+
"docker-build: release"
91+
],
92+
"dockerRun": {},
93+
"netCore": {
94+
"appProject": "${workspaceFolder}/WebEmpty.csproj"
95+
}
96+
}
97+
]
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace ConfigurationDebugViewEndpoint
2+
{
3+
/// <summary>
4+
/// Contains options for the <see cref="ConfigurationDebugViewMiddleware"/>.
5+
/// </summary>
6+
public class ConfigurationDebugViewOptions
7+
{
8+
/// <summary>
9+
/// Gets or sets a value that controls whether to use <see cref="ConfigurationDebugViewMiddleware"/>
10+
/// exclusively on Development environment.
11+
/// </summary>
12+
public bool AllowDevelopmentOnly { get; set; }
13+
}
14+
}

Source/ConfigurationDebugViewEndpoint/EndpointRouteBuilderExtensions.cs

+20-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ namespace ConfigurationDebugViewEndpoint.Extensions
33
using System;
44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.AspNetCore.Routing;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
68

79
/// <summary>
810
/// Provides extension methods for <see cref="IEndpointRouteBuilder"/> to add routes.
@@ -14,24 +16,36 @@ public static class EndpointRouteBuilderExtensions
1416
/// </summary>
1517
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add endpoint to.</param>
1618
/// <param name="pattern">The URL pattern of the endpoint.</param>
19+
/// <param name="optionsDelegate"></param>
1720
/// <returns>A route for the endpoint.</returns>
18-
public static IEndpointConventionBuilder MapConfigurationDebugView(
21+
public static IEndpointConventionBuilder? MapConfigurationDebugView(
1922
this IEndpointRouteBuilder endpoints,
20-
string pattern = "config")
23+
string pattern = "config",
24+
Action<ConfigurationDebugViewOptions>? optionsDelegate = default)
2125
{
2226
if (endpoints == null)
2327
{
2428
throw new ArgumentNullException(nameof(endpoints));
2529
}
2630

27-
return MapConfigurationDebugViewCore(endpoints, pattern);
31+
var options = new ConfigurationDebugViewOptions();
32+
optionsDelegate?.Invoke(options);
33+
34+
return MapConfigurationDebugViewCore(endpoints, pattern, options);
2835
}
2936

30-
private static IEndpointConventionBuilder MapConfigurationDebugViewCore(
37+
private static IEndpointConventionBuilder? MapConfigurationDebugViewCore(
3138
IEndpointRouteBuilder endpoints,
32-
string pattern)
39+
string pattern, ConfigurationDebugViewOptions options)
3340
{
34-
var pipeline = endpoints.CreateApplicationBuilder()
41+
var environment = endpoints.ServiceProvider.GetRequiredService<IHostEnvironment>();
42+
var builder = endpoints.CreateApplicationBuilder();
43+
44+
if (options.AllowDevelopmentOnly && !environment.IsDevelopment())
45+
{
46+
return null;
47+
}
48+
var pipeline = builder
3549
.UseMiddleware<ConfigurationDebugViewMiddleware>()
3650
.Build();
3751

Tests/ConfigurationDebugViewEndpoint.Test/ConfigurationDebugViewMiddlewareTests.cs Tests/ConfigurationDebugViewEndpoint.Test/EndpointRouteBuilderExtensionsTests.cs

+31-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ namespace ConfigurationDebugViewEndpoint.Test
99
using Microsoft.AspNetCore.Builder;
1010
using Microsoft.AspNetCore.Hosting;
1111
using System;
12+
using ConfigurationDebugViewEndpoint.Extensions;
1213

13-
public class ConfigurationDebugViewMiddlewareTests
14+
public class EndpointRouteBuilderExtensionsTests
1415
{
1516
[Fact]
1617
public async Task EmptyConfiguration()
@@ -51,5 +52,34 @@ public async Task NotEmptyConfiguration_ExpectedConfigurationReturned()
5152
Assert.Contains(settingKey, content);
5253
Assert.Contains(settingValue, content);
5354
}
55+
56+
[Theory]
57+
[InlineData("Development", 200)]
58+
[InlineData("Production", 404)]
59+
public async Task WithAllowDevelopmentOnlyOptions_Development_ConfigurationReturned(
60+
string environment,
61+
int expectedStatusCode)
62+
{
63+
var pattern = "/config";
64+
using var host = new HostBuilder().ConfigureWebHost(builder =>
65+
{
66+
builder.ConfigureServices(services => services.AddRouting());
67+
builder.Configure(app =>
68+
{
69+
app.UseRouting();
70+
app.UseEndpoints(endpoints =>
71+
endpoints.MapConfigurationDebugView(pattern, (options) => options.AllowDevelopmentOnly = true));
72+
});
73+
builder.UseTestServer()
74+
.UseEnvironment(environment);
75+
})
76+
.Build();
77+
78+
await host.StartAsync();
79+
using var server = host.GetTestServer();
80+
var response = await server.CreateClient().GetAsync($"http://example.com{pattern}");
81+
Assert.Equal(expectedStatusCode, (int)response.StatusCode);
82+
}
83+
5484
}
5585
}

0 commit comments

Comments
 (0)