Files
mixtape/zero.Web/ZeroApplicationBuilderExtensions.cs
T

69 lines
1.8 KiB
C#
Raw Normal View History

2020-04-15 14:09:52 +02:00
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using zero.Core.Extensions;
using zero.Core.Options;
2020-11-05 00:46:18 +01:00
using zero.Web.Middlewares;
2020-11-17 11:27:29 +01:00
using zero.Web.Routing;
2020-04-15 14:09:52 +02:00
namespace zero.Web
{
public static class ZeroApplicationBuilderExtensions
{
2020-11-01 23:23:36 +01:00
public static IApplicationBuilder UseZero(this IApplicationBuilder app)
2020-04-15 14:09:52 +02:00
{
IZeroOptions options = app.ApplicationServices.GetService<IZeroOptions>();
2020-04-15 14:09:52 +02:00
string path = options.BackofficePath.EnsureStartsWith('/').TrimEnd('/');
2020-11-17 11:27:31 +01:00
app.UseMiddleware<ZeroContextMiddleware>();
app.UseStaticFiles();
2020-11-06 19:18:56 +01:00
2020-04-15 14:09:52 +02:00
// map backoffice
2020-11-17 11:27:29 +01:00
app.UseWhen(ctx => ctx.Request.Path.ToString().StartsWith(path), builder =>
{
builder.UseRouting();
2020-04-15 14:09:52 +02:00
2020-11-17 11:27:29 +01:00
builder.UseAuthentication();
builder.UseAuthorization();
2020-04-15 14:09:52 +02:00
2020-11-17 11:27:31 +01:00
//builder.UseMiddleware<ZeroMiddleware>(options);
2020-11-17 11:27:29 +01:00
builder.UseEndpoints(endpoints =>
{
2020-11-17 11:27:31 +01:00
// setup route
//endpoints.MapControllerRoute(
// name: "setup",
// pattern: path + "/setup",
// defaults: new
// {
// controller = "ZeroSetup",
// action = "Index"
// }
//);
2020-11-17 11:27:29 +01:00
//// routes for API
//endpoints.MapControllerRoute(
// name: "api",
// pattern: path + "/api/{controller}/{action}/{id?}"
//);
2020-04-15 14:09:52 +02:00
2020-11-17 11:27:29 +01:00
// fallbacks for SPA
endpoints.MapFallbackToController(path + "/{**path}", "Index", "ZeroBackoffice");
});
});
2020-04-15 14:09:52 +02:00
2020-11-06 19:18:56 +01:00
return app;
}
2020-10-27 01:04:23 +01:00
2020-11-06 19:18:56 +01:00
2020-11-17 11:27:31 +01:00
public static IApplicationBuilder UseZeroRoutes(this IApplicationBuilder app)
2020-11-06 19:18:56 +01:00
{
2020-11-17 11:27:31 +01:00
return app.UseEndpoints(endpoints =>
2020-10-26 13:59:46 +01:00
{
2020-11-17 11:27:31 +01:00
endpoints.MapDynamicControllerRoute<ZeroRoutesTransformer>("{**url}");
});
2020-04-15 14:09:52 +02:00
}
}
}