-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#173: Deprecating endpoint mapping in favor of plain middleware #174
Closed
ch-ti8m-michalpenka
wants to merge
1
commit into
asyncapi:main
from
ch-ti8m-michalpenka:bugfix/173-deprecate-endpoint-mapping-in-favor-of-plan-middleware
Closed
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 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,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.
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.
This could be cached instead of serialising every time
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.
My apologies but that's what's been used all the time. That's not what this PR is about. It's about enabling others to use plain middlewares in order to be less limited.
That being said, thanks for your review.