Skip to content

Commit

Permalink
refactor(server): migrate controllers to endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
gingters committed Jul 15, 2024
1 parent 5848500 commit 8e27cca
Show file tree
Hide file tree
Showing 28 changed files with 287 additions and 346 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public partial class RelayTargetRegistry<TRequest, TResponse>
/// <summary>
/// Initializes a new instance of the <see cref="RelayTargetRegistry{TRequest,TResponse}"/> class.
/// </summary>
/// <param name="logger">An <see cref="ILogger{TCategory}"/>.</param>
/// <param name="logger">An <see cref="ILogger{TCategoryName}"/>.</param>
/// <param name="relayTargetOptions">An <see cref="IOptions{TOptions}"/>.</param>
public RelayTargetRegistry(ILogger<RelayTargetRegistry<TRequest, TResponse>> logger,
IOptions<RelayTargetOptions> relayTargetOptions)
Expand Down
16 changes: 8 additions & 8 deletions src/Thinktecture.Relay.Server.Abstractions/IBodyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IBodyStore
/// <summary>
/// Stores the request body stream for the request id.
/// </summary>
/// <param name="requestId">The id of the request.</param>
/// <param name="requestId">The unique id of the request.</param>
/// <param name="bodyStream">The request stream to store.</param>
/// <param name="cancellationToken">
/// The token to monitor for cancellation requests. The default value is
Expand All @@ -25,7 +25,7 @@ public interface IBodyStore
/// <summary>
/// Stores the response body stream for the request id.
/// </summary>
/// <param name="requestId">The id of the request.</param>
/// <param name="requestId">The unique id of the request.</param>
/// <param name="bodyStream">The response stream to store.</param>
/// <param name="cancellationToken">
/// The token to monitor for cancellation requests. The default value is
Expand All @@ -37,7 +37,7 @@ public interface IBodyStore
/// <summary>
/// Opens the request body stream for the request id.
/// </summary>
/// <param name="requestId">The id of the request.</param>
/// <param name="requestId">The unique id of the request.</param>
/// <param name="cancellationToken">
/// The token to monitor for cancellation requests. The default value is
/// <see cref="CancellationToken.None"/>.
Expand All @@ -48,7 +48,7 @@ public interface IBodyStore
/// <summary>
/// Opens the response body stream for the request id.
/// </summary>
/// <param name="requestId">The id of the request.</param>
/// <param name="requestId">The unique id of the request.</param>
/// <param name="cancellationToken">
/// The token to monitor for cancellation requests. The default value is
/// <see cref="CancellationToken.None"/>.
Expand All @@ -59,7 +59,7 @@ public interface IBodyStore
/// <summary>
/// Removes the stored request body.
/// </summary>
/// <param name="requestId">The id of the request.</param>
/// <param name="requestId">The unique id of the request.</param>
/// <param name="cancellationToken">
/// The token to monitor for cancellation requests. The default value is
/// <see cref="CancellationToken.None"/>.
Expand All @@ -70,7 +70,7 @@ public interface IBodyStore
/// <summary>
/// Removes the stored response body.
/// </summary>
/// <param name="requestId">The id of the request.</param>
/// <param name="requestId">The unique id of the request.</param>
/// <param name="cancellationToken">
/// The token to monitor for cancellation requests. The default value is
/// <see cref="CancellationToken.None"/>.
Expand All @@ -81,14 +81,14 @@ public interface IBodyStore
/// <summary>
/// Returns an <see cref="IAsyncDisposable"/> which removes the stored request body when disposed.
/// </summary>
/// <param name="requestId">The id of the request.</param>
/// <param name="requestId">The unique id of the request.</param>
/// <returns>An <see cref="IAsyncDisposable"/>.</returns>
IAsyncDisposable GetRequestBodyRemoveDisposable(Guid requestId);

/// <summary>
/// Returns an <see cref="IAsyncDisposable"/> which removes the stored response body when disposed.
/// </summary>
/// <param name="requestId">The id of the request.</param>
/// <param name="requestId">The unique id of the request.</param>
/// <returns>An <see cref="IAsyncDisposable"/>.</returns>
IAsyncDisposable GetResponseBodyRemoveDisposable(Guid requestId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public partial class ConnectorRegistry<T>
/// <summary>
/// Initializes a new instance of the <see cref="ConnectorRegistry{T}"/> class.
/// </summary>
/// <param name="logger">An <see cref="ILogger{TCategory}"/>.</param>
/// <param name="logger">An <see cref="ILogger{TCategoryName}"/>.</param>
/// <param name="serviceProvider">An <see cref="IServiceProvider"/></param>
/// <param name="connectionStatisticsWriter">An <see cref="IConnectionStatisticsWriter"/>.</param>
/// <param name="relayServerContext">The <see cref="RelayServerContext"/>.</param>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
Expand All @@ -9,51 +8,40 @@

namespace Thinktecture.Relay.Server.Management.Endpoints;

/// <summary>
/// Provides extension methods for the <see cref="IEndpointRouteBuilder"/>.
/// </summary>
public static partial class EndpointRouteBuilderExtensions
{
/// <summary>
/// Maps the endpoint to delete an existing tenant.
/// </summary>
/// <param name="app">The web application to add the endpoint to.</param>
/// <param name="app">An <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="pattern">The url pattern for this endpoint.</param>
/// <param name="policy">Optional; The authorization policy to apply to this endpoint.</param>
/// <returns>The <see cref="RouteHandlerBuilder"/> with the configured endpoint.</returns>
public static RouteHandlerBuilder MapDeleteTenant(this IEndpointRouteBuilder app, string pattern, string? policy)
/// <param name="policy">An optional authorization policy.</param>
/// <returns>The <see cref="IEndpointRouteBuilder"/>.</returns>
public static IEndpointRouteBuilder MapDeleteTenant(this IEndpointRouteBuilder app, string pattern, string? policy)
{
var builder = app
.MapDelete($"{pattern}/{{tenantName}}", DeleteTenantEndpoint.HandleRequestAsync)
.WithName("DeleteTenant")
.WithDisplayName("Delete tenant")
.Produces(StatusCodes.Status204NoContent)
.Produces(StatusCodes.Status404NotFound)
;
.MapDelete($"{pattern}/{{tenantName}}", DeleteTenantEndpoint.HandleRequestAsync)
.WithName("DeleteTenant")
.WithDisplayName("Delete tenant")
.Produces(StatusCodes.Status204NoContent)
.Produces(StatusCodes.Status404NotFound);

if (!String.IsNullOrWhiteSpace(policy))
if (!string.IsNullOrWhiteSpace(policy))
{
builder.RequireAuthorization(policy)
.Produces(StatusCodes.Status401Unauthorized)
.Produces(StatusCodes.Status403Forbidden)
;
.Produces(StatusCodes.Status403Forbidden);
}

return builder;
return app;
}
}

/// <summary>
/// Provides an endpoint handler.
/// </summary>
public static class DeleteTenantEndpoint
internal static class DeleteTenantEndpoint
{
/// <summary>
/// Deletes a tenant from the persistence layer.
/// </summary>
/// <param name="tenantName">The name of the tenant to delete.</param>
/// <param name="service">An instance of an <see cref="ITenantService"/> to access the persistence.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is
/// <see cref="P:System.Threading.CancellationToken.None"/>.
/// </param>
/// <returns>204, if deleted; otherwise, 404.</returns>
public static async Task<IResult> HandleRequestAsync([FromRoute] string tenantName,
[FromServices] ITenantService service, CancellationToken cancellationToken = default
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,40 @@

namespace Thinktecture.Relay.Server.Management.Endpoints;

/// <summary>
/// Provides extension methods for the <see cref="IEndpointRouteBuilder"/>.
/// </summary>
public static partial class EndpointRouteBuilderExtensions
{
/// <summary>
/// Maps the endpoint to retrieve a tenant.
/// Maps the endpoint to get an existing tenant.
/// </summary>
/// <param name="app">The web application to add the endpoint to.</param>
/// <param name="app">An <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="pattern">The url pattern for this endpoint.</param>
/// <param name="policy">Optional; The authorization policy to apply to this endpoint.</param>
/// <returns>The <see cref="RouteHandlerBuilder"/> with the configured endpoint.</returns>
public static RouteHandlerBuilder MapGetTenant(this IEndpointRouteBuilder app, string pattern, string? policy)
/// <param name="policy">An optional authorization policy.</param>
/// <returns>The <see cref="IEndpointRouteBuilder"/>.</returns>
public static IEndpointRouteBuilder MapGetTenant(this IEndpointRouteBuilder app, string pattern, string? policy)
{
var builder = app
.MapGet($"{pattern}/{{tenantName}}", GetTenantEndpoint.HandleRequestAsync)
.WithName("GetSingleTenant")
.WithDisplayName("Get single tenant")
.Produces<Tenant>()
.Produces(StatusCodes.Status404NotFound)
;
.MapGet($"{pattern}/{{tenantName}}", GetTenantEndpoint.HandleRequestAsync)
.WithName("GetSingleTenant")
.WithDisplayName("Get single tenant")
.Produces<Tenant>()
.Produces(StatusCodes.Status404NotFound);

if (!String.IsNullOrWhiteSpace(policy))
{
builder.RequireAuthorization(policy)
.Produces(StatusCodes.Status401Unauthorized)
.Produces(StatusCodes.Status403Forbidden)
;
.Produces(StatusCodes.Status403Forbidden);
}

return builder;
return app;
}
}

/// <summary>
/// Provides an endpoint handler.
/// </summary>
public static class GetTenantEndpoint
internal static class GetTenantEndpoint
{
/// <summary>
/// Retrieves a single tenant.
/// </summary>
/// <param name="tenantName">The name of the tenant to load.</param>
/// <param name="service">An instance of an <see cref="ITenantService"/> to access the persistence.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is
/// <see cref="P:System.Threading.CancellationToken.None"/>.
/// </param>
/// <returns>The tenant, if found; otherwise, 404.</returns>
public static async Task<IResult> HandleRequestAsync([FromRoute] string tenantName,
[FromServices] ITenantService service, CancellationToken cancellationToken = default
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
Expand All @@ -13,58 +12,41 @@
namespace Thinktecture.Relay.Server.Management.Endpoints;

/// <summary>
/// Provides extensions methods for the <see cref="IEndpointRouteBuilder"/>.
/// Provides extension methods for the <see cref="IEndpointRouteBuilder"/>.
/// </summary>
public static partial class EndpointRouteBuilderExtensions
{
/// <summary>
/// Maps the endpoint to retrieve the tenants.
/// Maps the endpoint to get the existing tenants page-wise.
/// </summary>
/// <param name="app">The web application to add the endpoint to.</param>
/// <param name="app">An <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="pattern">The url pattern for this endpoint.</param>
/// <param name="policy">Optional; The authorization policy to apply to this endpoint.</param>
/// <returns>The <see cref="RouteHandlerBuilder"/> with the configured endpoint.</returns>
public static RouteHandlerBuilder MapGetTenantsPaged(this IEndpointRouteBuilder app, string pattern, string? policy = null)
/// <param name="policy">An optional authorization policy.</param>
/// <returns>The <see cref="IEndpointRouteBuilder"/>.</returns>
public static IEndpointRouteBuilder MapGetTenantsPaged(this IEndpointRouteBuilder app, string pattern,
string? policy = null)
{
var builder = app
.MapGet(pattern, GetTenantsPagedEndpoint.HandleRequestAsync)
.WithName("GetTenantsPaged")
.WithDisplayName("Get tenants paged")
.Produces<Page<Tenant>>()
;
.MapGet(pattern, GetTenantsPagedEndpoint.HandleRequestAsync)
.WithName("GetTenantsPaged")
.WithDisplayName("Get tenants paged")
.Produces<Page<Tenant>>();

if (!String.IsNullOrWhiteSpace(policy))
if (!string.IsNullOrWhiteSpace(policy))
{
builder.RequireAuthorization(policy)
.Produces(StatusCodes.Status401Unauthorized)
.Produces(StatusCodes.Status403Forbidden)
;
.Produces(StatusCodes.Status403Forbidden);
}

return builder;
return app;
}
}

/// <summary>
/// Provides an endpoint handler.
/// </summary>
public static class GetTenantsPagedEndpoint
internal static class GetTenantsPagedEndpoint
{
/// <summary>
/// Handles the endpoint to retrieve the tenants.
/// </summary>
/// <param name="service">An instance of an <see cref="ITenantService"/> to access the persistence layer.</param>
/// <param name="skip">The amount of tenants to skip while retrieving.</param>
/// <param name="take">The amount of tenants to fetch per page. Defaults to 10.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is
/// <see cref="P:System.Threading.CancellationToken.None"/>.
/// </param>
/// <returns>A page containing the requested tenants.</returns>
public static async Task<Page<Tenant>> HandleRequestAsync(
[FromServices] ITenantService service,
[FromQuery] int skip = 0,
[FromQuery] int take = 10,
CancellationToken cancellationToken = default
public static async Task<Page<Tenant>> HandleRequestAsync([FromServices] ITenantService service,
[FromQuery] int skip = 0, [FromQuery] int take = 10, CancellationToken cancellationToken = default
)
=> (await service.LoadAllTenantsPagedAsync(skip, take, cancellationToken)).ToModel();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
Expand All @@ -11,52 +10,40 @@

namespace Thinktecture.Relay.Server.Management.Endpoints;

/// <summary>
/// Provides extension methods for the <see cref="IEndpointRouteBuilder"/>.
/// </summary>
public static partial class EndpointRouteBuilderExtensions
{
/// <summary>
/// Maps the endpoint to add a new tenant.
/// </summary>
/// <param name="app">The web application to add the endpoint to.</param>
/// <param name="app">An <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="pattern">The url pattern for this endpoint.</param>
/// <param name="policy">Optional; The authorization policy to apply to this endpoint.</param>
/// <returns>The <see cref="RouteHandlerBuilder"/> with the configured endpoint.</returns>
public static RouteHandlerBuilder MapPostTenant(this IEndpointRouteBuilder app, string pattern, string? policy)
/// <param name="policy">An optional authorization policy.</param>
/// <returns>The <see cref="IEndpointRouteBuilder"/>.</returns>
public static IEndpointRouteBuilder MapPostTenant(this IEndpointRouteBuilder app, string pattern, string? policy)
{
var builder = app
.MapPost($"{pattern}", PostTenantEndpoint.HandleRequestAsync)
.WithName("PostTenant")
.WithDisplayName("Post tenant")
.Produces(StatusCodes.Status201Created, typeof(IdResult))
.Produces(StatusCodes.Status404NotFound)
;
.Produces(StatusCodes.Status404NotFound);

if (!String.IsNullOrWhiteSpace(policy))
if (!string.IsNullOrWhiteSpace(policy))
{
builder.RequireAuthorization(policy)
.Produces(StatusCodes.Status401Unauthorized)
.Produces(StatusCodes.Status403Forbidden)
;
.Produces(StatusCodes.Status403Forbidden);
}

return builder;
return app;
}
}

/// <summary>
/// Provides an endpoint handler.
/// </summary>
public static class PostTenantEndpoint
internal static class PostTenantEndpoint
{
/// <summary>
/// Stores a tenant to the persistence layer.
/// </summary>
/// <param name="service">An instance of an <see cref="ITenantService"/> to access the persistence.</param>
/// <param name="tenant">The tenant to save.</param>
/// <param name="request">An instance of an <see cref="HttpRequest"/>.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is
/// <see cref="P:System.Threading.CancellationToken.None"/>.
/// </param>
/// <returns>202, if created.</returns>
public static async Task<IResult> HandleRequestAsync([FromBody] Tenant tenant, [FromServices] ITenantService service,
HttpRequest request, CancellationToken cancellationToken = default)
{
Expand Down
Loading

0 comments on commit 8e27cca

Please sign in to comment.