|
|
|
@ -0,0 +1,113 @@
|
|
|
|
|
using Aucma.Scada.Api.Config;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
using Serilog.Events;
|
|
|
|
|
using Serilog;
|
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
using Aucma.Scada.Api.Repository;
|
|
|
|
|
using Aucma.Scada.Api.Common;
|
|
|
|
|
|
|
|
|
|
namespace Aucma.Scada.Api
|
|
|
|
|
{
|
|
|
|
|
public class StartUp
|
|
|
|
|
{
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
|
|
public StartUp(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddControllers();
|
|
|
|
|
|
|
|
|
|
//配置Swagger
|
|
|
|
|
services.AddSwaggerGen(swagger =>
|
|
|
|
|
{
|
|
|
|
|
//自定义接口信息
|
|
|
|
|
swagger.SwaggerDoc("V1.0", new OpenApiInfo
|
|
|
|
|
{
|
|
|
|
|
Title = "MES Web Api",
|
|
|
|
|
Version = "V1.0",
|
|
|
|
|
Description = $"API版本V1.0",
|
|
|
|
|
Contact = new OpenApiContact
|
|
|
|
|
{
|
|
|
|
|
Name = "MES Web Api",
|
|
|
|
|
Email = "wenjy@mesnac.com"
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//自定义实体别名
|
|
|
|
|
swagger.CustomSchemaIds(type => type.GetCustomAttributes(typeof(DataContractAttribute), true)
|
|
|
|
|
.Cast<DataContractAttribute>()
|
|
|
|
|
.FirstOrDefault()?.Name);
|
|
|
|
|
|
|
|
|
|
//配置Action名称
|
|
|
|
|
var path = Path.Combine(AppContext.BaseDirectory, "Aucma.Scada.Api.xml");
|
|
|
|
|
swagger.IncludeXmlComments(path, true); // true : 显示控制器层注释
|
|
|
|
|
swagger.OrderActionsBy(o => o.RelativePath); // 对action的名称进行排序,如果有多个,就可以看见效果了。
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//注册配置类
|
|
|
|
|
services.AddSingleton<AppConfig>(provider =>
|
|
|
|
|
{
|
|
|
|
|
var configuration = provider.GetService<IConfiguration>();
|
|
|
|
|
return configuration.GetSection("AppConfig").Get<AppConfig>();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<JsonChange>();
|
|
|
|
|
|
|
|
|
|
//注册SqlSugar
|
|
|
|
|
services.AddSqlSugarSetup();
|
|
|
|
|
|
|
|
|
|
//注册Repository、Service
|
|
|
|
|
services.AddServices();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//启用Swagger中间件
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerEndpoint("/swagger/V1.0/swagger.json", "MES Web Api V1.0");
|
|
|
|
|
c.RoutePrefix = "";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//启用Serilog中间件
|
|
|
|
|
app.UseSerilogRequestLogging();
|
|
|
|
|
|
|
|
|
|
#region 通过配置文件读取日志存放位置
|
|
|
|
|
var appConfig = app.ApplicationServices.GetService<AppConfig>();
|
|
|
|
|
var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/";
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.Console()
|
|
|
|
|
.WriteTo.File(Path.Combine(logPath, "Info.log"), LogEventLevel.Information, fileSizeLimitBytes: 5 * 1024)
|
|
|
|
|
.WriteTo.File(Path.Combine(logPath, "Error.log"), LogEventLevel.Error, fileSizeLimitBytes: 5 * 1024)
|
|
|
|
|
.WriteTo.File(Path.Combine(logPath, "Warn.log"), LogEventLevel.Warning, fileSizeLimitBytes: 5 * 1024)
|
|
|
|
|
.WriteTo.File(Path.Combine(logPath, "Trace.log"), LogEventLevel.Verbose, fileSizeLimitBytes: 5 * 1024)
|
|
|
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
|
|
//app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
{
|
|
|
|
|
endpoints.MapControllers();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Log.Information($"项目初始化完成,日志存放路径:{appConfig.logPath}");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|