forked from asyncapi/saunter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
asyncapi#173: Deprecating endpoint mapping in favor of plain middleware
- Loading branch information
1 parent
02d631d
commit 26a4e89
Showing
6 changed files
with
206 additions
and
161 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
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.AspNetCore.Builder; | ||
using Saunter.UI; | ||
|
||
namespace Saunter; | ||
|
||
public static class ApplicationBuilderExtensions | ||
{ | ||
public static IApplicationBuilder UseAsyncApi(this IApplicationBuilder applicationBuilder) | ||
{ | ||
applicationBuilder.UseMiddleware<AsyncApiMiddleware>(); | ||
applicationBuilder.UseMiddleware<AsyncApiUiMiddleware>(); | ||
|
||
return applicationBuilder; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Saunter; | ||
|
||
internal static class HttpContextExtensions | ||
{ | ||
internal const string UriDocumentPlaceholder = "{document}"; | ||
private const string UriDocumentPlaceholderEncoded = "%7Bdocument%7D"; | ||
private const string UriDocumentFile = "/asyncapi.json"; | ||
|
||
public static bool TryGetDocument(this HttpContext context, AsyncApiOptions options, out string documentName) | ||
{ | ||
foreach (var documentNameSpecified in options.NamedApis.Values.Select(x => x.DocumentName)) | ||
{ | ||
var pathStart = options.Middleware.Route | ||
.Replace(UriDocumentPlaceholder, documentNameSpecified) | ||
.Replace(UriDocumentFile, string.Empty); | ||
|
||
if (!HttpMethods.IsGet(context.Request.Method) | ||
|| !context.Request.Path.StartsWithSegments(pathStart)) | ||
{ | ||
continue; | ||
} | ||
|
||
documentName = documentNameSpecified; | ||
return true; | ||
} | ||
|
||
documentName = string.Empty; | ||
return false; | ||
} | ||
|
||
public static bool IsRequestingUiBase(this HttpContext context, AsyncApiOptions options) | ||
{ | ||
var uiBaseRoute = options.Middleware.UiBaseRoute; | ||
return IsRequestingAsyncApiUrl(context, options, uiBaseRoute) | ||
|| IsRequestingAsyncApiUrl(context, options, uiBaseRoute.TrimEnd('/')); | ||
} | ||
|
||
public static bool IsRequestingAsyncApiUi(this HttpContext context, AsyncApiOptions options) | ||
{ | ||
var uiIndexRoute = options.Middleware.UiBaseRoute?.TrimEnd('/') + "/index.html"; | ||
return context.IsRequestingAsyncApiUrl(options, uiIndexRoute); | ||
} | ||
|
||
public static bool IsRequestingAsyncApiDocument(this HttpContext context, AsyncApiOptions options) | ||
{ | ||
var uiIndexRoute = options.Middleware.Route; | ||
return context.IsRequestingAsyncApiUrl(options, uiIndexRoute); | ||
} | ||
|
||
public static string GetAsyncApiUiIndexFullRoute(this HttpContext context, AsyncApiOptions options) | ||
{ | ||
var uiIndexRoute = options.Middleware.UiBaseRoute?.TrimEnd('/') + "/index.html"; | ||
return context.GetFullRoute(options, uiIndexRoute); | ||
} | ||
|
||
public static string GetAsyncApiDocumentFullRoute(this HttpContext context, AsyncApiOptions options) | ||
{ | ||
var documentRoute = options.Middleware.Route; | ||
return context.GetFullRoute(options, documentRoute); | ||
} | ||
|
||
private static bool IsRequestingAsyncApiUrl(this HttpContext context, AsyncApiOptions options, string asyncApiBaseRoute) | ||
{ | ||
if (context.TryGetDocument(options, out var documentName)) | ||
{ | ||
asyncApiBaseRoute = asyncApiBaseRoute.Replace(UriDocumentPlaceholder, documentName); | ||
} | ||
|
||
return HttpMethods.IsGet(context.Request.Method) && context.Request.Path.Equals(asyncApiBaseRoute); | ||
} | ||
|
||
private static string GetFullRoute(this HttpContext context, AsyncApiOptions options, string route) | ||
{ | ||
if (context.TryGetDocument(options, out var documentName)) | ||
{ | ||
route = route | ||
.Replace(UriDocumentPlaceholder, documentName) | ||
.Replace(UriDocumentPlaceholderEncoded, documentName); | ||
} | ||
|
||
return context.Request.PathBase.Add(route); | ||
} | ||
} |
Oops, something went wrong.