Skip to content

Commit

Permalink
Merge c300369 into d222fc2
Browse files Browse the repository at this point in the history
  • Loading branch information
idg10 authored May 7, 2021
2 parents d222fc2 + c300369 commit 445cb8a
Show file tree
Hide file tree
Showing 35 changed files with 1,035 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
<PackageDescription>Menes is a framework for hosting Web APIs with OpenAPI-based service definitions. This library defines the ASP.NET-Core-specific aspects of hosting in Menes.</PackageDescription>

<WarningsAsErrors />

<!-- Don't want ConfigureAwait warnings because ASP.NET Core doesn't use synchronization context, so such calls are overhead with no purpose. -->
<NoWarn>$(NoWarn);RCS1090</NoWarn>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Menes.Specs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Endjin.RecommendedPractices" Version="1.2.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <copyright file="HttpContextExtensions.cs" company="Endjin Limited">
// Copyright (c) Endjin Limited. All rights reserved.
// </copyright>

namespace Menes.Hosting.AspNetCore
{
using System.Threading.Tasks;

using Menes.Internal;

using Microsoft.AspNetCore.Http;

/// <summary>
/// Extension methods for HttpContext.
/// </summary>
public static class HttpContextExtensions
{
/// <summary>
/// Uses the <see cref="IOpenApiHost{HttpRequest, IActionResult}"/> to handle the request.
/// </summary>
/// <param name="host">The host to handle the request.</param>
/// <param name="httpContext">The context for the request to handle.</param>
/// <param name="parameters">Any dynamically constructed parameters sent to the request.</param>
/// <returns>The result of the request.</returns>
public static async Task HandleRequestAsync(
this IOpenApiHost<HttpRequest, IHttpResponseResult> host, HttpContext httpContext, object parameters)
{
HttpRequest httpRequest = httpContext.Request;
IHttpResponseResult result = await host.HandleRequestAsync(httpRequest.Path, httpRequest.Method, httpRequest, parameters);
await result.ExecuteResultAsync(httpContext.Response);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// <copyright file="MenesCatchAllMiddleware.cs" company="Endjin Limited">
// Copyright (c) Endjin Limited. All rights reserved.
// </copyright>

namespace Menes.Hosting.AspNetCore
{
using System.Threading.Tasks;

using Menes.Internal;

using Microsoft.AspNetCore.Http;

/// <summary>
/// Middleware that passes all requests on to Menes. This never forwards requests further down the pipeline.
/// </summary>
internal class MenesCatchAllMiddleware : IMiddleware
{
private static readonly object EmptyParameters = new ();
private readonly IOpenApiHost<HttpRequest, IHttpResponseResult> host;

/// <summary>
/// Creates a <see cref="MenesCatchAllMiddleware"/>.
/// </summary>
/// <param name="host">The Menes host.</param>
public MenesCatchAllMiddleware(IOpenApiHost<HttpRequest, IHttpResponseResult> host)
{
this.host = host;
}

/// <inheritdoc/>
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
await this.host.HandleRequestAsync(context, EmptyParameters);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// <copyright file="OpenApiAspNetApplicationBuilderExtensions.cs" company="Endjin Limited">
// Copyright (c) Endjin Limited. All rights reserved.
// </copyright>

namespace Menes.Hosting.AspNetCore
{
using Microsoft.AspNetCore.Builder;

/// <summary>
/// Extension methods for adding Menes to an ASP.NET Core pipeline.
/// </summary>
public static class OpenApiAspNetApplicationBuilderExtensions
{
/// <summary>
/// Adds middleware that directs all requests to Menes.
/// </summary>
/// <param name="app">The pipeline builder.</param>
/// <returns>The modified pipeline builder.</returns>
public static IApplicationBuilder UseMenesCatchAll(this IApplicationBuilder app)
{
return app.UseMiddleware<MenesCatchAllMiddleware>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// <copyright file="IHttpResponseResult.cs" company="Endjin Limited">
// Copyright (c) Endjin Limited. All rights reserved.
// </copyright>

namespace Menes.Internal
{
using System.Threading.Tasks;

using Microsoft.AspNetCore.Http;

/// <summary>
/// Populates an <see cref="HttpResponse"/> with the outcome (or failure) of an operation.
/// </summary>
public interface IHttpResponseResult
{
/// <summary>
/// Populates the response.
/// </summary>
/// <param name="httpResponse">The response to populate.</param>
/// <returns>A task that completes when the response has been populated.</returns>
Task ExecuteResultAsync(HttpResponse httpResponse);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
// <copyright file="IActionResultOutputBuilder.cs" company="Endjin Limited">
// <copyright file="IResponseOutputBuilder.cs" company="Endjin Limited">
// Copyright (c) Endjin Limited. All rights reserved.
// </copyright>

namespace Menes.Internal
{
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;

/// <summary>
/// Interface implemented by types that are used to build <see cref="IActionResult"/> based output.
/// Interface implemented by types that are used to build outputs to be used with the Menes
/// host.
/// </summary>
/// <remarks>
/// <para>
/// This can be used by any host that implements the AspNetCore 2.x model, such as
/// Functions V2 and AspNetCore itself.
/// </para>
/// </remarks>
public interface IActionResultOutputBuilder
/// <typeparam name="TResponse">
/// The response type. Corresponds to the 2nd type argument in
/// <see cref="IOpenApiHost{TRequest, TResponse}"/>.
/// </typeparam>
public interface IResponseOutputBuilder<TResponse>
{
/// <summary>
/// Gets the priority of the output builder.
Expand All @@ -41,7 +39,7 @@ public interface IActionResultOutputBuilder
/// </summary>
/// <param name="result">The result.</param>
/// <param name="operation">The operation.</param>
/// <returns>The <see cref="IActionResult"/> constructed from the operation and result.</returns>
IActionResult BuildOutput(object result, OpenApiOperation operation);
/// <returns>The <typeparamref name="TResponse"/> constructed from the operation and result.</returns>
TResponse BuildOutput(object result, OpenApiOperation operation);
}
}
}
Loading

0 comments on commit 445cb8a

Please sign in to comment.