2020-04-15 14:09:52 +02:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2020-10-15 15:19:39 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
using Microsoft.AspNetCore.SpaServices;
|
2020-05-11 15:34:47 +02:00
|
|
|
using Microsoft.AspNetCore.SpaServices.Webpack;
|
2020-04-15 14:09:52 +02:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using System;
|
2020-10-15 15:19:39 +02:00
|
|
|
using System.Collections.Generic;
|
2020-05-11 15:34:47 +02:00
|
|
|
using System.IO;
|
2020-10-15 15:19:39 +02:00
|
|
|
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;
|
2020-05-14 13:32:33 +02:00
|
|
|
using zero.Core.Options;
|
2020-04-15 14:09:52 +02:00
|
|
|
|
|
|
|
|
namespace zero.Web
|
|
|
|
|
{
|
|
|
|
|
public static class ZeroApplicationBuilderExtensions
|
|
|
|
|
{
|
2020-10-15 15:19:39 +02:00
|
|
|
public static IApplicationBuilder UseZero(this IApplicationBuilder app, string devPath = null)
|
2020-04-15 14:09:52 +02:00
|
|
|
{
|
2020-05-14 13:32:33 +02:00
|
|
|
IZeroOptions options = app.ApplicationServices.GetService<IZeroOptions>();
|
2020-04-15 14:09:52 +02:00
|
|
|
|
|
|
|
|
string path = options.BackofficePath.EnsureStartsWith('/').TrimEnd('/');
|
|
|
|
|
|
2020-05-11 15:34:47 +02:00
|
|
|
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 = "Setup",
|
|
|
|
|
action = "Index"
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// routes for API
|
|
|
|
|
endpoints.MapControllerRoute(
|
|
|
|
|
name: "api",
|
|
|
|
|
pattern: path + "/api/{controller}/{action}/{id?}"
|
|
|
|
|
);
|
|
|
|
|
|
2020-10-15 15:19:39 +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", "Index");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 15:34:47 +02:00
|
|
|
|
2020-10-15 15:19:39 +02:00
|
|
|
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-12 15:55:56 +02:00
|
|
|
public static IApplicationBuilder UseZeroDevEnvironment(this IApplicationBuilder app, string path = null)
|
2020-05-11 15:34:47 +02:00
|
|
|
{
|
2020-10-15 15:19:39 +02:00
|
|
|
//IZeroOptions options = app.ApplicationServices.GetService<IZeroOptions>();
|
|
|
|
|
//string zeroPath = options.BackofficePath.EnsureStartsWith('/').TrimEnd('/');
|
2020-05-11 15:34:47 +02:00
|
|
|
|
|
|
|
|
|
2020-10-15 15:19:39 +02:00
|
|
|
//app.UseDeveloperExceptionPage();
|
|
|
|
|
|
|
|
|
|
//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
|
|
|
//{
|
2020-10-15 15:19:39 +02:00
|
|
|
// builder.UseRouting();
|
|
|
|
|
|
|
|
|
|
|
2020-07-09 15:33:58 +02:00
|
|
|
//});
|
|
|
|
|
|
2020-10-15 15:19:39 +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
|
2020-05-11 15:34:47 +02:00
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
}
|
2020-04-15 14:09:52 +02:00
|
|
|
}
|
|
|
|
|
}
|