2025-03-22 12:16:22 +00:00
|
|
|
|
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;
|
2025-03-24 03:26:11 +00:00
|
|
|
|
using Microsoft.Extensions.FileProviders;
|
2025-03-22 12:16:22 +00:00
|
|
|
|
|
|
|
|
|
|
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));
|
2025-03-30 23:38:50 +00:00
|
|
|
|
builder.Services.AddTransient<TokenValidationMiddleware>();
|
2025-03-22 12:16:22 +00:00
|
|
|
|
////ע<><D7A2>SignalR
|
|
|
|
|
|
builder.Services.AddSignalR();
|
2025-03-27 16:34:20 +00:00
|
|
|
|
builder.Services.AddHttpClient();
|
2025-03-22 12:16:22 +00:00
|
|
|
|
//ע<><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) =>
|
|
|
|
|
|
{
|
2025-04-07 14:38:22 +00:00
|
|
|
|
// Console.WriteLine(sql + "<22><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
2025-03-22 12:16:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF>ͱ<EFBFBD><CDB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4>һ<EFBFBD><D2BB>
|
|
|
|
|
|
//db.DbMaintenance.CreateDatabase();
|
2025-03-29 14:50:23 +00:00
|
|
|
|
//db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(UserEntity));
|
2025-03-22 12:16:22 +00:00
|
|
|
|
#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);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-03-24 03:26:11 +00:00
|
|
|
|
//<2F><><EFBFBD>Ⲣ<EFBFBD><E2B2A2><EFBFBD><EFBFBD>ͼƬ<CDBC>洢<EFBFBD>ļ<EFBFBD>Ŀ¼
|
|
|
|
|
|
string path = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "Img"));
|
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-22 12:16:22 +00:00
|
|
|
|
SnowFlakeSingle.WorkId = Convert.ToInt32(builder.Configuration.GetSection("SnowFlakeWordId")?.Value ?? "1");
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
ServiceLocator.Instance = app.Services;
|
|
|
|
|
|
var device = app.Services.GetService<DeviceService>();
|
2025-03-29 14:50:23 +00:00
|
|
|
|
await device?.Init();
|
2025-03-24 03:26:11 +00:00
|
|
|
|
app.UseStaticFiles(new StaticFileOptions()
|
|
|
|
|
|
{
|
|
|
|
|
|
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Img")),
|
2025-04-04 14:01:40 +00:00
|
|
|
|
RequestPath = "/api/upload"
|
2025-03-24 03:26:11 +00:00
|
|
|
|
});
|
2025-03-22 12:16:22 +00:00
|
|
|
|
// Configure the HTTP request pipeline.
|
2025-04-02 15:12:16 +00:00
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
|
{
|
2025-03-29 14:50:23 +00:00
|
|
|
|
|
2025-04-02 15:12:16 +00:00
|
|
|
|
}
|
2025-03-28 15:20:31 +00:00
|
|
|
|
app.UseSwagger();
|
|
|
|
|
|
app.UseSwaggerUI();
|
2025-03-22 12:16:22 +00:00
|
|
|
|
//·<><C2B7>ƥ<EFBFBD><C6A5>
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
app.UseCors("CorsPolicy");
|
|
|
|
|
|
|
|
|
|
|
|
//<2F>쳣<EFBFBD>м<EFBFBD><D0BC><EFBFBD>
|
|
|
|
|
|
app.UseMiddleware<CustomErrorMiddleware>();
|
2025-03-29 14:50:23 +00:00
|
|
|
|
//token<65><6E>֤<EFBFBD>м<EFBFBD><D0BC><EFBFBD>
|
2025-04-02 15:12:16 +00:00
|
|
|
|
app.UseMiddleware<TokenValidationMiddleware>();
|
2025-03-22 12:16:22 +00:00
|
|
|
|
//ִ<><D6B4>ƥ<EFBFBD><C6A5><EFBFBD>Ķ˵<C4B6>
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
|
{
|
2025-04-02 13:53:32 +00:00
|
|
|
|
endpoints.MapHub<SocketHub>("/websocket");
|
2025-03-22 12:16:22 +00:00
|
|
|
|
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; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|