mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-03 08:10:51 +08:00
框架分层
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CC.ElectronicCommerce.WebCore.MiddlewareExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// Axios会触发,需要做个状态返回,还需要指定跨域信息,这里放在网关了
|
||||
///
|
||||
/// OPTIONS请求即预检请求,可用于检测服务器允许的http方法。当发起跨域请求时,由于安全原因,触发一定条件时浏览器会在正式请求之前自动先发起OPTIONS请求,即CORS预检请求,服务器若接受该跨域请求,浏览器才继续发起正式请求。
|
||||
/// </summary>
|
||||
public class PreOptionRequestMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public PreOptionRequestMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
if (context.Request.Method.ToUpper() == "OPTIONS")
|
||||
{
|
||||
//context.Response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:8070");
|
||||
//context.Response.Headers.Add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,PATCH,OPTIONS");
|
||||
//context.Response.Headers.Add("Access-Control-Allow-Headers", "*");
|
||||
context.Response.StatusCode = 200;
|
||||
return;
|
||||
}
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扩展中间件
|
||||
/// </summary>
|
||||
public static class PreOptionsRequestMiddlewareExtensions
|
||||
{
|
||||
public static IApplicationBuilder UsePreOptionsRequest(this IApplicationBuilder app)
|
||||
{
|
||||
return app.UseMiddleware<PreOptionRequestMiddleware>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CC.ElectronicCommerce.WebCore.MiddlewareExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// 支持在返回HTML时,将返回的Stream保存到指定目录
|
||||
/// </summary>
|
||||
public class StaticPageMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private string _directoryPath = @"D:/cc-ec/";
|
||||
private bool _supportDelete = false;
|
||||
private bool _supportWarmup = false;
|
||||
|
||||
public StaticPageMiddleware(RequestDelegate next, string directoryPath, bool supportDelete, bool supportWarmup)
|
||||
{
|
||||
this._next = next;
|
||||
this._directoryPath = directoryPath;
|
||||
this._supportDelete = supportDelete;
|
||||
this._supportWarmup = supportWarmup;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
if (this._supportDelete && "Delete".Equals(context.Request.Query["ActionHeader"]))
|
||||
{
|
||||
this.DeleteHmtl(context.Request.Path.Value);
|
||||
context.Response.StatusCode = 200;
|
||||
}
|
||||
else if (this._supportWarmup && "ClearAll".Equals(context.Request.Query["ActionHeader"]))
|
||||
{
|
||||
this.ClearDirectory(10);//考虑数据量
|
||||
context.Response.StatusCode = 200;
|
||||
}
|
||||
else if (!context.Request.IsAjaxRequest())
|
||||
{
|
||||
Console.WriteLine($"This is StaticPageMiddleware InvokeAsync {context.Request.Path.Value}");
|
||||
#region context.Response.Body
|
||||
var originalStream = context.Response.Body;
|
||||
using (var copyStream = new MemoryStream())
|
||||
{
|
||||
context.Response.Body = copyStream;
|
||||
await _next(context);
|
||||
|
||||
copyStream.Position = 0;
|
||||
var reader = new StreamReader(copyStream);
|
||||
var content = await reader.ReadToEndAsync();
|
||||
string url = context.Request.Path.Value;
|
||||
|
||||
this.SaveHmtl(url, content);
|
||||
|
||||
copyStream.Position = 0;
|
||||
await copyStream.CopyToAsync(originalStream);
|
||||
context.Response.Body = originalStream;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
else
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveHmtl(string url, string html)
|
||||
{
|
||||
try
|
||||
{
|
||||
//Console.WriteLine($"Response: {html}");
|
||||
if (string.IsNullOrWhiteSpace(html))
|
||||
return;
|
||||
if (!url.EndsWith(".html"))
|
||||
return;
|
||||
|
||||
if (Directory.Exists(_directoryPath) == false)
|
||||
Directory.CreateDirectory(_directoryPath);
|
||||
|
||||
var totalPath = Path.Combine(_directoryPath, url.Split("/").Last());
|
||||
File.WriteAllText(totalPath, html);//直接覆盖
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除某个页面
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="index"></param>
|
||||
private void DeleteHmtl(string url)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!url.EndsWith(".html"))
|
||||
return;
|
||||
var totalPath = Path.Combine(_directoryPath, url.Split("/").Last());
|
||||
File.Delete(totalPath);//直接删除
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Delete {url} 异常,{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理文件,支持重试
|
||||
/// </summary>
|
||||
/// <param name="index">最多重试次数</param>
|
||||
private void ClearDirectory(int index)
|
||||
{
|
||||
if (index > 0)//简陋版---重试index次
|
||||
{
|
||||
try
|
||||
{
|
||||
var files = Directory.GetFiles(_directoryPath);
|
||||
foreach (var file in files)
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"ClearDirectory failed {ex.Message}");
|
||||
ClearDirectory(index--);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扩展中间件
|
||||
/// </summary>
|
||||
public static class StaticPageMiddlewareExtensions
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="directoryPath">文件写入地址,文件夹目录</param>
|
||||
/// <param name="supportDelete">是否支持删除</param>
|
||||
/// <param name="supportClear">是否支持全量删除</param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseStaticPageMiddleware(this IApplicationBuilder app, string directoryPath, bool supportDelete, bool supportClear)
|
||||
{
|
||||
return app.UseMiddleware<StaticPageMiddleware>(directoryPath, supportDelete, supportClear);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user