using LiveForum.Model.Dto.Others; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; namespace LiveForum.WebApi.Controllers { /// /// 配置测试控制器(测试配置绑定,生产环境应删除) /// [ApiController] [Route("api/[controller]")] public class ConfigTestController : ControllerBase { private readonly IOptionsSnapshot _cosConfigSnapshot; private readonly IOptionsSnapshot _appSettingsSnapshot; private readonly IConfiguration _configuration; public ConfigTestController( IOptionsSnapshot cosConfigSnapshot, IOptionsSnapshot appSettingsSnapshot, IConfiguration configuration) { _cosConfigSnapshot = cosConfigSnapshot; _appSettingsSnapshot = appSettingsSnapshot; _configuration = configuration; } /// /// 测试腾讯云COS配置绑定 /// [HttpGet("test-cos-config")] public IActionResult TestCosConfig() { var config = _cosConfigSnapshot.Value; return Ok(new { Message = "腾讯云COS配置绑定测试", Config = new { config.AppId, config.Region, SecretId = MaskSecret(config.SecretId), SecretKey = MaskSecret(config.SecretKey), config.BucketName, config.DomainUrl, config.MaxSize, config.DurationSecond, config.Prefixes }, Note = "SecretId 和 SecretKey 已脱敏显示" }); } /// /// 测试应用配置绑定 /// [HttpGet("test-app-settings")] public IActionResult TestAppSettings() { var settings = _appSettingsSnapshot.Value; return Ok(new { Message = "应用配置绑定测试", Settings = new { settings.UserDefaultIcon, settings.UserDefaultName } }); } /// /// 对比原始配置和绑定后的配置 /// [HttpGet("compare-configs")] public IActionResult CompareConfigs() { var cosConfig = _cosConfigSnapshot.Value; return Ok(new { Message = "配置绑定对比", 原始配置格式 = new { APPID = _configuration["TENCENT_COS:AppId"], REGION = _configuration["TENCENT_COS:Region"], SECRET_ID = MaskSecret(_configuration["TENCENT_COS:SecretId"]), BUCKET_NAME = _configuration["TENCENT_COS:BucketName"], MAX_SIZE = _configuration["TENCENT_COS:MaxSize"] }, 绑定后的模型 = new { cosConfig.AppId, cosConfig.Region, SecretId = MaskSecret(cosConfig.SecretId), cosConfig.BucketName, cosConfig.MaxSize }, 说明 = new[] { "配置文件使用 PascalCase (AppId, Region)", ".NET 配置绑定自动处理大小写和下划线", "推荐使用规范的 PascalCase 命名" } }); } /// /// 测试配置热更新 /// [HttpGet("test-hot-reload")] public IActionResult TestHotReload() { var cosConfig = _cosConfigSnapshot.Value; var appSettings = _appSettingsSnapshot.Value; return Ok(new { Message = "配置热更新测试", 说明 = "在 AgileConfig 中修改配置后,刷新此接口查看配置是否更新", 当前时间 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), COS配置 = new { cosConfig.Region, cosConfig.BucketName, cosConfig.MaxSize, cosConfig.DomainUrl }, 应用配置 = new { appSettings.UserDefaultName, appSettings.UserDefaultIcon }, 测试步骤 = new[] { "1. 记录当前配置值", "2. 在 AgileConfig 中修改配置", "3. 刷新此接口", "4. 对比配置值是否已更新" } }); } /// /// 脱敏显示敏感信息 /// private string MaskSecret(string secret) { if (string.IsNullOrEmpty(secret) || secret.Length <= 8) { return "***"; } return secret.Substring(0, 4) + "***" + secret.Substring(secret.Length - 4); } } }