2026-07-06 14:21:21 +08:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.AspNetCore.ResponseCompression;
|
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Common.Extensions;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Common.Helpers;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.Autofacs;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.Context;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.Extensions;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.Middleware;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.RabbitMQ;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.Redis;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.SDK;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Enum;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Settings;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Repository;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Repository.Core;
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
|
using SqlSugar.IOC;
|
|
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
using Yitter.IdGenerator;
|
|
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
|
|
builder.Configuration.AddJsonFile("medal-rule-config.json", optional: false, reloadOnChange: true);
|
|
|
|
|
|
|
2026-07-10 10:44:00 +08:00
|
|
|
|
var snowflakeSettings = builder.Configuration.GetSection("SnowflakeSettings").Get<SnowflakeSettings>() ?? new SnowflakeSettings { WorkerId = 1 };
|
|
|
|
|
|
YitIdHelper.SetIdGenerator(new IdGeneratorOptions { WorkerId = snowflakeSettings.WorkerId });
|
2026-07-06 14:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
builder.UseAutofac();
|
|
|
|
|
|
|
|
|
|
|
|
builder.InitSqlSugarDb(new IocConfig
|
|
|
|
|
|
{
|
|
|
|
|
|
ConfigId = 0,
|
|
|
|
|
|
DbType = IocDbType.MySql,
|
|
|
|
|
|
ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection"),
|
|
|
|
|
|
IsAutoCloseConnection = true,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
|
|
|
|
.ReadFrom.Configuration(builder.Configuration)
|
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
|
|
|
|
builder.Host.UseSerilog();
|
2026-07-07 18:01:17 +08:00
|
|
|
|
builder.AddInteractiveMagazineApiDefaults();
|
2026-07-06 14:21:21 +08:00
|
|
|
|
builder.Services.AddScoped(typeof(BaseRepository<>));
|
|
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSwaggerGen(option =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var xmlFile = $"{AppDomain.CurrentDomain.FriendlyName}.xml";
|
|
|
|
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
|
|
|
|
var modelXml = Path.Combine(AppContext.BaseDirectory, "QYZH.InteractiveMagazine.Models.xml");
|
|
|
|
|
|
var version = ApiVersionEnum.Wechat;
|
|
|
|
|
|
|
|
|
|
|
|
option.SwaggerDoc(version.ToString(), new OpenApiInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
Title = AppDomain.CurrentDomain.FriendlyName,
|
|
|
|
|
|
Version = "互动期刊接口文档",
|
|
|
|
|
|
Description = $"{version.GetDescription()}接口,Last Modify Time:{new FileInfo(xmlPath).LastWriteTime:yyyy-MM-dd HH:mm:ss}"
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
option.OrderActionsBy(o => o.RelativePath);
|
|
|
|
|
|
|
|
|
|
|
|
if (File.Exists(xmlPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
option.IncludeXmlComments(xmlPath, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (File.Exists(modelXml))
|
|
|
|
|
|
{
|
|
|
|
|
|
option.IncludeXmlComments(modelXml, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
|
|
|
|
{
|
|
|
|
|
|
Description = "请输入 Token,格式为 Bearer Token",
|
|
|
|
|
|
Name = "Authorization",
|
|
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
|
|
Type = SecuritySchemeType.ApiKey,
|
|
|
|
|
|
BearerFormat = "JWT",
|
|
|
|
|
|
Scheme = "Bearer"
|
|
|
|
|
|
});
|
|
|
|
|
|
option.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
|
|
{
|
|
|
|
|
|
{
|
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
|
{
|
|
|
|
|
|
Reference = new OpenApiReference
|
|
|
|
|
|
{
|
|
|
|
|
|
Type = ReferenceType.SecurityScheme,
|
|
|
|
|
|
Id = "Bearer"
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
[]
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
option.DocInclusionPredicate((docName, apiDesc) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!apiDesc.TryGetMethodInfo(out var methodInfo))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var groupName = methodInfo.DeclaringType?
|
|
|
|
|
|
.GetCustomAttributes(true)
|
|
|
|
|
|
.OfType<ApiExplorerSettingsAttribute>()
|
|
|
|
|
|
.FirstOrDefault()?
|
|
|
|
|
|
.GroupName;
|
|
|
|
|
|
|
|
|
|
|
|
return groupName == docName;
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
|
app.UseSwaggerUI(c =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var version = ApiVersionEnum.Wechat;
|
|
|
|
|
|
c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{version.GetDescription()}接口");
|
|
|
|
|
|
c.DocExpansion(DocExpansion.None);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
app.UseServiceContext();
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
app.UseCors("AllowAll");
|
|
|
|
|
|
app.UseMiddleware<GlobalExceptionMiddleware>();
|
|
|
|
|
|
app.UseMiddleware<OperationLogMiddleware>();
|
|
|
|
|
|
app.UseMiddleware<JwtAutoRefreshMiddleware>();
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
|
|
|
|
app.Run();
|