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.
123 lines
4.2 KiB
123 lines
4.2 KiB
using Autofac; |
|
using Autofac.Extensions.DependencyInjection; |
|
using Common.Shared.API.Infrastructure; |
|
using Microsoft.OpenApi.Models; |
|
using NLog; |
|
using NLog.Extensions.Logging; |
|
using NLog.Web; |
|
using System.Reflection; |
|
|
|
namespace Common.Shared.API |
|
{ |
|
public class Program |
|
{ |
|
public static void Main(string[] args) |
|
{ |
|
var logger = 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 |
|
|
|
builder.Services.AddCors(options => |
|
{ |
|
options.AddPolicy("_myAllowSpecificOrigins", |
|
builder => |
|
{ |
|
builder.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 |
|
|
|
// 设置全局默认请求体大小限制 |
|
builder.WebHost.ConfigureKestrel(options => |
|
{ |
|
options.Limits.MaxRequestBodySize = 200 * 1024 * 1024; // 默认 200MB |
|
}); |
|
|
|
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(); |
|
} |
|
} |
|
} |
|
} |