host shit
This commit is contained in:
@@ -89,12 +89,14 @@ namespace zero.Core
|
||||
|
||||
// set default database for document store
|
||||
Store.ResolvedDatabase = Application.Database;
|
||||
}
|
||||
|
||||
if (IsBackofficeRequest is false && context.Request.RouteValues.TryGetValue("zero.route", out object route))
|
||||
{
|
||||
ResolvedRoute = (IResolvedRoute)route;
|
||||
Route = ResolvedRoute.Route;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void SetRoute(IResolvedRoute route)
|
||||
{
|
||||
ResolvedRoute = route;
|
||||
Route = ResolvedRoute.Route;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,5 +143,10 @@ namespace zero.Core
|
||||
/// the currently active backoffice user, as users are not signed in with the default scheme and do therefore not populate HttpContext.User
|
||||
/// </summary>
|
||||
Task Resolve(HttpContext context, IEnumerable<IApplication> applications);
|
||||
|
||||
/// <summary>
|
||||
/// Set resolved route for frontend requests
|
||||
/// </summary>
|
||||
void SetRoute(IResolvedRoute route);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,30 +3,50 @@ using System;
|
||||
using System.Threading.Tasks;
|
||||
using zero.Core;
|
||||
using zero.Core.Host;
|
||||
using zero.Core.Routing;
|
||||
|
||||
namespace zero.Web.Middlewares
|
||||
{
|
||||
public class ZeroMiddleware
|
||||
{
|
||||
readonly RequestDelegate Next;
|
||||
readonly IZeroHost Host;
|
||||
readonly IRoutes Routes;
|
||||
|
||||
|
||||
public ZeroMiddleware(RequestDelegate next)
|
||||
public ZeroMiddleware(RequestDelegate next, IZeroHost zeroHost, IRoutes routes)
|
||||
{
|
||||
Next = next;
|
||||
Host = zeroHost;
|
||||
Routes = routes;
|
||||
}
|
||||
|
||||
|
||||
public async Task Invoke(HttpContext httpContext, IZeroHost zeroHost)
|
||||
public async Task Invoke(HttpContext httpContext)
|
||||
{
|
||||
await zeroHost.Initialize();
|
||||
await Host.Initialize();
|
||||
|
||||
if (httpContext == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(httpContext));
|
||||
}
|
||||
|
||||
IZeroContext context = await zeroHost.GetContext(httpContext);
|
||||
IZeroContext context = await Host.GetContext(httpContext);
|
||||
|
||||
if (!context.IsBackofficeRequest)
|
||||
{
|
||||
IResolvedRoute route = await Routes.ResolveUrl(httpContext);
|
||||
|
||||
if (route != null)
|
||||
{
|
||||
context.SetRoute(route);
|
||||
|
||||
RouteProviderEndpoint endpoint = Routes.MapEndpoint(route);
|
||||
|
||||
httpContext.Request.RouteValues["controller"] = endpoint.Controller;
|
||||
httpContext.Request.RouteValues["action"] = endpoint.Action;
|
||||
}
|
||||
}
|
||||
|
||||
await Next(httpContext);
|
||||
}
|
||||
|
||||
@@ -1,40 +1,40 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Routing;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using zero.Core.Routing;
|
||||
//using Microsoft.AspNetCore.Http;
|
||||
//using Microsoft.AspNetCore.Mvc.Routing;
|
||||
//using Microsoft.AspNetCore.Routing;
|
||||
//using System;
|
||||
//using System.Threading.Tasks;
|
||||
//using zero.Core.Routing;
|
||||
|
||||
namespace zero.Web.Routing
|
||||
{
|
||||
public class ZeroRoutesTransformer : DynamicRouteValueTransformer
|
||||
{
|
||||
IRoutes Routes;
|
||||
//namespace zero.Web.Routing
|
||||
//{
|
||||
// public class ZeroRoutesTransformer : DynamicRouteValueTransformer
|
||||
// {
|
||||
// IRoutes Routes;
|
||||
|
||||
public ZeroRoutesTransformer(IRoutes routes)
|
||||
{
|
||||
Routes = routes;
|
||||
}
|
||||
// public ZeroRoutesTransformer(IRoutes routes)
|
||||
// {
|
||||
// Routes = routes;
|
||||
// }
|
||||
|
||||
|
||||
public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
|
||||
{
|
||||
Console.WriteLine("resolve route");
|
||||
IResolvedRoute route = await Routes.ResolveUrl(httpContext);
|
||||
// public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
|
||||
// {
|
||||
// Console.WriteLine("resolve route");
|
||||
// IResolvedRoute route = await Routes.ResolveUrl(httpContext);
|
||||
|
||||
Console.WriteLine("route: " + route?.Route.Url);
|
||||
if (route == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
// Console.WriteLine("route: " + route?.Route.Url);
|
||||
// if (route == null)
|
||||
// {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
RouteProviderEndpoint endpoint = Routes.MapEndpoint(route);
|
||||
// RouteProviderEndpoint endpoint = Routes.MapEndpoint(route);
|
||||
|
||||
values["zero.route"] = route;
|
||||
values["controller"] = endpoint.Controller;
|
||||
values["action"] = endpoint.Action;
|
||||
// values["zero.route"] = route;
|
||||
// values["controller"] = endpoint.Controller;
|
||||
// values["action"] = endpoint.Action;
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
||||
}
|
||||
// return values;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -8,7 +8,6 @@ using zero.Core;
|
||||
using zero.Core.Extensions;
|
||||
using zero.Core.Options;
|
||||
using zero.Web.Middlewares;
|
||||
using zero.Web.Routing;
|
||||
|
||||
namespace zero.Web
|
||||
{
|
||||
@@ -20,29 +19,33 @@ namespace zero.Web
|
||||
|
||||
string path = options.BackofficePath.EnsureStartsWith('/').TrimEnd('/');
|
||||
|
||||
app.UseStatusCodePages();
|
||||
app.UseRouting();
|
||||
app.UseRequestLocalization();
|
||||
|
||||
app.UseMiddleware<PoweredByZeroMiddleware>();
|
||||
app.UseMiddleware<ZeroMiddleware>();
|
||||
|
||||
// map backoffice
|
||||
app.UseWhen(ctx => ctx.Request.Path.ToString().StartsWith(path), builder =>
|
||||
{
|
||||
builder.UseRouting();
|
||||
//app.UseWhen(ctx => ctx.Request.Path.ToString().StartsWith(path), builder =>
|
||||
//{
|
||||
// builder.UseRouting();
|
||||
|
||||
builder.UseAuthentication();
|
||||
builder.UseAuthorization();
|
||||
// builder.UseAuthentication();
|
||||
// builder.UseAuthorization();
|
||||
|
||||
builder.UseEndpoints(endpoints =>
|
||||
{
|
||||
//// routes for API
|
||||
//endpoints.MapControllerRoute(
|
||||
// name: "api",
|
||||
// pattern: path + "/api/{controller}/{action}/{id?}"
|
||||
//);
|
||||
// builder.UseEndpoints(endpoints =>
|
||||
// {
|
||||
// //// routes for API
|
||||
// //endpoints.MapControllerRoute(
|
||||
// // name: "api",
|
||||
// // pattern: path + "/api/{controller}/{action}/{id?}"
|
||||
// //);
|
||||
|
||||
// fallbacks for SPA
|
||||
endpoints.MapFallbackToController(path + "/{**path}", "Index", "ZeroBackoffice");
|
||||
});
|
||||
});
|
||||
// // fallbacks for SPA
|
||||
// endpoints.MapFallbackToController(path + "/{**path}", "Index", "ZeroBackoffice");
|
||||
// });
|
||||
//});
|
||||
|
||||
return app;
|
||||
}
|
||||
@@ -84,13 +87,17 @@ namespace zero.Web
|
||||
|
||||
RequestDelegate backofficePipeline = endpoints.CreateApplicationBuilder()
|
||||
.UseMiddleware<ZeroMiddleware>(args)
|
||||
.UseStaticFiles()
|
||||
.UseRouting()
|
||||
.UseAuthentication()
|
||||
.UseAuthorization()
|
||||
.Build();
|
||||
|
||||
RequestDelegate frontendPipeline = endpoints.CreateApplicationBuilder()
|
||||
.UseMiddleware<ZeroMiddleware>(args)
|
||||
.Build();
|
||||
//RequestDelegate frontendPipeline = endpoints.CreateApplicationBuilder()
|
||||
// .UseMiddleware<ZeroMiddleware>(args)
|
||||
// .Build();
|
||||
|
||||
endpoints.map(frontendPipeline);
|
||||
//endpoints.map(frontendPipeline);
|
||||
|
||||
return endpoints.Map(pattern, backofficePipeline).WithDisplayName("Zero");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user