diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/AbpAspNetCoreMvcUiBundlingModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/AbpAspNetCoreMvcUiBundlingModule.cs index bdd4cb2a27..4e020bfcf8 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/AbpAspNetCoreMvcUiBundlingModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/AbpAspNetCoreMvcUiBundlingModule.cs @@ -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; @@ -11,5 +12,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling; )] public class AbpAspNetCoreMvcUiBundlingModule : AbpModule { - + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.CheckLibs = true; + }); + } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs index 7b68fca32d..379523b659 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs @@ -32,6 +32,7 @@ using Volo.Abp.AspNetCore.Mvc.DataAnnotations; 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; @@ -231,6 +232,7 @@ public class AbpAspNetCoreMvcModule : AbpModule public override void OnApplicationInitialization(ApplicationInitializationContext context) { AddApplicationParts(context); + CheckLibs(context); } private static void AddApplicationParts(ApplicationInitializationContext context) @@ -277,4 +279,9 @@ public class AbpAspNetCoreMvcModule : AbpModule partManager.ApplicationParts.AddIfNotContains(moduleAssembly); } } + + private static void CheckLibs(ApplicationInitializationContext context) + { + context.ServiceProvider.GetRequiredService().CheckLibs(context); + } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/AbpMvcLibsOptions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/AbpMvcLibsOptions.cs new file mode 100644 index 0000000000..d166528a05 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/AbpMvcLibsOptions.cs @@ -0,0 +1,6 @@ +namespace Volo.Abp.AspNetCore.Mvc.Libs; + +public class AbpMvcLibsOptions +{ + public bool CheckLibs { get; set; } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/AbpMvcLibsService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/AbpMvcLibsService.cs new file mode 100644 index 0000000000..1329fa5e27 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/AbpMvcLibsService.cs @@ -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? _checkLibsTask; + + public virtual void CheckLibs(ApplicationInitializationContext context) + { + var options = context.ServiceProvider.GetRequiredService>().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( + "" + + " " + + " Error - The Libs folder is missing!" + + " " + + " " + + "

⚠️ The Libs folder under the wwwroot/libs directory is empty!

" + + "

The Libs folder contains mandatory NPM Packages for running the project.

" + + "

Make sure you run the abp install-libs CLI tool command.

" + + "

For more information, check out the ABP CLI documentation

" + + " " + + "", + Encoding.UTF8 + ); + return; + } + + await next(httpContext); + }); + } + } + + protected virtual Task CheckLibsAsyncOnceAsync(HttpContext httpContext) + { + if (_checkLibsTask == null) + { + _checkLibsTask = CheckLibsAsync(httpContext); + } + + return _checkLibsTask; + } + + protected virtual Task CheckLibsAsync(HttpContext httpContext) + { + var logger = httpContext.RequestServices.GetRequiredService>(); + try + { + var fileProvider = new PhysicalFileProvider(httpContext.RequestServices.GetRequiredService().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); + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/IAbpMvcLibsService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/IAbpMvcLibsService.cs new file mode 100644 index 0000000000..98a88184e8 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/IAbpMvcLibsService.cs @@ -0,0 +1,6 @@ +namespace Volo.Abp.AspNetCore.Mvc.Libs; + +public interface IAbpMvcLibsService +{ + void CheckLibs(ApplicationInitializationContext context); +} diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs index 027403d226..0e13746d66 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages; 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; @@ -140,6 +141,11 @@ public class AbpAspNetCoreMvcTestModule : AbpModule }); context.Services.TransformAbpClaims(); + + Configure(options => + { + options.CheckLibs = false; + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context)