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.
162 lines
5.7 KiB
162 lines
5.7 KiB
using Autofac; |
|
using Autofac.Extensions.DependencyInjection; |
|
using Common.Shared.Application.DaHua; |
|
using Common.Shared.DomainService; |
|
using Microsoft.OpenApi.Models; |
|
using NLog; |
|
using NLog.Extensions.Logging; |
|
using NLog.Web; |
|
using Quartz; |
|
using System.Reflection; |
|
using Video.API.Infrastructure; |
|
using Video.Application; |
|
|
|
namespace Video.API |
|
{ |
|
/// <summary> |
|
/// |
|
/// </summary> |
|
public class Program |
|
{ |
|
public static void Main(string[] args) |
|
{ |
|
var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger(); |
|
logger.Debug("init main"); |
|
try |
|
{ |
|
var builder = WebApplication.CreateBuilder(args); |
|
|
|
builder.Services.AddHttpContextAccessor(); |
|
builder.Services.AddHttpClient(); |
|
|
|
builder.Services.AddControllers(); |
|
builder.Services.AddSingleton(builder.Configuration); |
|
|
|
#region Cors |
|
|
|
var isconfig = builder.Configuration.GetSection("IdentityClientConfig").Get<IdentityClientConfig>(); |
|
builder.Services.AddCors(options => |
|
{ |
|
options.AddPolicy("_myAllowSpecificOrigins", |
|
builder => |
|
{ |
|
builder |
|
.WithOrigins(isconfig.CorsWithOrigins) //被允许的来源,多个使用逗号分隔 |
|
//.AllowAnyOrigin() //允许所有源访问本API(开发环境设置) |
|
.AllowAnyHeader() |
|
.AllowAnyMethod() |
|
.AllowCredentials() |
|
.SetIsOriginAllowed((h) => true);//为Signalr新添加的配置 |
|
}); |
|
}); |
|
|
|
#endregion Cors |
|
|
|
#region SwaggerUI |
|
|
|
builder.Services.AddEndpointsApiExplorer(); |
|
builder.Services.AddSwaggerGen(c => |
|
{ |
|
c.SwaggerDoc("v1.0", new OpenApiInfo |
|
{ |
|
Version = "v1.0", |
|
Title = "WeiCloud.IoT",//左侧大标题名称 |
|
Description = "安消一体化平台", |
|
Contact = new OpenApiContact |
|
{ |
|
Name = "hi7t", |
|
Email = "", |
|
Url = null |
|
} |
|
}); |
|
//c.OperationFilter<AddProjectHeaderParameter>(); |
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; |
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); |
|
c.IncludeXmlComments(xmlPath, true); |
|
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); |
|
}); |
|
|
|
#endregion SwaggerUI |
|
|
|
builder.Services.AddLogging(m => { m.AddNLog(); }); |
|
|
|
#region Autofac |
|
|
|
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()) |
|
.ConfigureContainer<ContainerBuilder>(builder => |
|
{ |
|
builder.RegisterModule(new ConfigureAutofac()); |
|
}); |
|
|
|
#endregion Autofac |
|
|
|
#region CAP注册 |
|
|
|
var capGroupName = builder.Configuration["Cap:CapGroupName"]; |
|
var capDBConnection = builder.Configuration["Cap:DBConnection"]; |
|
var capRedisConnection = builder.Configuration["Cap:RedisConnection"]?.ToString(); |
|
|
|
if (string.IsNullOrEmpty(capGroupName)) |
|
{ |
|
capGroupName = "info-group-api-msg"; |
|
} |
|
builder.Services.AddCap(x => |
|
{ |
|
x.UseMySql(x => |
|
{ |
|
x.ConnectionString = capDBConnection.ToString(); |
|
x.TableNamePrefix = capGroupName; |
|
}); |
|
x.UseRedis(capRedisConnection); |
|
x.DefaultGroupName = capGroupName; |
|
}); |
|
|
|
#endregion CAP注册 |
|
|
|
// 设置全局默认请求体大小限制 |
|
builder.WebHost.ConfigureKestrel(options => |
|
{ |
|
options.Limits.MaxRequestBodySize = 200 * 1024 * 1024; // 默认 200MB |
|
}); |
|
|
|
#region 注册大华token 服务 |
|
|
|
builder.Services.AddSingleton<ITokenProviderService, TokenProviderService>(); |
|
|
|
#endregion 注册大华token 服务 |
|
|
|
var app = builder.Build(); |
|
|
|
if (app.Environment.IsDevelopment()) |
|
{ |
|
app.UseSwagger(); |
|
app.UseSwaggerUI(c => |
|
{ |
|
c.SwaggerEndpoint("/swagger/v1.0/swagger.json", "WeiCloud.IoT-v1.0"); |
|
}); |
|
} |
|
|
|
app.UseHttpsRedirection(); |
|
|
|
app.UseAuthorization(); |
|
|
|
app.MapControllers(); |
|
// 创建 Startup 实例 |
|
var startup = new Startup(builder.Configuration); |
|
startup.Configure(app, app.Environment, builder.Configuration); |
|
app.Run(); |
|
} |
|
catch (Exception exception) |
|
{ |
|
// NLog: catch setup errors |
|
logger.Error(exception, "Stopped program because of exception"); |
|
throw; |
|
} |
|
finally |
|
{ |
|
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux) |
|
NLog.LogManager.Shutdown(); |
|
} |
|
} |
|
} |
|
} |