2026-10-12 16:22:04 +08:00
|
|
|
using System.Text.Json;
|
|
|
|
|
using AdminApi;
|
|
|
|
|
using AdminHost.Extension;
|
|
|
|
|
using AdminHost.Jobs;
|
|
|
|
|
using AdminHost.Middleware;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
using AdminService;
|
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
|
|
|
|
using Common.Request;
|
|
|
|
|
|
|
|
|
|
using JWTHelper;
|
|
|
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using MongoRepository.Config;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NLog.Extensions.Logging;
|
|
|
|
|
using NLog.Web;
|
|
|
|
|
using RedisHelper;
|
|
|
|
|
using ScheduledService;
|
|
|
|
|
using SharpAbp.Infrastructure.Api.Middleware;
|
|
|
|
|
|
|
|
|
|
Environment.SetEnvironmentVariable("SERVICE_NAME",
|
|
|
|
|
Environment.GetEnvironmentVariable("SERVICE_NAME")
|
|
|
|
|
?? System.Reflection.Assembly.GetEntryAssembly()?.GetName().Name
|
|
|
|
|
?? "AdminHost");
|
|
|
|
|
Environment.SetEnvironmentVariable("INSTANCE_ID",
|
|
|
|
|
Environment.GetEnvironmentVariable("INSTANCE_ID") ?? "default");
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
|
builder.Services.AddControllers()
|
|
|
|
|
.AddApplicationPart(typeof(AssemblyMarker).Assembly)
|
|
|
|
|
.AddControllersAsServices()
|
|
|
|
|
.ConfigureApiBehaviorOptions(options =>
|
|
|
|
|
{
|
|
|
|
|
options.SuppressModelStateInvalidFilter = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
builder.Logging.AddNLog();
|
|
|
|
|
builder.Host.UseNLog();
|
|
|
|
|
|
|
|
|
|
builder.Services.AddControllers().AddNewtonsoftJson(
|
|
|
|
|
option => {
|
|
|
|
|
option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
option.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
|
|
|
|
|
option.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
|
|
|
|
|
option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
builder.Configuration.AddJsonFile(Path.Combine("Config", "system.json"), optional: true);
|
|
|
|
|
builder.Services.Configure<MongoDbSettings>(builder.Configuration.GetSection(nameof(MongoDbSettings)));
|
|
|
|
|
builder.Services.AddSingleton<IMongoDbSettings>(sp => sp.GetRequiredService<IOptions<MongoDbSettings>>().Value);
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
|
|
|
builder.Services.AddSingleton<IRequestApi, RequestApi>();
|
|
|
|
|
builder.Services.AddAutoMapper(typeof(AutoMapperConfig));
|
|
|
|
|
|
|
|
|
|
builder.Services.AddHealthChecks()
|
|
|
|
|
.AddCheck("self", () => HealthCheckResult.Healthy(), tags: new[] { "self" });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("Cors", policy =>
|
|
|
|
|
{
|
|
|
|
|
policy
|
|
|
|
|
.SetIsOriginAllowed(_ => true)
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowCredentials();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSignalR();
|
|
|
|
|
|
|
|
|
|
#region jwt
|
|
|
|
|
builder.Services.AddCustomJwt(builder.Configuration);
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region redis
|
|
|
|
|
builder.Services.AddRedisCache(builder.Configuration);
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Cap
|
|
|
|
|
builder.Services.AddCap(builder.Configuration);
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
builder.Services.AddCronJob<DailyTaskJob>(c =>
|
|
|
|
|
{
|
|
|
|
|
c.TimeZoneInfo = TimeZoneInfo.Local;
|
|
|
|
|
c.CronExpression = @"0 0 0 * * *";
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-18 17:28:45 +08:00
|
|
|
#region Swagger
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
|
builder.Services.AddSwaggerGen(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
|
|
|
|
|
{
|
|
|
|
|
Title = "AdminApi",
|
|
|
|
|
Description = "游戏后台管理系统",
|
|
|
|
|
Version = "v1"
|
|
|
|
|
});
|
|
|
|
|
// 加载各项目生成的 XML 注释,用于展示接口的中文说明
|
|
|
|
|
foreach (var xml in System.IO.Directory.GetFiles(AppContext.BaseDirectory, "*.xml"))
|
|
|
|
|
{
|
|
|
|
|
try { c.IncludeXmlComments(xml, true); } catch { }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
#endregion
|
|
|
|
|
|
2026-10-12 16:22:04 +08:00
|
|
|
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
|
|
|
|
|
builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.RegisterModule<AutofacModule>();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 17:28:45 +08:00
|
|
|
#region Swagger
|
|
|
|
|
app.UseSwagger(c => c.RouteTemplate = "adminswagger/{documentName}/swagger.json");
|
|
|
|
|
app.UseSwaggerUI(c =>
|
|
|
|
|
{
|
|
|
|
|
c.RoutePrefix = "adminswagger";
|
|
|
|
|
c.SwaggerEndpoint("v1/swagger.json", "AdminApi v1");
|
|
|
|
|
});
|
|
|
|
|
#endregion
|
|
|
|
|
|
2026-10-12 16:22:04 +08:00
|
|
|
app.UseCors("Cors");
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.UseApiLogging();
|
|
|
|
|
|
|
|
|
|
app.UseOperationLog();
|
|
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
app.UseGlobalExceptionHandler();
|
|
|
|
|
app.MapHealthChecks("/health", new HealthCheckOptions
|
|
|
|
|
{
|
|
|
|
|
Predicate = _ => true,
|
|
|
|
|
ResponseWriter = async (context, report) =>
|
|
|
|
|
{
|
|
|
|
|
context.Response.ContentType = "application/json";
|
|
|
|
|
var result = new
|
|
|
|
|
{
|
|
|
|
|
status = report.Status.ToString(),
|
|
|
|
|
checks = report.Entries.Select(e => new
|
|
|
|
|
{
|
|
|
|
|
name = e.Key,
|
|
|
|
|
status = e.Value.Status.ToString(),
|
|
|
|
|
description = e.Value.Description,
|
|
|
|
|
duration = e.Value.Duration.TotalMilliseconds
|
|
|
|
|
}),
|
|
|
|
|
totalDuration = report.TotalDuration.TotalMilliseconds
|
|
|
|
|
};
|
|
|
|
|
await context.Response.WriteAsync(System.Text.Json.JsonSerializer.Serialize(result));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.Run();
|