2026-06-30 17:41:00 +08:00
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.RabbitMQ;
|
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.Redis;
|
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.SDK;
|
|
|
|
|
using QYZH.InteractiveMagazine.PrintWorker.Consumers;
|
|
|
|
|
using Serilog;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using SqlSugar.IOC;
|
2026-07-01 10:56:18 +08:00
|
|
|
using System.Text;
|
2026-06-30 17:41:00 +08:00
|
|
|
using Yitter.IdGenerator;
|
|
|
|
|
|
2026-07-01 10:56:18 +08:00
|
|
|
Directory.SetCurrentDirectory(AppContext.BaseDirectory);
|
|
|
|
|
|
|
|
|
|
var builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings
|
|
|
|
|
{
|
|
|
|
|
Args = args,
|
|
|
|
|
ContentRootPath = AppContext.BaseDirectory
|
|
|
|
|
});
|
|
|
|
|
const string serviceName = "QYZH.InteractiveMagazine.PrintWorker";
|
2026-06-30 17:41:00 +08:00
|
|
|
|
|
|
|
|
builder.Configuration
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
|
|
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
|
|
|
|
|
2026-07-01 10:56:18 +08:00
|
|
|
var logDirectory = Path.Combine(AppContext.BaseDirectory, "logs");
|
|
|
|
|
Directory.CreateDirectory(logDirectory);
|
|
|
|
|
var logFilePath = Path.Combine(logDirectory, "printworker-log-.txt");
|
|
|
|
|
|
2026-06-30 17:41:00 +08:00
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
|
|
|
.ReadFrom.Configuration(builder.Configuration)
|
2026-07-01 10:56:18 +08:00
|
|
|
.WriteTo.File(logFilePath, rollingInterval: RollingInterval.Day, encoding: Encoding.UTF8)
|
2026-06-30 17:41:00 +08:00
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSerilog();
|
2026-07-01 10:56:18 +08:00
|
|
|
builder.Services.AddWindowsService(options => options.ServiceName = serviceName);
|
2026-06-30 17:41:00 +08:00
|
|
|
|
|
|
|
|
YitIdHelper.SetIdGenerator(new IdGeneratorOptions { WorkerId = 3 });
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSqlSugar(new IocConfig
|
|
|
|
|
{
|
|
|
|
|
ConfigId = 0,
|
|
|
|
|
DbType = IocDbType.MySql,
|
|
|
|
|
ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection"),
|
|
|
|
|
IsAutoCloseConnection = true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
SugarIocServices.ConfigurationSugar(db =>
|
|
|
|
|
{
|
|
|
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
{
|
|
|
|
|
Log.Information("[SQL] {Sql}", UtilMethods.GetSqlString((DbType)IocDbType.MySql, sql, pars));
|
|
|
|
|
};
|
|
|
|
|
db.Aop.OnError = ex =>
|
|
|
|
|
{
|
|
|
|
|
Log.Error(ex, "[SQL Error] {Message}", ex.Message);
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddScoped<ISqlSugarClient>(_ => DbScoped.SugarScope);
|
|
|
|
|
builder.Services.AddCSRedisCacheExtension(builder.Configuration.GetSection("RedisSettings"));
|
|
|
|
|
builder.Services.AddHttpClient();
|
|
|
|
|
builder.Services.AddSDKService(builder.Configuration);
|
|
|
|
|
builder.Services.AddRabbitMQ(builder.Configuration);
|
|
|
|
|
|
|
|
|
|
builder.Services.AddScoped<IQueueConsumer, AutoDotCodeConsumer>();
|
|
|
|
|
builder.Services.AddHostedService<RabbitMQHostedService>();
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
2026-07-01 10:56:18 +08:00
|
|
|
Log.Information("{ServiceName} 已启动,仅消费自动铺码队列", serviceName);
|
2026-06-30 17:41:00 +08:00
|
|
|
|
|
|
|
|
await app.RunAsync();
|