api fixes
This commit is contained in:
@@ -5,9 +5,8 @@ namespace zero.Api.Abstractions;
|
||||
|
||||
[ApiController]
|
||||
[ZeroAuthorize]
|
||||
[DisableBrowserCacheFilter]
|
||||
[ApiMetadataFilter]
|
||||
[ApiResponseFilter]
|
||||
//[ApiMetadataFilter]
|
||||
//[ApiResponseFilter]
|
||||
//[ServiceFilter(typeof(ModelStateValidationFilterAttribute))]
|
||||
//[ServiceFilter(typeof(BackofficeFilterAttribute))]
|
||||
public abstract class ZeroApiController : ControllerBase
|
||||
|
||||
@@ -8,17 +8,23 @@ public class ErrorController : ZeroApiController
|
||||
[HttpGet("")]
|
||||
public virtual ActionResult<ApiResponse> Index()
|
||||
{
|
||||
IExceptionHandlerFeature exception = HttpContext.Features.Get<IExceptionHandlerFeature>();
|
||||
|
||||
return new ErrorApiResponse()
|
||||
{
|
||||
Success = false,
|
||||
Status = HttpContext.Response.StatusCode,
|
||||
Error = new()
|
||||
{
|
||||
ApiPath = exception.Path,
|
||||
Message = exception.Error.Message
|
||||
}
|
||||
Status = HttpContext.Response.StatusCode
|
||||
};
|
||||
|
||||
//IExceptionHandlerFeature exception = HttpContext.Features.Get<IExceptionHandlerFeature>();
|
||||
|
||||
//return new ErrorApiResponse()
|
||||
//{
|
||||
// Success = false,
|
||||
// Status = HttpContext.Response.StatusCode,
|
||||
// Error = new()
|
||||
// {
|
||||
// ApiPath = exception.Path,
|
||||
// Message = exception.Error.Message
|
||||
// }
|
||||
//};
|
||||
}
|
||||
}
|
||||
@@ -7,20 +7,24 @@ namespace zero.Api.Controllers;
|
||||
|
||||
public class ZeroApiControllerModelConvention : IControllerModelConvention
|
||||
{
|
||||
protected Type BaseClassType { get; set; } = typeof(ZeroApiController);
|
||||
|
||||
readonly AttributeRouteModel AppAwareRouteModel;
|
||||
|
||||
readonly AttributeRouteModel AppUnawareRouteModel;
|
||||
|
||||
readonly Type BaseClass = typeof(ZeroApiController);
|
||||
readonly Type SystemApiType = typeof(ZeroSystemApiAttribute);
|
||||
|
||||
readonly ApiParameterTransformer Transformer = new();
|
||||
|
||||
readonly bool RuntimeIsAppAware = false;
|
||||
|
||||
|
||||
public ZeroApiControllerModelConvention(string zeroPath, string apiPath = "api", bool isAppAware = false)
|
||||
public ZeroApiControllerModelConvention(string path, bool isAppAware = false)
|
||||
{
|
||||
RuntimeIsAppAware = isAppAware;
|
||||
AppAwareRouteModel = BuildRouteModel(zeroPath, apiPath, true);
|
||||
AppUnawareRouteModel = BuildRouteModel(zeroPath, apiPath, false);
|
||||
AppAwareRouteModel = BuildRouteModel(path, true);
|
||||
AppUnawareRouteModel = BuildRouteModel(path, false);
|
||||
}
|
||||
|
||||
|
||||
@@ -31,20 +35,25 @@ public class ZeroApiControllerModelConvention : IControllerModelConvention
|
||||
{
|
||||
bool hasAttributeRouteModels = controller.Selectors.Any(selector => selector.AttributeRouteModel != null);
|
||||
|
||||
if (controller.ControllerType.IsSubclassOf(BaseClass))
|
||||
if (controller.ControllerType.IsSubclassOf(BaseClassType))
|
||||
{
|
||||
bool isAppAware = RuntimeIsAppAware && controller.ControllerType.GetCustomAttribute<ZeroSystemApiAttribute>() == null;
|
||||
bool isAppAware = RuntimeIsAppAware && controller.ControllerType.GetCustomAttribute(SystemApiType) == null;
|
||||
|
||||
controller.Selectors[0].AttributeRouteModel = isAppAware ? AppAwareRouteModel : AppUnawareRouteModel;
|
||||
controller.Filters.Add(new DisableBrowserCacheFilterAttribute());
|
||||
|
||||
foreach (var action in controller.Actions)
|
||||
{
|
||||
action.RouteParameterTransformer = Transformer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected AttributeRouteModel BuildRouteModel(string zeroPath, string apiPath, bool appAware = false)
|
||||
protected AttributeRouteModel BuildRouteModel(string pathSegment, bool appAware = false)
|
||||
{
|
||||
StringBuilder path = new();
|
||||
path.Append(zeroPath.EnsureEndsWith('/'));
|
||||
path.Append(apiPath.TrimStart('/').EnsureEndsWith('/'));
|
||||
path.Append(pathSegment.EnsureSurroundedWith('/'));
|
||||
|
||||
if (appAware)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
@@ -18,7 +17,7 @@ internal class ZeroApiMvcOptions : IConfigureOptions<MvcOptions>
|
||||
|
||||
public void Configure(MvcOptions options)
|
||||
{
|
||||
options.Conventions.Add(new ZeroApiControllerModelConvention(Options.ZeroPath, isAppAware: Options.Applications.Count > 1));
|
||||
options.Conventions.Add(new RouteTokenTransformerConvention(new ApiParameterTransformer()));
|
||||
Console.WriteLine("Configure API");
|
||||
options.Conventions.Add(new ZeroApiControllerModelConvention(Options.ZeroPath + "/api", isAppAware: Options.Applications.Count > 1));
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,10 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using zero.Api.Controllers;
|
||||
|
||||
namespace zero.Backoffice.Abstractions;
|
||||
|
||||
[ApiController]
|
||||
[ZeroAuthorize]
|
||||
[DisableBrowserCacheFilter]
|
||||
//[ServiceFilter(typeof(ModelStateValidationFilterAttribute))]
|
||||
//[ServiceFilter(typeof(BackofficeFilterAttribute))]
|
||||
public abstract class ZeroBackofficeController : ControllerBase
|
||||
|
||||
@@ -1,41 +1,11 @@
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using System.Reflection;
|
||||
using zero.Api.Controllers;
|
||||
using zero.Api.Filters;
|
||||
using zero.Api.Controllers;
|
||||
|
||||
namespace zero.Backoffice;
|
||||
|
||||
public class ZeroBackofficeControllerModelConvention : ZeroApiControllerModelConvention
|
||||
{
|
||||
readonly AttributeRouteModel AppAwareRouteModel;
|
||||
|
||||
readonly AttributeRouteModel AppUnawareRouteModel;
|
||||
|
||||
readonly Type BaseClass = typeof(ZeroBackofficeController);
|
||||
|
||||
readonly bool RuntimeIsAppAware = false;
|
||||
|
||||
|
||||
public ZeroBackofficeControllerModelConvention(string zeroPath, string backofficeApiPath = "backoffice", bool isAppAware = false) : base(zeroPath, backofficeApiPath, isAppAware)
|
||||
public ZeroBackofficeControllerModelConvention(string path, bool isAppAware = false) : base(path, isAppAware)
|
||||
{
|
||||
RuntimeIsAppAware = isAppAware;
|
||||
AppAwareRouteModel = BuildRouteModel(zeroPath, backofficeApiPath, true);
|
||||
AppUnawareRouteModel = BuildRouteModel(zeroPath, backofficeApiPath, false);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Configure routing model for all backoffice controllers
|
||||
/// </summary>
|
||||
public override void Apply(ControllerModel controller)
|
||||
{
|
||||
bool hasAttributeRouteModels = controller.Selectors.Any(selector => selector.AttributeRouteModel != null);
|
||||
|
||||
if (controller.ControllerType.IsSubclassOf(BaseClass))
|
||||
{
|
||||
bool isAppAware = RuntimeIsAppAware && controller.ControllerType.GetCustomAttribute<ZeroSystemApiAttribute>() == null;
|
||||
|
||||
controller.Selectors[0].AttributeRouteModel = isAppAware ? AppAwareRouteModel : AppUnawareRouteModel;
|
||||
}
|
||||
BaseClassType = typeof(ZeroBackofficeController);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,6 @@ internal class ZeroBackofficeMvcOptions : IConfigureOptions<MvcOptions>
|
||||
|
||||
public void Configure(MvcOptions options)
|
||||
{
|
||||
options.Conventions.Add(new ZeroBackofficeControllerModelConvention(Options.ZeroPath));
|
||||
options.Conventions.Add(new ZeroBackofficeControllerModelConvention(Options.ZeroPath + "/backoffice", Options.Applications.Count > 1));
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace zero.Routing;
|
||||
|
||||
public class ZeroRoutesTransformer : DynamicRouteValueTransformer
|
||||
{
|
||||
IRouteResolver RouteResolver;
|
||||
readonly IRouteResolver RouteResolver;
|
||||
|
||||
public ZeroRoutesTransformer(IRouteResolver routeResolver)
|
||||
{
|
||||
@@ -30,7 +30,7 @@ public class ZeroRoutesTransformer : DynamicRouteValueTransformer
|
||||
return null;
|
||||
}
|
||||
|
||||
httpContext.Features.Set(route);
|
||||
httpContext.Features.Set<IRouteModel>(route);
|
||||
|
||||
RouteEndpoint endpoint = RouteResolver.MapEndpoint(route);
|
||||
|
||||
|
||||
@@ -39,12 +39,11 @@ if (!app.Environment.IsDevelopment())
|
||||
app.UseZero().WithEndpoints(x =>
|
||||
{
|
||||
//x.MapControllerRoute("default", "api/{controller}/{action=Index}/{id?}");
|
||||
x.MapControllers();
|
||||
x.MapRazorPages();
|
||||
x.MapZeroRoutes();
|
||||
//x.MapZeroBackoffice();
|
||||
x.MapFallbackToController("Index", "Error");
|
||||
});
|
||||
|
||||
//app.UseZeroBackoffice();
|
||||
|
||||
app.Run();
|
||||
app.Run();
|
||||
@@ -40,7 +40,7 @@ public class SetupController : Controller
|
||||
//}
|
||||
|
||||
|
||||
[HttpGet]
|
||||
[HttpGet("/api/setup/indexes")]
|
||||
public async Task<IActionResult> Indexes([FromServices] IZeroStore store, [FromServices] IZeroContext context)
|
||||
{
|
||||
var indexes = context.Options.For<RavenOptions>().Indexes;
|
||||
@@ -57,7 +57,7 @@ public class SetupController : Controller
|
||||
}
|
||||
|
||||
|
||||
[HttpGet]
|
||||
[HttpGet("/api/setup/routes")]
|
||||
public async Task<IActionResult> Routes([FromServices] IZeroStore store, [FromServices] IZeroContext context, [FromServices] IEnumerable<IRouteProvider> providers)
|
||||
{
|
||||
RouteBulkRefresher refresher = new(store, context, providers);
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
{
|
||||
"Zero": {
|
||||
"Applications": {
|
||||
"EnableMultiple": true
|
||||
},
|
||||
"Applications": [
|
||||
{
|
||||
"Key": "hofbauer",
|
||||
"Name": "Hofbauer",
|
||||
"Database": "laola.hofbauer"
|
||||
},
|
||||
{
|
||||
"Key": "brothers",
|
||||
"Name": "brothers",
|
||||
"Database": "laola.brothers"
|
||||
}
|
||||
],
|
||||
"Raven": {
|
||||
"Url": "http://localhost:9800",
|
||||
"Database": "laola"
|
||||
@@ -16,4 +25,4 @@
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user