feat:搭建yi-abp tool框架
This commit is contained in:
9
Yi.Abp.Net8/tool/Yi.Abp.Tool.Web/Program.cs
Normal file
9
Yi.Abp.Net8/tool/Yi.Abp.Tool.Web/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Yi.Abp.Tool.Web;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.WebHost.UseUrls(builder.Configuration["App:SelfUrl"]);
|
||||
builder.Host.UseAutofac();
|
||||
await builder.Services.AddApplicationAsync<YiAbpToolWebModule>();
|
||||
var app = builder.Build();
|
||||
await app.InitializeApplicationAsync();
|
||||
await app.RunAsync();
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Yi.Abp.Tool.Web": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
//"ASPNETCORE_ENVIRONMENT": "Staging"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "http://localhost:19002"
|
||||
},
|
||||
"Docker": {
|
||||
"commandName": "Docker",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_HTTP_PORTS": "19002"
|
||||
},
|
||||
"publishAllPorts": true
|
||||
}
|
||||
},
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json"
|
||||
}
|
||||
24
Yi.Abp.Net8/tool/Yi.Abp.Tool.Web/Yi.Abp.Tool.Web.csproj
Normal file
24
Yi.Abp.Net8/tool/Yi.Abp.Tool.Web/Yi.Abp.Tool.Web.csproj
Normal file
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Import Project="..\..\common.props" />
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.6" />
|
||||
<PackageReference Include="Volo.Abp.AspNetCore.Mvc" Version="$(AbpVersion)" />
|
||||
<PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\framework\Yi.Framework.AspNetCore\Yi.Framework.AspNetCore.csproj" />
|
||||
<ProjectReference Include="..\Yi.Abp.Tool.Application\Yi.Abp.Tool.Application.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="wwwroot\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
159
Yi.Abp.Net8/tool/Yi.Abp.Tool.Web/YiAbpToolWebModule.cs
Normal file
159
Yi.Abp.Net8/tool/Yi.Abp.Tool.Web/YiAbpToolWebModule.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using System.Globalization;
|
||||
using System.Threading.RateLimiting;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Volo.Abp.AspNetCore.Mvc;
|
||||
using Volo.Abp.AspNetCore.Mvc.AntiForgery;
|
||||
using Volo.Abp.Autofac;
|
||||
using Volo.Abp.Swashbuckle;
|
||||
using Yi.Abp.Tool.Application;
|
||||
using Yi.Framework.AspNetCore;
|
||||
using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder;
|
||||
using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Yi.Abp.Tool.Web
|
||||
{
|
||||
[DependsOn(typeof(YiAbpToolApplicationModule),
|
||||
|
||||
|
||||
typeof(AbpAspNetCoreMvcModule),
|
||||
typeof(AbpAutofacModule),
|
||||
typeof(AbpSwashbuckleModule),
|
||||
typeof(YiFrameworkAspNetCoreModule)
|
||||
)]
|
||||
public class YiAbpToolWebModule : AbpModule
|
||||
{
|
||||
private const string DefaultCorsPolicyName = "Default";
|
||||
public override Task ConfigureServicesAsync(ServiceConfigurationContext context)
|
||||
{
|
||||
var configuration = context.Services.GetConfiguration();
|
||||
var host = context.Services.GetHostingEnvironment();
|
||||
var service = context.Services;
|
||||
|
||||
//动态Api
|
||||
Configure<AbpAspNetCoreMvcOptions>(options =>
|
||||
{
|
||||
options.ConventionalControllers.Create(typeof(YiAbpToolApplicationModule).Assembly, options => options.RemoteServiceName = "tool");
|
||||
//统一前缀
|
||||
options.ConventionalControllers.ConventionalControllerSettings.ForEach(x => x.RootPath = "api/app");
|
||||
});
|
||||
|
||||
//设置api格式
|
||||
service.AddControllers().AddNewtonsoftJson(options =>
|
||||
{
|
||||
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
||||
options.SerializerSettings.Converters.Add(new StringEnumConverter());
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
Configure<AbpAntiForgeryOptions>(options =>
|
||||
{
|
||||
options.AutoValidate = false;
|
||||
});
|
||||
|
||||
//Swagger
|
||||
context.Services.AddYiSwaggerGen<YiAbpToolWebModule>(options =>
|
||||
{
|
||||
options.SwaggerDoc("default", new OpenApiInfo { Title = "Yi.Framework.Abp", Version = "v1", Description = "集大成者" });
|
||||
});
|
||||
|
||||
//跨域
|
||||
context.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(DefaultCorsPolicyName, builder =>
|
||||
{
|
||||
builder
|
||||
.WithOrigins(
|
||||
configuration["App:CorsOrigins"]!
|
||||
.Split(";", StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(o => o.RemovePostFix("/"))
|
||||
.ToArray()
|
||||
)
|
||||
.WithAbpExposedHeaders()
|
||||
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowCredentials();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
//速率限制
|
||||
//每60秒限制100个请求,滑块添加,分6段
|
||||
service.AddRateLimiter(_ =>
|
||||
{
|
||||
_.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
|
||||
_.OnRejected = (context, _) =>
|
||||
{
|
||||
if (context.Lease.TryGetMetadata(MetadataName.RetryAfter, out var retryAfter))
|
||||
{
|
||||
context.HttpContext.Response.Headers.RetryAfter =
|
||||
((int)retryAfter.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo);
|
||||
}
|
||||
context.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests;
|
||||
context.HttpContext.Response.WriteAsync("Too many requests. Please try again later.");
|
||||
|
||||
return new ValueTask();
|
||||
};
|
||||
|
||||
//全局使用,链式表达式
|
||||
_.GlobalLimiter = PartitionedRateLimiter.CreateChained(
|
||||
PartitionedRateLimiter.Create<HttpContext, string>(httpContext =>
|
||||
{
|
||||
var userAgent = httpContext.Request.Headers.UserAgent.ToString();
|
||||
|
||||
return RateLimitPartition.GetSlidingWindowLimiter
|
||||
(userAgent, _ =>
|
||||
new SlidingWindowRateLimiterOptions
|
||||
{
|
||||
PermitLimit = 1000,
|
||||
Window = TimeSpan.FromSeconds(60),
|
||||
SegmentsPerWindow = 6,
|
||||
QueueProcessingOrder = QueueProcessingOrder.OldestFirst
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
|
||||
{
|
||||
var service = context.ServiceProvider;
|
||||
var env = context.GetEnvironment();
|
||||
var app = context.GetApplicationBuilder();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
//跨域
|
||||
app.UseCors(DefaultCorsPolicyName);
|
||||
|
||||
if (!env.IsDevelopment())
|
||||
{
|
||||
//速率限制
|
||||
app.UseRateLimiter();
|
||||
}
|
||||
//swagger
|
||||
app.UseYiSwagger();
|
||||
|
||||
//请求处理
|
||||
app.UseYiApiHandlinge();
|
||||
//静态资源
|
||||
app.UseStaticFiles("/api/app/wwwroot");
|
||||
app.UseDefaultFiles();
|
||||
app.UseDirectoryBrowser("/api/app/wwwroot");
|
||||
|
||||
//终节点
|
||||
app.UseConfiguredEndpoints();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Yi.Abp.Net8/tool/Yi.Abp.Tool.Web/appsettings.json
Normal file
19
Yi.Abp.Net8/tool/Yi.Abp.Tool.Web/appsettings.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
//应用启动
|
||||
"App": {
|
||||
"SelfUrl": "http://*:19002",
|
||||
"CorsOrigins": "http://localhost:19002;http://localhost:18002"
|
||||
},
|
||||
"ToolOptions": {
|
||||
"TempDirPath": "wwwroot/temp",
|
||||
"ModuleTemplateFilePath": "wwwroot/ModuleTemplate.zip",
|
||||
"ProjectTemplateFilePath": "wwwroot/ProjectTemplate.zip"
|
||||
}
|
||||
}
|
||||
BIN
Yi.Abp.Net8/tool/Yi.Abp.Tool.Web/wwwroot/ModuleTemplate.zip
Normal file
BIN
Yi.Abp.Net8/tool/Yi.Abp.Tool.Web/wwwroot/ModuleTemplate.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user