-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20593 from abpframework/auto-merge/rel-8-3/2931
Merge branch dev with rel-8.3
- Loading branch information
Showing
6 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/AbpMvcLibsOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
88 changes: 88 additions & 0 deletions
88
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/AbpMvcLibsService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> ⚠️ 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); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Libs/IAbpMvcLibsService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters