134 lines
5.2 KiB
C#
134 lines
5.2 KiB
C#
using Hangfire;
|
|
using Hangfire.Dashboard;
|
|
using LearningOfficer.OA.Common.Configs;
|
|
using LearningOfficer.OA.Infrastructure.RabbitMQ;
|
|
using LearningOfficer.OA.Mobile.Api.CollectionExtensions;
|
|
using LearningOfficer.OA.Mobile.Api.Filters;
|
|
using LearningOfficer.OA.Mobile.Api.Helper;
|
|
using Microsoft.Extensions.Options;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Converters;
|
|
using Newtonsoft.Json.Serialization;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using TrackingSystem.SDK;
|
|
using Yitter.IdGenerator;
|
|
|
|
namespace LearningOfficer.OA.Mobile.Api
|
|
{
|
|
public class Startup
|
|
{
|
|
public IConfiguration Configuration { get; }
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public void ConfigureServices(IServiceCollection services, IWebHostEnvironment environment, IHostBuilder hostBuilder)
|
|
{
|
|
services.AddControllers().AddJsonOptions(option =>
|
|
{
|
|
option.JsonSerializerOptions.PropertyNamingPolicy = new CustomJsonNamingPolicy();
|
|
});
|
|
services.AddControllers(opt =>
|
|
{
|
|
// 添加全局异常过滤器
|
|
opt.Filters.Add<GlobalExceptionCatchFilter>();
|
|
// 添加统一返回结果过滤器
|
|
opt.Filters.Add<UniformResultActionFilter>();
|
|
opt.Filters.Add<GlobalOperationLogFilter>();
|
|
//// 添加认证过滤器,限制仅可单设备登录
|
|
//opt.Filters.Add<AuthonizationFilter>();
|
|
}).AddNewtonsoftJson(opt =>
|
|
{
|
|
//忽略循环引用
|
|
opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
//不改变字段大小
|
|
opt.SerializerSettings.ContractResolver = new DefaultContractResolver();
|
|
|
|
opt.SerializerSettings.ContractResolver = new CustomContractResolver();
|
|
//日期类型默认格式化处理
|
|
opt.SerializerSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
|
|
});
|
|
services.AddControllers().AddJsonOptions(options =>
|
|
{
|
|
options.JsonSerializerOptions.PropertyNamingPolicy = new CustomJsonNamingPolicy();
|
|
});
|
|
|
|
// 添加跨域服务
|
|
services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(builder =>
|
|
{
|
|
builder.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader();
|
|
});
|
|
});
|
|
|
|
services.BatchRegisterServices();
|
|
services.AddApiVersion();
|
|
services.AddJwtAuth(Configuration);
|
|
services.AddSwagger();
|
|
services.AddHttpClient();
|
|
services.AddSerilog(Configuration, hostBuilder, environment);
|
|
services.AddRedis(Configuration);
|
|
|
|
services.AddSqlSugar(Configuration, environment);
|
|
YitIdHelper.SetIdGenerator(WorkerIdAutoRegisterHelper.GetIdGeneratorOptions());
|
|
services.AddConfigureOptions(Configuration);
|
|
services.AddHttpContextAccessor();
|
|
|
|
|
|
}
|
|
|
|
public void Configure(WebApplication app, IWebHostEnvironment env)
|
|
{
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(c =>
|
|
{
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "OA移动端Api v1");
|
|
c.SwaggerEndpoint("/swagger/v2/swagger.json", "OA移动端Api v2");
|
|
c.SwaggerEndpoint("/swagger/v3/swagger.json", "OA移动端Api v3");
|
|
});
|
|
}
|
|
app.UseCors();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.Use(async (context, next) =>
|
|
{
|
|
if (context.Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
context.Request.EnableBuffering();
|
|
using (var reader = new StreamReader(context.Request.Body, encoding: Encoding.UTF8
|
|
, detectEncodingFromByteOrderMarks: false, leaveOpen: true))
|
|
{
|
|
var body = await reader.ReadToEndAsync();
|
|
context.Items.Add("body", body);
|
|
context.Request.Body.Position = 0;
|
|
}
|
|
}
|
|
if (context.Request.Method.Equals("Put", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
context.Request.EnableBuffering();
|
|
|
|
using (var reader = new StreamReader(context.Request.Body, encoding: Encoding.UTF8
|
|
, detectEncodingFromByteOrderMarks: false, leaveOpen: true))
|
|
{
|
|
var body = await reader.ReadToEndAsync();
|
|
context.Items.Add("body", body);
|
|
context.Request.Body.Position = 0;
|
|
}
|
|
}
|
|
await next.Invoke();
|
|
});
|
|
app.MapControllers();
|
|
}
|
|
|
|
}
|
|
}
|