143 lines
4.3 KiB
C#
143 lines
4.3 KiB
C#
|
|
using LY.App.Extensions;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using Microsoft.OpenApi.Models;
|
|||
|
|
using Newtonsoft.Json.Serialization;
|
|||
|
|
using Newtonsoft.Json;
|
|||
|
|
using SqlSugar;
|
|||
|
|
using Microsoft.AspNetCore.Cors.Infrastructure;
|
|||
|
|
using LY.App.Common.Redis;
|
|||
|
|
using LY.App.Service;
|
|||
|
|
using LY.App.Model;
|
|||
|
|
using LY.App.MiddleWare;
|
|||
|
|
using LY.App.Common.WebSocket;
|
|||
|
|
|
|||
|
|
var builder = WebApplication.CreateBuilder(args);
|
|||
|
|
|
|||
|
|
// Add services to the container.
|
|||
|
|
|
|||
|
|
builder.Services.AddControllers(options =>
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ [Required] <20><><EFBFBD>Ա<EFBFBD><D4B1>ǶԷǿɿ<C7BF><C9BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true;
|
|||
|
|
}).AddNewtonsoftJson()
|
|||
|
|
.AddNewtonsoftJson(NewtonsoftInitialize); ;
|
|||
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|||
|
|
builder.Services.AddEndpointsApiExplorer();
|
|||
|
|
builder.Services.AddSwaggerGen(
|
|||
|
|
options =>
|
|||
|
|
{
|
|||
|
|
options.SwaggerDoc("v1", new OpenApiInfo()
|
|||
|
|
{
|
|||
|
|
Title = "LY.App-Api",
|
|||
|
|
Version = "v1",
|
|||
|
|
Description = "Api<70>ӿ<EFBFBD><D3BF>ĵ<EFBFBD>",
|
|||
|
|
});
|
|||
|
|
var path = Path.Combine(AppContext.BaseDirectory, "ly.xml");
|
|||
|
|
options.IncludeXmlComments(path, true);
|
|||
|
|
options.OrderActionsBy(_ => _.RelativePath);
|
|||
|
|
});
|
|||
|
|
builder.Services.AddCors(CorsOptionsEvnet);
|
|||
|
|
//redis
|
|||
|
|
string redisConnection = builder.Configuration.GetValue<string>("Redis:ConnectionString") ?? "localhost:6379";
|
|||
|
|
|
|||
|
|
// ע<><D7A2> RedisService
|
|||
|
|
builder.Services.AddSingleton(new RedisService(redisConnection));
|
|||
|
|
////ע<><D7A2>SignalR
|
|||
|
|
builder.Services.AddSignalR();
|
|||
|
|
//ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
|
|||
|
|
builder.Services.ServicesAutoInjectionExtension();
|
|||
|
|
//<2F><><EFBFBD>ݿ<EFBFBD>
|
|||
|
|
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
|||
|
|
//sqlsugar
|
|||
|
|
builder.Services.AddTransient<SqlSugarClient>(sp =>
|
|||
|
|
{
|
|||
|
|
return new SqlSugarClient(new ConnectionConfig()
|
|||
|
|
{
|
|||
|
|
ConnectionString = connectionString,
|
|||
|
|
DbType = DbType.MySql,
|
|||
|
|
IsAutoCloseConnection = true,
|
|||
|
|
ConfigureExternalServices = new ConfigureExternalServices()
|
|||
|
|
{
|
|||
|
|
EntityService = (x, p) => //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ų<EFBFBD>DTO<54><4F>
|
|||
|
|
p.DbColumnName = UtilMethods.ToUnderLine(p.DbColumnName);//ToUnderLine<6E>շ<EFBFBD>ת<EFBFBD>»<EFBFBD><C2BB>߷<EFBFBD><DFB7><EFBFBD>
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
InitKeyType = InitKeyType.Attribute // ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
|
|||
|
|
}, db =>
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
#if DEBUG
|
|||
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|||
|
|
{
|
|||
|
|
Console.WriteLine(sql + "<22><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|||
|
|
};
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF>ͱ<EFBFBD><CDB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4>һ<EFBFBD><D2BB>
|
|||
|
|
//db.DbMaintenance.CreateDatabase();
|
|||
|
|
db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(LogEntity));
|
|||
|
|
#endif
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
// db.QueryFilter.AddTableFilter<IDeleted>(it => it.IsDeleted == false);
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
SnowFlakeSingle.WorkId = Convert.ToInt32(builder.Configuration.GetSection("SnowFlakeWordId")?.Value ?? "1");
|
|||
|
|
var app = builder.Build();
|
|||
|
|
ServiceLocator.Instance = app.Services;
|
|||
|
|
var device = app.Services.GetService<DeviceService>();
|
|||
|
|
device?.Init();
|
|||
|
|
|
|||
|
|
// Configure the HTTP request pipeline.
|
|||
|
|
if (app.Environment.IsDevelopment())
|
|||
|
|
{
|
|||
|
|
app.UseSwagger();
|
|||
|
|
app.UseSwaggerUI();
|
|||
|
|
}
|
|||
|
|
//·<><C2B7>ƥ<EFBFBD><C6A5>
|
|||
|
|
app.UseRouting();
|
|||
|
|
app.UseAuthorization();
|
|||
|
|
app.UseCors("CorsPolicy");
|
|||
|
|
|
|||
|
|
//<2F>쳣<EFBFBD>м<EFBFBD><D0BC><EFBFBD>
|
|||
|
|
app.UseMiddleware<CustomErrorMiddleware>();
|
|||
|
|
//ִ<><D6B4>ƥ<EFBFBD><C6A5><EFBFBD>Ķ˵<C4B6>
|
|||
|
|
app.UseEndpoints(endpoints =>
|
|||
|
|
{
|
|||
|
|
endpoints.MapHub<SocketHub>("/notification");
|
|||
|
|
endpoints.MapControllers();
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
app.MapControllers();
|
|||
|
|
app.Run();
|
|||
|
|
|
|||
|
|
static void NewtonsoftInitialize(MvcNewtonsoftJsonOptions options)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϸ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
|
|||
|
|
//<2F><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>
|
|||
|
|
//options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
|
|||
|
|
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
static void CorsOptionsEvnet(CorsOptions options)
|
|||
|
|
{
|
|||
|
|
options.AddPolicy("CorsPolicy", cors =>
|
|||
|
|
{
|
|||
|
|
cors.AllowAnyOrigin();
|
|||
|
|
cors.AllowAnyHeader();
|
|||
|
|
cors.AllowAnyMethod();
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
public static class ServiceLocator
|
|||
|
|
{
|
|||
|
|
public static IServiceProvider Instance { get; set; }
|
|||
|
|
}
|
|||
|
|
|