using Autofac;
|
using Autofac.Core;
|
using Autofac.Extensions.DependencyInjection;
|
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.FileProviders;
|
using Newtonsoft.Json;
|
using Newtonsoft.Json.Converters;
|
using Newtonsoft.Json.Serialization;
|
using Quartz;
|
using Serilog;
|
using System.Net.Security;
|
using System.Reflection;
|
using System.Runtime.ConstrainedExecution;
|
using System.Text;
|
using WIDESEA_Core;
|
using WIDESEA_Core.Authorization;
|
using WIDESEA_Core.BaseServices;
|
using WIDESEA_Core.Core;
|
using WIDESEA_Core.Extensions;
|
using WIDESEA_Core.Filter;
|
using WIDESEA_Core.Helper;
|
//using WIDESEA_Core.HostedService;
|
using WIDESEA_Core.Middlewares;
|
using WIDESEA_WMSServer.Filter;
|
using WIDESEA_WMSServer.Jobs;
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
//// ÅäÖÃSerilog£¨Ê¾ÀýÅäÖã¬Ò²¿Éͨ¹ýappsettings.jsonÅäÖã©
|
//Log.Logger = new LoggerConfiguration()
|
// .MinimumLevel.Information()
|
// .WriteTo.Console()
|
// .WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day)
|
// .CreateLogger();
|
|
Log.Logger = new LoggerConfiguration()
|
.MinimumLevel.Information() // ÉèÖÃÈ«¾Ö×îµÍÈÕÖ¾¼¶±ðΪ Information
|
// ÖØÐ´ÌØ¶¨ÃüÃû¿Õ¼äµÄÈÕÖ¾¼¶±ð£¬¹ýÂËϵͳÔëÒô
|
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning) // Microsoft Ïà¹ØÈÕÖ¾Ö»ÏÔʾ Warning ¼°ÒÔÉÏ
|
.MinimumLevel.Override("Microsoft.AspNetCore", Serilog.Events.LogEventLevel.Warning) // ÌØ±ð¹ýÂË AspNetCore µÄÈÕÖ¾
|
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", Serilog.Events.LogEventLevel.Information) // µ«±£ÁôÆô¶¯/¹Ø±ÕÐÅÏ¢
|
.Enrich.FromLogContext() // ·á¸»ÈÕÖ¾ÉÏÏÂÎÄ£¬¶ÔÓÚºóÐøÀ©Õ¹ºÜÓÐÓÃ
|
.WriteTo.Console( // ÅäÖÃÊä³öµ½¿ØÖÆÌ¨
|
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}" // ¶¨Òå¼ò½àµÄÊä³öÄ£°å
|
)
|
.WriteTo.File( // ÅäÖÃÊä³öµ½Îļþ
|
path: "logs/log-.txt", // ÈÕÖ¾Îļþ·¾¶£¬»á×Ô¶¯´´½¨ logs Îļþ¼Ð
|
rollingInterval: RollingInterval.Day, // °´Ìì¹ö¶¯ÈÕÖ¾Îļþ
|
retainedFileCountLimit: 7, // ×î¶à±£Áô7ÌìµÄÈÕÖ¾
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
|
)
|
.CreateLogger();
|
|
|
builder.Host.UseSerilog(); // ʹÓÃSerilog×÷ΪÈÕÖ¾Ìṩ³ÌÐò
|
|
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()).ConfigureContainer<ContainerBuilder>(builder =>
|
{
|
builder.RegisterModule<AutofacModuleRegister>();//´øÓнӿڲãµÄ·þÎñ×¢Èë
|
builder.RegisterModule<AutofacPropertityModuleReg>();//
|
}).ConfigureAppConfiguration((hostingContext, config) =>
|
{
|
hostingContext.Configuration.ConfigureApplication();
|
config.Sources.Clear();
|
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
}); ;
|
builder.ConfigureApplication();
|
|
//App.ExpDateTime = DateTime.Parse("2025-03-31 00:00:00");//¹ýÆÚʱ¼ä
|
|
// 2¡¢ÅäÖ÷þÎñ
|
builder.Services.AddSingleton(new AppSettings(builder.Configuration));//×¢²á
|
builder.Services.AddAllOptionRegister();//¶ÁÈ¡ÅäÖÃÎļþ
|
builder.Services.AddMemoryCacheSetup();//»º´æ
|
builder.Services.AddWebSocketSetup();
|
builder.Services.AddSqlsugarSetup();//SqlSugar Æô¶¯·þÎñ
|
builder.Services.AddDbSetup();//Db Æô¶¯·þÎñ
|
builder.Services.AddInitializationHostServiceSetup();//Ó¦Óóõʼ»¯·þÎñ×¢Èë
|
//builder.Services.AddHostedService<PermissionDataHostService>();//Êý¾ÝȨÏÞ
|
builder.Services.AddAutoMapperSetup();
|
|
builder.Services.AddCorsSetup();
|
|
builder.Services.AddMiniProfilerSetup();
|
|
builder.Services.AddSwaggerSetup();
|
|
builder.Services.AddHttpContextSetup();
|
|
|
builder.Services.AddMvc(options =>
|
{
|
options.Filters.Add(typeof(ApiAuthorizeFilter));
|
options.Filters.Add(typeof(ActionExecuteFilter));
|
});
|
|
builder.Services.AddAuthorizationSetup();
|
|
builder.Services.AddIpPolicyRateLimitSetup(builder.Configuration);//IPLimitÏÞÁ÷ Æô¶¯·þÎñ
|
|
builder.Services.AddScoped<UseServiceDIAttribute>();
|
|
builder.Services.AddSession();
|
|
builder.Services.AddHttpClient();
|
|
builder.Services.AddControllers(o =>
|
{
|
o.Filters.Add(typeof(GlobalExceptionsFilter));//È«¾ÖÒì³£
|
})
|
.AddNewtonsoftJson(options =>
|
{
|
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
|
//options.SerializerSettings.ContractResolver = new DefaultContractResolver();
|
options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
|
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
options.SerializerSettings.NullValueHandling = NullValueHandling.Include;
|
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
|
options.SerializerSettings.Converters.Add(new StringEnumConverter());
|
});
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
builder.Services.AddHttpClient("ESSUrl", (serviceProvider, client) =>
|
{
|
var essurl = AppSettings.GetValue("ESSUrl").ObjToString();
|
client.BaseAddress = new Uri(essurl!);
|
client.Timeout = TimeSpan.FromSeconds(30);
|
|
});
|
|
builder.Services.AddHttpClient("MESUrl", (serviceProvider, client) =>
|
{
|
var mesurl = AppSettings.GetValue("MESUrl").ObjToString();
|
client.BaseAddress = new Uri(mesurl!);
|
client.Timeout = TimeSpan.FromSeconds(30);
|
|
});
|
|
builder.Services.AddHttpClient("ERPUrl", (serviceProvider, client) =>
|
{
|
var erpurl = AppSettings.GetValue("ERPUrl").ObjToString();
|
client.BaseAddress = new Uri(erpurl!);
|
client.Timeout = TimeSpan.FromSeconds(30);
|
|
}) ;
|
|
builder.Services.AddQuartz(q =>
|
{
|
|
var jobKey = new JobKey("ErpJob");
|
q.AddJob<ErpJob>(opts => opts.WithIdentity(jobKey));
|
|
|
// Ìí¼Ó´¥·¢Æ÷ - ÿÌì10:00, 14:00, 20:00Ö´ÐÐ
|
q.AddTrigger(opts => opts
|
.ForJob(jobKey)
|
.WithIdentity("ErpJob-trigger")
|
.WithCronSchedule("0 0 10,14,20 * * ?"));
|
|
var inventoryLockJobKey = new JobKey("InventoryLockJob");
|
q.AddJob<InventoryLockJob>(opts => opts.WithIdentity(inventoryLockJobKey));
|
|
q.AddTrigger(opts => opts
|
.ForJob(inventoryLockJobKey)
|
.WithIdentity("InventoryLockJob-trigger")
|
.WithCronSchedule("0 0/10 * * * ?")); // ÿ10·ÖÖÓÖ´ÐÐÒ»´Î
|
|
});
|
|
|
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
|
var app = builder.Build();
|
|
|
app.UseMiniProfiler();//ÐÔÄÜ·ÖÎöÆ÷
|
app.ConfigureApplication();//ÅäÖÃÎļþ
|
app.UseApplicationSetup();//Æô¶¯ÅäÖÃ
|
|
app.UseAllServicesMiddle(builder.Services);
|
|
app.UseSession();
|
app.UseSwaggerAuthorized();
|
app.UseSwaggerMiddle(() => Assembly.GetExecutingAssembly().GetManifestResourceStream("WIDESEA_WMSServer.index.html") ?? throw new Exception("WIDESEA_WMSServer.index.htmlÎļþ"));
|
app.UseIpLimitMiddle();
|
app.UseApiLogMiddleware();
|
//todo
|
//app.UseRecordAccessLogsMiddle();
|
|
app.UseCors(AppSettings.Get(new string[] { "Cors", "PolicyName" }));
|
|
DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
|
defaultFilesOptions.DefaultFileNames.Clear();
|
defaultFilesOptions.DefaultFileNames.Add("index.html");
|
app.UseDefaultFiles(defaultFilesOptions);
|
app.UseMiddleware<HttpRequestMiddleware>();
|
app.UseStaticFiles();
|
|
app.UseStaticFiles(new StaticFileOptions
|
{
|
FileProvider = new PhysicalFileProvider(App.WebHostEnvironment.WebRootPath)
|
});
|
|
app.UseCookiePolicy();
|
app.UseStatusCodePages();
|
|
app.UseRouting();
|
|
app.UseAuthentication();
|
app.UseAuthorization();
|
|
app.MapControllers();
|
|
app.Run();
|