This repository was archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 522
Implement configuration support #2186
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -3,13 +3,15 @@ | |
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Net; | ||
using System.Security.Authentication; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Server.Kestrel.Core; | ||
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
@@ -48,10 +50,31 @@ public static Task Main(string[] args) | |
{ | ||
factory.AddConsole(); | ||
}) | ||
.ConfigureAppConfiguration((hostingContext, config) => | ||
{ | ||
var env = hostingContext.HostingEnvironment; | ||
config.AddJsonFile("appsettings.json", optional: true) | ||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least one of these appsettings files is required now, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, they're optional in the sample. The config is additive on top of the endpoints defined in code. |
||
}) | ||
.UseKestrel((context, options) => | ||
{ | ||
if (context.HostingEnvironment.IsDevelopment()) | ||
{ | ||
ShowConfig(context.Configuration); | ||
} | ||
|
||
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000; | ||
|
||
options.ConfigureEndpointDefaults(opt => | ||
{ | ||
opt.Protocols = HttpProtocols.Http1; | ||
}); | ||
|
||
options.ConfigureHttpsDefaults(httpsOptions => | ||
{ | ||
httpsOptions.SslProtocols = SslProtocols.Tls12; | ||
}); | ||
|
||
// Run callbacks on the transport thread | ||
options.ApplicationSchedulingMode = SchedulingMode.Inline; | ||
|
||
|
@@ -71,11 +94,34 @@ public static Task Main(string[] args) | |
|
||
options.ListenLocalhost(basePort + 2, listenOptions => | ||
{ | ||
listenOptions.UseHttps("testCert.pfx", "testPassword"); | ||
// Use default dev cert | ||
listenOptions.UseHttps(); | ||
}); | ||
|
||
options.ListenAnyIP(basePort + 3); | ||
|
||
options.ListenAnyIP(basePort + 4, listenOptions => | ||
{ | ||
listenOptions.UseHttps(StoreName.My, "aspnet.test", allowInvalid: true); | ||
}); | ||
|
||
options | ||
.Configure() | ||
.Endpoint(IPAddress.Loopback, basePort + 5) | ||
.LocalhostEndpoint(basePort + 6) | ||
.Load(); | ||
|
||
options | ||
.Configure(context.Configuration.GetSection("Kestrel")) | ||
.Endpoint("NamedEndpoint", opt => | ||
{ | ||
opt.ListenOptions.Protocols = HttpProtocols.Http1; | ||
}) | ||
.Endpoint("NamedHttpsEndpoint", opt => | ||
{ | ||
opt.HttpsOptions.SslProtocols = SslProtocols.Tls12; | ||
}); | ||
|
||
options.UseSystemd(); | ||
|
||
// The following section should be used to demo sockets | ||
|
@@ -96,5 +142,14 @@ public static Task Main(string[] args) | |
|
||
return hostBuilder.Build().RunAsync(); | ||
} | ||
|
||
private static void ShowConfig(IConfiguration config) | ||
{ | ||
foreach (var pair in config.GetChildren()) | ||
{ | ||
Console.WriteLine($"{pair.Path} - {pair.Value}"); | ||
ShowConfig(pair); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
{ | ||
"Kestrel": { | ||
"Endpoints": { | ||
"NamedEndpoint": { "Url": "http://localhost:6000" }, | ||
"NamedHttpsEndpoint": { | ||
"Url": "https://localhost:6443", | ||
"Certificate": { | ||
"Subject": "aspnet.test", | ||
"Store": "My", | ||
"AllowInvalid": true | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
{ | ||
"Kestrel": { | ||
"Endpoints": { | ||
"NamedEndpoint": { "Url": "http://*:6000" }, | ||
"NamedHttpsEndpoint": { | ||
"Url": "https://*:6443", | ||
"Certificate": { | ||
"Path": "testCert.pfx", | ||
"Password": "testPassword" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,6 @@ | ||
{ | ||
"Kestrel": { | ||
"Endpoints": { | ||
} | ||
} | ||
} |
This file contains hidden or 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,92 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace SystemdTestApp | ||
{ | ||
public class Startup | ||
{ | ||
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) | ||
{ | ||
var logger = loggerFactory.CreateLogger("Default"); | ||
|
||
app.Run(async context => | ||
{ | ||
var connectionFeature = context.Connection; | ||
logger.LogDebug($"Peer: {connectionFeature.RemoteIpAddress?.ToString()}:{connectionFeature.RemotePort}" | ||
+ $"{Environment.NewLine}" | ||
+ $"Sock: {connectionFeature.LocalIpAddress?.ToString()}:{connectionFeature.LocalPort}"); | ||
|
||
var response = $"hello, world{Environment.NewLine}"; | ||
context.Response.ContentLength = response.Length; | ||
context.Response.ContentType = "text/plain"; | ||
await context.Response.WriteAsync(response); | ||
}); | ||
} | ||
|
||
public static Task Main(string[] args) | ||
{ | ||
TaskScheduler.UnobservedTaskException += (sender, e) => | ||
{ | ||
Console.WriteLine("Unobserved exception: {0}", e.Exception); | ||
}; | ||
|
||
var hostBuilder = new WebHostBuilder() | ||
.ConfigureLogging((_, factory) => | ||
{ | ||
factory.AddConsole(); | ||
}) | ||
.UseKestrel((context, options) => | ||
{ | ||
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000; | ||
|
||
// Run callbacks on the transport thread | ||
options.ApplicationSchedulingMode = SchedulingMode.Inline; | ||
|
||
options.Listen(IPAddress.Loopback, basePort, listenOptions => | ||
{ | ||
// Uncomment the following to enable Nagle's algorithm for this endpoint. | ||
//listenOptions.NoDelay = false; | ||
|
||
listenOptions.UseConnectionLogging(); | ||
}); | ||
|
||
options.Listen(IPAddress.Loopback, basePort + 1, listenOptions => | ||
{ | ||
listenOptions.UseHttps("testCert.pfx", "testPassword"); | ||
listenOptions.UseConnectionLogging(); | ||
}); | ||
|
||
options.UseSystemd(); | ||
|
||
// The following section should be used to demo sockets | ||
//options.ListenUnixSocket("/tmp/kestrel-test.sock"); | ||
}) | ||
.UseContentRoot(Directory.GetCurrentDirectory()) | ||
.UseStartup<Startup>(); | ||
|
||
if (string.Equals(Process.GetCurrentProcess().Id.ToString(), Environment.GetEnvironmentVariable("LISTEN_PID"))) | ||
{ | ||
// Use libuv if activated by systemd, since that's currently the only transport that supports being passed a socket handle. | ||
hostBuilder.UseLibuv(options => | ||
{ | ||
// Uncomment the following line to change the default number of libuv threads for all endpoints. | ||
// options.ThreadCount = 4; | ||
}); | ||
} | ||
|
||
return hostBuilder.Build().RunAsync(); | ||
} | ||
} | ||
} |
This file contains hidden or 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,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp2.1;netcoreapp2.0;net461</TargetFrameworks> | ||
<IsPackable>false</IsPackable> | ||
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Kestrel\Kestrel.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsolePackageVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="testCert.pfx"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
</Project> |
Binary file not shown.
This file contains hidden or 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 hidden or 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 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNetCore.Server.Kestrel.Core; | ||
using Microsoft.AspNetCore.Server.Kestrel.Https; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel | ||
{ | ||
public class EndpointConfiguration | ||
{ | ||
internal EndpointConfiguration(bool isHttps, ListenOptions listenOptions, HttpsConnectionAdapterOptions httpsOptions, IConfigurationSection configSection) | ||
{ | ||
IsHttps = isHttps; | ||
ListenOptions = listenOptions ?? throw new ArgumentNullException(nameof(listenOptions)); | ||
HttpsOptions = httpsOptions ?? throw new ArgumentNullException(nameof(httpsOptions)); | ||
ConfigSection = configSection ?? throw new ArgumentNullException(nameof(configSection)); | ||
} | ||
|
||
public bool IsHttps { get; } | ||
public ListenOptions ListenOptions { get; } | ||
public HttpsConnectionAdapterOptions HttpsOptions { get; } | ||
public IConfigurationSection ConfigSection { get; } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍