-
Notifications
You must be signed in to change notification settings - Fork 251
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
10 changed files
with
150 additions
and
9 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
14 changes: 14 additions & 0 deletions
14
Management/src/ActuatorWeb/CustomActuators/LocalTime/ConfigureLocalTimeEndpointOptions.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 @@ | ||
using Steeltoe.Management.Endpoint.Configuration; | ||
|
||
namespace Steeltoe.Samples.ActuatorWeb.CustomActuators.LocalTime; | ||
|
||
internal sealed class ConfigureLocalTimeEndpointOptions(IConfiguration configuration) | ||
: ConfigureEndpointOptions<LocalTimeEndpointOptions>(configuration, "Management:Endpoints:LocalTime", "local-time") | ||
{ | ||
public override void Configure(LocalTimeEndpointOptions options) | ||
{ | ||
base.Configure(options); | ||
|
||
options.Format ??= "O"; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Management/src/ActuatorWeb/CustomActuators/LocalTime/EndpointServiceCollectionExtensions.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,43 @@ | ||
using Steeltoe.Management.Endpoint; | ||
|
||
namespace Steeltoe.Samples.ActuatorWeb.CustomActuators.LocalTime; | ||
|
||
public static class EndpointServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the local time actuator to the service container and configures the ASP.NET middleware pipeline. | ||
/// </summary> | ||
/// <param name="services"> | ||
/// The <see cref="IServiceCollection" /> to add services to. | ||
/// </param> | ||
/// <returns> | ||
/// The incoming <paramref name="services" /> so that additional calls can be chained. | ||
/// </returns> | ||
public static IServiceCollection AddLocalTimeActuator(this IServiceCollection services) | ||
{ | ||
return services.AddLocalTimeActuator(true); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the local time actuator to the service container. | ||
/// </summary> | ||
/// <param name="services"> | ||
/// The <see cref="IServiceCollection" /> to add services to. | ||
/// </param> | ||
/// <param name="configureMiddleware"> | ||
/// When <c>false</c>, skips configuration of the ASP.NET middleware pipeline. While this provides full control over the pipeline order, it requires | ||
/// manual addition of the appropriate middleware for actuators to work correctly. | ||
/// </param> | ||
/// <returns> | ||
/// The incoming <paramref name="services" /> so that additional calls can be chained. | ||
/// </returns> | ||
public static IServiceCollection AddLocalTimeActuator(this IServiceCollection services, bool configureMiddleware) | ||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
|
||
services.AddCoreActuatorServices<LocalTimeEndpointOptions, ConfigureLocalTimeEndpointOptions, LocalTimeEndpointMiddleware, ILocalTimeEndpointHandler, | ||
LocalTimeEndpointHandler, object?, string>(configureMiddleware); | ||
|
||
return services; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
Management/src/ActuatorWeb/CustomActuators/LocalTime/ILocalTimeEndpointHandler.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,5 @@ | ||
using Steeltoe.Management.Endpoint; | ||
|
||
namespace Steeltoe.Samples.ActuatorWeb.CustomActuators.LocalTime; | ||
|
||
public interface ILocalTimeEndpointHandler : IEndpointHandler<object?, string>; |
30 changes: 30 additions & 0 deletions
30
Management/src/ActuatorWeb/CustomActuators/LocalTime/LocalTimeEndpointHandler.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,30 @@ | ||
using System.Globalization; | ||
using Microsoft.Extensions.Options; | ||
using Steeltoe.Management.Configuration; | ||
|
||
namespace Steeltoe.Samples.ActuatorWeb.CustomActuators.LocalTime; | ||
|
||
internal sealed class LocalTimeEndpointHandler : ILocalTimeEndpointHandler | ||
{ | ||
private readonly IOptionsMonitor<LocalTimeEndpointOptions> _optionsMonitor; | ||
|
||
public EndpointOptions Options => _optionsMonitor.CurrentValue; | ||
|
||
public LocalTimeEndpointHandler(IOptionsMonitor<LocalTimeEndpointOptions> optionsMonitor) | ||
{ | ||
ArgumentNullException.ThrowIfNull(optionsMonitor); | ||
|
||
_optionsMonitor = optionsMonitor; | ||
} | ||
|
||
public Task<string> InvokeAsync(object? argument, CancellationToken cancellationToken) | ||
{ | ||
string localTime = GetLocalTime(); | ||
return Task.FromResult(localTime); | ||
} | ||
|
||
private string GetLocalTime() | ||
{ | ||
return DateTime.Now.ToString(_optionsMonitor.CurrentValue.Format, CultureInfo.InvariantCulture); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Management/src/ActuatorWeb/CustomActuators/LocalTime/LocalTimeEndpointMiddleware.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,15 @@ | ||
using Microsoft.Extensions.Options; | ||
using Steeltoe.Management.Endpoint.Configuration; | ||
using Steeltoe.Management.Endpoint.Middleware; | ||
|
||
namespace Steeltoe.Samples.ActuatorWeb.CustomActuators.LocalTime; | ||
|
||
internal sealed class LocalTimeEndpointMiddleware( | ||
ILocalTimeEndpointHandler endpointHandler, IOptionsMonitor<ManagementOptions> managementOptionsMonitor, ILoggerFactory loggerFactory) | ||
: EndpointMiddleware<object?, string>(endpointHandler, managementOptionsMonitor, loggerFactory) | ||
{ | ||
protected override async Task<string> InvokeEndpointHandlerAsync(HttpContext context, CancellationToken cancellationToken) | ||
{ | ||
return await EndpointHandler.InvokeAsync(null, cancellationToken); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Management/src/ActuatorWeb/CustomActuators/LocalTime/LocalTimeEndpointOptions.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,15 @@ | ||
using Steeltoe.Management.Configuration; | ||
|
||
namespace Steeltoe.Samples.ActuatorWeb.CustomActuators.LocalTime; | ||
|
||
public sealed class LocalTimeEndpointOptions : EndpointOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets the date/time format to use in the response. Defaults to "O". | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://learn.microsoft.com/dotnet/standard/base-types/standard-date-and-time-format-strings" /> and | ||
/// <see href="https://learn.microsoft.com/dotnet/standard/base-types/custom-date-and-time-format-strings" /> for possible values. | ||
/// </remarks> | ||
public string? Format { get; set; } | ||
} |
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
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