Skip to content

Commit

Permalink
Merge pull request #20593 from abpframework/auto-merge/rel-8-3/2931
Browse files Browse the repository at this point in the history
Merge branch dev with rel-8.3
  • Loading branch information
maliming authored Aug 22, 2024
2 parents 86af937 + ec43d1b commit 08a519a
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap;
using Volo.Abp.AspNetCore.Mvc.Libs;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap;
using Volo.Abp.Minify;
using Volo.Abp.Modularity;

Expand All @@ -11,5 +12,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling;
)]
public class AbpAspNetCoreMvcUiBundlingModule : AbpModule
{

public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpMvcLibsOptions>(options =>
{
options.CheckLibs = true;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using Volo.Abp.AspNetCore.Mvc.DependencyInjection;
using Volo.Abp.AspNetCore.Mvc.Infrastructure;
using Volo.Abp.AspNetCore.Mvc.Json;
using Volo.Abp.AspNetCore.Mvc.Libs;
using Volo.Abp.AspNetCore.Mvc.Localization;
using Volo.Abp.AspNetCore.VirtualFileSystem;
using Volo.Abp.DependencyInjection;
Expand Down Expand Up @@ -231,6 +232,7 @@ public override void PostConfigureServices(ServiceConfigurationContext context)
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
AddApplicationParts(context);
CheckLibs(context);
}

private static void AddApplicationParts(ApplicationInitializationContext context)
Expand Down Expand Up @@ -277,4 +279,9 @@ private static void AddToApplicationParts(ApplicationPartManager partManager, IE
partManager.ApplicationParts.AddIfNotContains(moduleAssembly);
}
}

private static void CheckLibs(ApplicationInitializationContext context)
{
context.ServiceProvider.GetRequiredService<IAbpMvcLibsService>().CheckLibs(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Volo.Abp.AspNetCore.Mvc.Libs;

public class AbpMvcLibsOptions
{
public bool CheckLibs { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.FileProviders;
using Volo.Abp.DependencyInjection;

namespace Volo.Abp.AspNetCore.Mvc.Libs;

public class AbpMvcLibsService : IAbpMvcLibsService, ITransientDependency
{
private Task<bool>? _checkLibsTask;

public virtual void CheckLibs(ApplicationInitializationContext context)
{
var options = context.ServiceProvider.GetRequiredService<IOptions<AbpMvcLibsOptions>>().Value;
if (options.CheckLibs)
{
var app = context.GetApplicationBuilder();
app.Use(async (httpContext, next) =>
{
if (!await CheckLibsAsyncOnceAsync(httpContext))
{
httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
httpContext.Response.ContentType = "text/html";
await httpContext.Response.WriteAsync(
"<html>" +
" <head>" +
" <title>Error - The Libs folder is missing!</title>" +
" </head>" +
" <body>" +
" <h1> &#9888;&#65039; The Libs folder under the <code style='background-color: #e7e7e7;'>wwwroot/libs</code> directory is empty!</h1>" +
" <p>The Libs folder contains mandatory NPM Packages for running the project.</p>" +
" <p>Make sure you run the <code style='background-color: #e7e7e7;'>abp install-libs</code> CLI tool command.</p>" +
" <p>For more information, check out the <a href='https://abp.io/docs/latest/CLI#install-libs'>ABP CLI documentation</a></p>" +
" </body>" +
"</html>",
Encoding.UTF8
);
return;
}

await next(httpContext);
});
}
}

protected virtual Task<bool> CheckLibsAsyncOnceAsync(HttpContext httpContext)
{
if (_checkLibsTask == null)
{
_checkLibsTask = CheckLibsAsync(httpContext);
}

return _checkLibsTask;
}

protected virtual Task<bool> CheckLibsAsync(HttpContext httpContext)
{
var logger = httpContext.RequestServices.GetRequiredService<ILogger<AbpMvcLibsService>>();
try
{
var fileProvider = new PhysicalFileProvider(httpContext.RequestServices.GetRequiredService<IWebHostEnvironment>().WebRootPath);
var libsFolder = fileProvider.GetDirectoryContents("/libs");
if (!libsFolder.Exists || !libsFolder.Any())
{
logger.LogError("The 'wwwroot/libs' folder does not exist or empty!");
return Task.FromResult(false);
}
}
catch (Exception e)
{
// In case of any exception, log it and return true to prevent crashing the application.
logger.LogError(e, "An error occurred while checking the libs folder!");
}

return Task.FromResult(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Volo.Abp.AspNetCore.Mvc.Libs;

public interface IAbpMvcLibsService
{
void CheckLibs(ApplicationInitializationContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations;
using Volo.Abp.AspNetCore.Mvc.GlobalFeatures;
using Volo.Abp.AspNetCore.Mvc.Libs;
using Volo.Abp.AspNetCore.Mvc.Localization;
using Volo.Abp.AspNetCore.Mvc.Localization.Resource;
using Volo.Abp.AspNetCore.Security.Claims;
Expand Down Expand Up @@ -140,6 +141,11 @@ public override void ConfigureServices(ServiceConfigurationContext context)
});

context.Services.TransformAbpClaims();

Configure<AbpMvcLibsOptions>(options =>
{
options.CheckLibs = false;
});
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
Expand Down

0 comments on commit 08a519a

Please sign in to comment.