using Furion;
|
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.Hosting;
|
using Serilog;
|
using SqlSugar;
|
using System.Text.Json.Serialization;
|
using Yw.Application;
|
using Mapster;
|
using IGeekFan.AspNetCore.Knife4jUI;
|
using Furion.SpecificationDocument;
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
using Furion.Authorization;
|
|
namespace IStation.WebApi
|
{
|
/// <summary>
|
///
|
/// </summary>
|
[AppStartup(10)]
|
public class Startup : AppStartup
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public void ConfigureServices(IServiceCollection services)
|
{
|
services.AddConfigurableOptions<Yw.JWT.JWTSettingsOptions>();
|
|
services.Configure<KestrelServerOptions>(options =>
|
{
|
options.Limits.MaxRequestBodySize = int.MaxValue;
|
});
|
services.Configure<IISServerOptions>(options =>
|
{
|
options.MaxRequestBodySize = int.MaxValue;
|
});
|
|
services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
|
|
//需在 services.AddControllers() 之前注册
|
services.AddCorsAccessor();
|
|
services.AddControllers().AddInjectWithUnifyResult<XnRestfulResultProvider>();
|
services.AddSwaggerGen(c =>
|
{
|
//避免dto 同名
|
c.CustomSchemaIds(x => x.FullName);
|
//支持knife4ui(必备)
|
c.CustomOperationIds(s =>
|
{
|
var a = s.ActionDescriptor as ControllerActionDescriptor;
|
return a.ControllerTypeInfo.Name + "-" + s.HttpMethod + a.ActionName;
|
});
|
c.UseInlineDefinitionsForEnums();
|
}).AddMiniProfiler();
|
|
|
services.AddJsonOptions(options =>
|
{
|
//返回属性大小写问题
|
options.JsonSerializerOptions.PropertyNamingPolicy = null;
|
//时间格式处理
|
options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter());
|
options.JsonSerializerOptions.Converters.Add(new DateTimeNullableJsonConverter());
|
|
//长整型格式处理
|
options.JsonSerializerOptions.Converters.Add(new LongJsonConverter());
|
options.JsonSerializerOptions.Converters.Add(new LongNullableJsonConverter());
|
//忽略循环引用
|
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; // 仅.NET 6支持
|
});
|
// 添加即时通讯
|
services.AddSignalR();
|
SnowFlakeSingle.WorkId = Yw.Settings.SqlSugarParasHelper.SqlSugar.SnowFlakeWorkId;
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
{
|
if (env.IsDevelopment())
|
{
|
app.UseDeveloperExceptionPage();
|
}
|
else
|
{
|
app.UseHsts();
|
}
|
|
// 添加规范化结果状态码,需要在这里注册
|
app.UseUnifyResultStatusCodes();
|
|
app.UseHttpsRedirection();
|
|
#region 开放 data 文件夹(可以通过url访问文件)
|
|
//app.UseStaticFiles();
|
string path = AppContext.BaseDirectory;
|
path = Path.Combine(path, Yw.Settings.ServiceParasHelper.Service.DataFolder);
|
if (!Directory.Exists(path))
|
{
|
Directory.CreateDirectory(path);
|
}
|
//通过url访问文件
|
app.UseStaticFiles(new StaticFileOptions()//自定义自己的文件路径
|
{
|
RequestPath = new PathString(""),//对外的访问路径 之前为 "/Data" ,改动是为了隐藏Data对外显示
|
FileProvider = new PhysicalFileProvider(path),//指定实际物理路径
|
OnPrepareResponse = (stf) =>//静态资源跨域问题
|
{
|
stf.Context.Response.Headers["Access-Control-Allow-Origin"] = "*";
|
stf.Context.Response.Headers["Access-Control-Allow-Headers"] = "*";
|
}
|
});
|
|
#endregion
|
|
// Serilog请求日志中间件---必须在 UseStaticFiles 和 UseRouting 之间
|
//app.UseSerilogRequestLogging();
|
|
app.UseRouting();
|
|
//需在 app.UseRouting(); 和 app.UseAuthentication(); 之间注册
|
app.UseCorsAccessor();
|
|
app.UseAuthentication();
|
app.UseAuthorization();
|
|
// 配置Swagger-Knife4UI(路由前缀一致代表独立,不同则代表共存)
|
app.UseKnife4UI(options =>
|
{
|
options.RoutePrefix = "Api"; // 配置 Knife4UI 路由地址
|
foreach (var groupInfo in SpecificationDocumentBuilder.GetOpenApiGroups())
|
{
|
options.SwaggerEndpoint("/" + groupInfo.RouteTemplate, groupInfo.Title);
|
}
|
});
|
|
app.UseInject(string.Empty);
|
|
app.UseEndpoints(endpoints =>
|
{
|
// 注册集线器
|
endpoints.MapHub<ChatHub>("/hubs/chathub");
|
endpoints.MapControllerRoute(
|
name: "default",
|
pattern: "{controller=Home}/{action=Index}/{id?}");
|
});
|
|
|
}
|
|
|
|
|
}
|
}
|