Files
mixtape/zero.Web/ZeroApplicationBuilderExtensions.cs
T

138 lines
3.8 KiB
C#
Raw Normal View History

2020-04-15 14:09:52 +02:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.SpaServices;
using Microsoft.AspNetCore.SpaServices.Webpack;
2020-04-15 14:09:52 +02:00
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
using VueCliMiddleware;
2020-04-15 14:09:52 +02:00
using zero.Core.Extensions;
2020-05-21 13:41:09 +02:00
using zero.Core.Middlewares;
using zero.Core.Options;
2020-10-26 13:59:46 +01:00
using zero.Web.Routing;
2020-04-15 14:09:52 +02:00
namespace zero.Web
{
public static class ZeroApplicationBuilderExtensions
{
public static IApplicationBuilder UseZero(this IApplicationBuilder app, string devPath = null)
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('/');
app.UseStaticFiles();
2020-05-21 13:41:09 +02:00
app.UseMiddleware<ApplicationContextMiddleware>();
2020-04-15 14:09:52 +02:00
// map backoffice
app.UseWhen(ctx => ctx.Request.Path.ToString().StartsWith(path), builder =>
{
builder.UseRouting();
builder.UseAuthentication();
builder.UseAuthorization();
//builder.UseMiddleware<ZeroMiddleware>(options);
builder.UseEndpoints(endpoints =>
{
// setup route
//endpoints.MapControllerRoute(
// name: "setup",
// pattern: path + "/setup",
// defaults: new
// {
// controller = "ZeroSetup",
// action = "Index"
// }
//);
2020-04-15 14:09:52 +02:00
//// routes for API
//endpoints.MapControllerRoute(
// name: "api",
// pattern: path + "/api/{controller}/{action}/{id?}"
//);
2020-04-15 14:09:52 +02:00
if (devPath != null)
{
endpoints.MapFallback(path + "/vue-cli/{**path}", CreateVueProxyDelegate(endpoints, new SpaOptions { SourcePath = devPath }));
}
2020-04-15 14:09:52 +02:00
// fallbacks for SPA
endpoints.MapFallbackToController(path + "/{**path}", "Index", "ZeroBackoffice");
2020-04-15 14:09:52 +02:00
});
});
2020-10-27 01:04:23 +01:00
app.UseEndpoints(endpoints =>
2020-10-26 13:59:46 +01:00
{
2020-10-27 01:04:23 +01:00
endpoints.MapDynamicControllerRoute<ZeroRoutesTransformer>("{**url}");
2020-10-26 13:59:46 +01:00
});
2020-04-15 14:09:52 +02:00
return app;
}
static RequestDelegate CreateVueProxyDelegate(IEndpointRouteBuilder endpoints, SpaOptions options = null)
{
var app = endpoints.CreateApplicationBuilder();
app.Use(next => context =>
{
context.SetEndpoint(null);
return next(context);
});
app.UseSpa(opt =>
{
if (options != null)
{
opt.Options.DefaultPage = options.DefaultPage;
opt.Options.DefaultPageStaticFileOptions = options.DefaultPageStaticFileOptions;
opt.Options.SourcePath = options.SourcePath;
opt.Options.StartupTimeout = options.StartupTimeout;
}
opt.UseVueCli();
});
return app.Build();
}
2020-10-27 01:04:23 +01:00
public static IApplicationBuilder UseZeroDevEnvironment(this IApplicationBuilder app)
{
//IZeroOptions options = app.ApplicationServices.GetService<IZeroOptions>();
//string zeroPath = options.BackofficePath.EnsureStartsWith('/').TrimEnd('/');
//string webUiPath = path ?? Path.Combine(Environment.CurrentDirectory, "..", "zero.Web.UI"); // TODO dynPATH
// map backoffice
//app.UseWhen(ctx => ctx.Request.Path.ToString().StartsWith(zeroPath), builder =>
2020-07-09 15:33:58 +02:00
//{
// builder.UseRouting();
2020-07-09 15:33:58 +02:00
//});
//#pragma warning disable CS0618
//app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions()
//{
// HotModuleReplacement = true,
// ProjectPath = webUiPath,
// HotModuleReplacementServerPort = 8999,
// EnvironmentVariables = new Dictionary<string, string>()
// {
// { "ZERO", "hi from zero 0.1" }
// }
//});
//#pragma warning restore CS0618
return app;
}
2020-04-15 14:09:52 +02:00
}
}