You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
6.5 KiB

4 months ago
using System.Globalization;
using System.Text;
8 months ago
using Autofac;
using Autofac.Extensions.DependencyInjection;
6 months ago
using Entity.Dto.Resp;
8 months ago
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.AutoTask;
8 months ago
using HybirdFrameworkCore.Configuration;
using HybirdFrameworkCore.Entity;
using HybirdFrameworkCore.Job;
7 months ago
using HybirdFrameworkCore.Redis;
using log4net;
6 months ago
using Mapster;
using Microsoft.AspNetCore.Authentication.JwtBearer;
4 months ago
using Microsoft.AspNetCore.Localization;
6 months ago
using Microsoft.IdentityModel.Tokens;
5 months ago
using Service.Charger.Client;
5 months ago
using Service.Execute;
5 months ago
using Service.Led;
5 months ago
using Service.Padar.Client;
using Service.RealTime;
4 months ago
using Service.Sound.SoundClient;
using Service.Ups;
8 months ago
using SqlSugar;
using SqlSugar.IOC;
var builder = WebApplication.CreateBuilder(args);
var log = LogManager.GetLogger(typeof(Program));
8 months ago
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(cb =>
{
cb.Register(c =>
{
6 months ago
var db = new SqlSugarScope(new ConnectionConfig
8 months ago
{
ConfigId = AppSettingsConstVars.ConfigId,
6 months ago
ConnectionString = AppSettingsConstVars.DbSqlConnection, // 设置数据库连接字符串
8 months ago
DbType = AppSettingsConstVars.DbDbType == IocDbType.MySql.ToString() ? DbType.MySql : DbType.SqlServer,
IsAutoCloseConnection = true,
6 months ago
InitKeyType = InitKeyType.Attribute // 如果使用实体类的属性进行主键标识,请设置为 InitKeyType.Attr
}, c =>
{
c.Aop.OnLogExecuting = (sql, pars) =>
{
string nativeSql = UtilMethods.GetNativeSql(sql, pars);
log.Info(nativeSql);
};
8 months ago
});
return db;
6 months ago
}).As<ISqlSugarClient>().SingleInstance();
8 months ago
8 months ago
cb.RegisterModule(new AutofacModuleRegister());
});
6 months ago
// 注册自定义映射
6 months ago
var mapper = new SysMenuMapper();
mapper.Register(TypeAdapterConfig.GlobalSettings);
//在 Autofac 容器中注册IHttpContextAccessor 服务
builder.Services.AddHttpContextAccessor();
6 months ago
//跨域
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy
(builde =>
6 months ago
{
builde
.SetIsOriginAllowed(_ => true)
6 months ago
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
6 months ago
}
);
});
7 months ago
//redis
var redisConnectionString = AppSettingsHelper.GetContent("Redis", "Connection");
var instanceName = AppSettingsHelper.GetContent("Redis", "InstanceName"); //默认数据库
var defaultDb = int.Parse(AppSettingsHelper.GetContent("Redis", "DefaultDB") ?? "0");
7 months ago
if (redisConnectionString != null && instanceName != null)
builder.Services.AddSingleton(new RedisHelper(redisConnectionString, instanceName, defaultDb));
7 months ago
6 months ago
builder.Services.AddControllers().AddJsonOptions(configure =>
{
configure.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
// configure.JsonSerializerOptions.Converters.Add(new LongJsonConverter());
6 months ago
});
7 months ago
8 months ago
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Logging.AddLog4Net("log4net.config");
builder.Services.AddSwaggerGen(c =>
{
var basePath = Path.GetDirectoryName(AppContext.BaseDirectory);
//c.IncludeXmlComments(Path.Combine(basePath, Assembly.GetExecutingAssembly().GetName().Name+".xml"), true);
c.IncludeXmlComments(Path.Combine(basePath, "WebStarter.xml"), true);
c.IncludeXmlComments(Path.Combine(basePath, "Entity.xml"), true);
c.IncludeXmlComments(Path.Combine(basePath, "HybirdFrameworkCore.xml"), true);
});
6 months ago
bool.TryParse(AppSettingsHelper.GetContent("SignalR", "enable"), out var signalrEnabled);
if (signalrEnabled)
{
builder.Services.AddSignalR(options =>
{
options.EnableDetailedErrors = true;
options.KeepAliveInterval = TimeSpan.FromMinutes(1);
});
}
6 months ago
// 添加jwt验证
var securityKey = AppSettingsHelper.GetContent("TokenOptions", "SecurityKey");
var audience = AppSettingsHelper.GetContent("TokenOptions", "Audience");
var issuer = AppSettingsHelper.GetContent("TokenOptions", "Issuer");
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options => //配置鉴权逻辑
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = issuer,
ValidAudience = audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey)),
ValidateLifetime = false
};
});
4 months ago
// 添加国际化支持
builder.Services.AddLocalization(options =>
{
options.ResourcesPath = "Resources";
});
6 months ago
8 months ago
var app = builder.Build();
8 months ago
8 months ago
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();
app.UseStaticFiles();
8 months ago
app.UseAuthorization();
app.UseCors();
8 months ago
app.MapControllers();
if (signalrEnabled)
{
app.MapHub<MyHub>("/realtime");
}
8 months ago
6 months ago
var list = AppSettingsHelper.GetContent("Kestrel", "Endpoints", "http", "Url");
foreach (var s in list.Split(";"))
{
log.Info($"use url={s}");
6 months ago
app.Urls.Add(s);
}
8 months ago
AppInfo.Container = app.Services.GetAutofacRoot();
5 months ago
////云平台
//if (AppSettingsHelper.GetBool("cloud", "enable"))
//{
// CloudClientMgr.Init();
//}
5 months ago
////PLC
3 months ago
if (AppSettingsHelper.GetBool("plc", "enable"))
{
ClientMgr.InitClient();
}
//雷达
if (AppSettingsHelper.GetBool("padar", "enable"))
{
PadarMgr.InitClient();
}
3 months ago
if (AppSettingsHelper.GetBool("led", "enable"))
{
LedClient.Init(AppSettingsHelper.GetContent("led", "ip"), Convert.ToInt32(AppSettingsHelper.GetContent("led", "port")));
}
5 months ago
////启动换电流程
5 months ago
if (AppSettingsHelper.GetBool("swap", "enable"))
{
StationSoftMgr.SwappingStateMachineStart();
}
5 months ago
//PadarMgr.InitClient();
5 months ago
3 months ago
////现场调试plc连接
5 months ago
AppInfo.Container = app.Services.GetAutofacRoot();
4 months ago
//ClientMgr.InitClient();
//ups
if (AppSettingsHelper.GetBool("ups", "enable"))
{
UpsMgr.UpsInit();
}
4 months ago
//new SoundClient().SoundConn();
5 months ago
//TaskInit.Init();
5 months ago
//PadarMgr.InitClient();
TaskInit.Init();
3 months ago
//QuartzSchedulerFactory.Init();
//app.Lifetime.ApplicationStopping.Register(QuartzSchedulerFactory.Shutdown);
4 months ago
// 配置多语言支持
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en"),
new CultureInfo("zh")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("zh"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures,
});
5 months ago
app.Run();