-
Notifications
You must be signed in to change notification settings - Fork 7
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
22 changed files
with
581 additions
and
234 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
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,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("SimpleEndpoints.Tests")] |
50 changes: 50 additions & 0 deletions
50
src/SimpleEndPoints/Conventions/ApiDescriptionGroupCollectionProviderDecorator.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,50 @@ | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using SimpleEndpoints.VerbScoped; | ||
|
||
namespace SimpleEndpoints.Conventions | ||
{ | ||
internal class ApiDescriptionGroupCollectionProviderDecorator : IApiDescriptionGroupCollectionProvider | ||
{ | ||
private readonly IApiDescriptionGroupCollectionProvider _inner; | ||
|
||
public ApiDescriptionGroupCollectionProviderDecorator(IApiDescriptionGroupCollectionProvider inner) | ||
{ | ||
_inner = inner; | ||
} | ||
|
||
public ApiDescriptionGroupCollection ApiDescriptionGroups | ||
{ | ||
get | ||
{ | ||
foreach (var apiDescriptionGroup in _inner.ApiDescriptionGroups.Items) | ||
{ | ||
foreach (var apiDescription in apiDescriptionGroup.Items) | ||
{ | ||
if (apiDescription.ActionDescriptor is ControllerActionDescriptor controller) | ||
{ | ||
if (typeof(IDeleteEndpoint).IsAssignableFrom(controller.ControllerTypeInfo)) | ||
{ | ||
apiDescription.HttpMethod = "DELETE"; | ||
} | ||
if (typeof(IGetEndpoint).IsAssignableFrom(controller.ControllerTypeInfo)) | ||
{ | ||
apiDescription.HttpMethod = "GET"; | ||
} | ||
if (typeof(IPostEndpoint).IsAssignableFrom(controller.ControllerTypeInfo)) | ||
{ | ||
apiDescription.HttpMethod = "POST"; | ||
} | ||
if (typeof(IPutEndpoint).IsAssignableFrom(controller.ControllerTypeInfo)) | ||
{ | ||
apiDescription.HttpMethod = "PUT"; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return _inner.ApiDescriptionGroups; | ||
} | ||
} | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
src/SimpleEndPoints/Conventions/HttpMethodMetadataMutator.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,32 @@ | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc.ApplicationModels; | ||
using Microsoft.AspNetCore.Routing; | ||
using SimpleEndpoints.VerbScoped; | ||
|
||
namespace SimpleEndpoints.Conventions | ||
{ | ||
internal class HttpMethodMetadataMutator : IConventionMutator | ||
{ | ||
public void Mutate(ControllerModel controller, SimpleEndpointsConfiguration configuration) | ||
{ | ||
var controllerInterfaces = controller.ControllerType.ImplementedInterfaces.ToArray(); | ||
|
||
if (controllerInterfaces.Contains(typeof(IDeleteEndpoint))) | ||
{ | ||
controller.Selectors[0].EndpointMetadata.Add(new HttpMethodMetadata(new[] {"DELETE"})); | ||
} | ||
else if (controllerInterfaces.Contains(typeof(IGetEndpoint))) | ||
{ | ||
controller.Selectors[0].EndpointMetadata.Add(new HttpMethodMetadata(new[] {"GET"})); | ||
} | ||
else if (controllerInterfaces.Contains(typeof(IPostEndpoint))) | ||
{ | ||
controller.Selectors[0].EndpointMetadata.Add(new HttpMethodMetadata(new[] {"POST"})); | ||
} | ||
else if (controllerInterfaces.Contains(typeof(IPutEndpoint))) | ||
{ | ||
controller.Selectors[0].EndpointMetadata.Add(new HttpMethodMetadata(new[] {"PUT"})); | ||
} | ||
} | ||
} | ||
} |
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,9 @@ | ||
using Microsoft.AspNetCore.Mvc.ApplicationModels; | ||
|
||
namespace SimpleEndpoints.Conventions | ||
{ | ||
internal interface IConventionMutator | ||
{ | ||
void Mutate(ControllerModel controller, SimpleEndpointsConfiguration configuration); | ||
} | ||
} |
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,31 @@ | ||
using System; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Mvc.ApplicationModels; | ||
|
||
namespace SimpleEndpoints.Conventions | ||
{ | ||
internal class RouteMutator : IConventionMutator | ||
{ | ||
private const string EndpointPlaceholder = "[endpoint]"; | ||
|
||
public void Mutate(ControllerModel controller, SimpleEndpointsConfiguration configuration) | ||
{ | ||
var routeBuilder = new StringBuilder(); | ||
var routeTemplate = controller.Selectors[0].AttributeRouteModel.Template; | ||
|
||
if (routeTemplate.Equals(EndpointPlaceholder, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
routeBuilder | ||
.Append($"{configuration.RoutePrefix}/") | ||
.Append(routeTemplate.Replace($"{EndpointPlaceholder}", | ||
controller.ControllerName.Replace(configuration.EndpointReplacementToken, string.Empty))); | ||
} | ||
else | ||
{ | ||
routeBuilder.Append(routeTemplate); | ||
} | ||
|
||
controller.Selectors[0].AttributeRouteModel.Template = routeBuilder.ToString(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
src/SimpleEndPoints/Extensions/ServiceCollectionExtensions.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,26 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SimpleEndpoints.Conventions; | ||
|
||
namespace SimpleEndpoints.Extensions | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddSimpleEndpointRouting(this IServiceCollection services, | ||
Action<SimpleEndpointsConfiguration> configure = null) | ||
{ | ||
var configuration = new SimpleEndpointsConfiguration(); | ||
configure?.Invoke(configuration); | ||
|
||
services.AddMvcCore(options => { options.Conventions.Add(new EndpointRoutingConvention(configuration)); }); | ||
|
||
services.AddSingleton<ApiDescriptionGroupCollectionProvider>(); | ||
services.AddSingleton<IApiDescriptionGroupCollectionProvider>(x => | ||
new ApiDescriptionGroupCollectionProviderDecorator( | ||
x.GetService<ApiDescriptionGroupCollectionProvider>())); | ||
|
||
return services; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.