|
|
|
using System.Reflection;
|
|
|
|
using Serilog;
|
|
|
|
using CoreAgent.Infrastructure.Extensions.Logging;
|
|
|
|
using CoreAgent.Infrastructure.Extensions.ServiceCollection;
|
|
|
|
using CoreAgent.Infrastructure.Middleware.GlobalException;
|
|
|
|
using CoreAgent.Domain.Contexts;
|
|
|
|
using CoreAgent.Domain.Models.System;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
|
|
namespace CoreAgent.API
|
|
|
|
{
|
|
|
|
public class Startup
|
|
|
|
{
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
public IHostEnvironment Environment { get; }
|
|
|
|
|
|
|
|
public Startup(IConfiguration configuration, IHostEnvironment environment)
|
|
|
|
{
|
|
|
|
Configuration = configuration;
|
|
|
|
Environment = environment;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
|
|
|
// Add MediatR
|
|
|
|
services.AddMediatR(cfg => {
|
|
|
|
cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
|
|
|
|
cfg.RegisterServicesFromAssembly(typeof(Startup).Assembly);
|
|
|
|
cfg.RegisterServicesFromAssembly(Assembly.Load("CoreAgent.Application"));
|
|
|
|
});
|
|
|
|
|
|
|
|
// 添加基础设施服务
|
|
|
|
services.AddInfrastructure(Configuration);
|
|
|
|
|
|
|
|
|
|
|
|
// 添加控制器
|
|
|
|
services.AddControllers();
|
|
|
|
|
|
|
|
// 添加API文档
|
|
|
|
services.AddEndpointsApiExplorer();
|
|
|
|
services.AddSwaggerGen();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app)
|
|
|
|
{
|
|
|
|
// 配置开发环境中间件
|
|
|
|
if (Environment.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
Log.Debug("Swagger UI enabled in development environment");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 使用基础设施中间件
|
|
|
|
app.UseInfrastructure(Configuration);
|
|
|
|
|
|
|
|
// 配置控制器路由
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
{
|
|
|
|
endpoints.MapControllers();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class StartupExtensions
|
|
|
|
{
|
|
|
|
public static WebApplicationBuilder AddConfigurations(this WebApplicationBuilder builder)
|
|
|
|
{
|
|
|
|
const string configurationsDirectory = "Configurations";
|
|
|
|
var env = builder.Environment;
|
|
|
|
|
|
|
|
// 基础配置
|
|
|
|
builder.Configuration
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
|
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
|
|
|
|
.AddEnvironmentVariables();
|
|
|
|
|
|
|
|
// 基础设施配置
|
|
|
|
builder.Configuration
|
|
|
|
.AddJsonFile($"{configurationsDirectory}/logger.json", optional: false, reloadOnChange: true)
|
|
|
|
.AddJsonFile($"{configurationsDirectory}/logger.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
|
|
|
|
.AddJsonFile($"{configurationsDirectory}/request-logging.json", optional: false, reloadOnChange: true)
|
|
|
|
.AddJsonFile($"{configurationsDirectory}/request-logging.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
|
|
|
|
.AddJsonFile($"{configurationsDirectory}/api-versioning.json", optional: false, reloadOnChange: true)
|
|
|
|
.AddJsonFile($"{configurationsDirectory}/api-versioning.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
|
|
|
|
.AddJsonFile($"{configurationsDirectory}/netcommand.json", optional: false, reloadOnChange: true)
|
|
|
|
.AddJsonFile($"{configurationsDirectory}/netcommand.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
|
|
|
|
|
|
|
var networkCommandConfig = builder.Configuration.GetSection("networkCommand").Get<NetworkCommandConfig>();
|
|
|
|
CellularNetworkContext.Instance.SetNetworkCommandConfig(networkCommandConfig);
|
|
|
|
|
|
|
|
// 添加配置
|
|
|
|
builder.Services.Configure<AppSettings>(builder.Configuration);
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration config)
|
|
|
|
{
|
|
|
|
return services
|
|
|
|
.AddLoggingService(config)
|
|
|
|
.AddApiVersioningService(config)
|
|
|
|
.AddRequestLogging(config)
|
|
|
|
.AddAuthorization()
|
|
|
|
.AddCommandCustomService()
|
|
|
|
.AddSwaggerGen();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IApplicationBuilder UseInfrastructure(this IApplicationBuilder app, IConfiguration config)
|
|
|
|
{
|
|
|
|
return app
|
|
|
|
.UseHttpsRedirection()
|
|
|
|
.UseGlobalExceptionMiddleware()
|
|
|
|
.UseAuthorization()
|
|
|
|
.UseRequestLogging(config)
|
|
|
|
.UseApiVersioningExtension();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void ConfigureSwagger(this WebApplication app)
|
|
|
|
{
|
|
|
|
//if (app.Environment.IsDevelopment())
|
|
|
|
//{
|
|
|
|
// app.UseSwagger();
|
|
|
|
// app.UseSwaggerUI();
|
|
|
|
//}
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 新增:创建Startup实例的扩展方法
|
|
|
|
public static Startup CreateStartup(this WebApplicationBuilder builder)
|
|
|
|
{
|
|
|
|
return new Startup(builder.Configuration, builder.Environment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|