|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace DockerStudyApi.Controllers
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 配置测试控制器
|
|
|
/// </summary>
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
[ApiController]
|
|
|
public class DemoController : ControllerBase
|
|
|
{
|
|
|
private readonly ILogger<DemoController> _logger;
|
|
|
private readonly IOptions<TestOption> _tespOption;
|
|
|
private readonly IOptionsMonitor<TestOption> _monitorTestOption;
|
|
|
private readonly IOptionsMonitorCache<TestOption> _monitorCacheTestOption;
|
|
|
private readonly IOptionsSnapshot<TestOption> _snapshotTestOption;
|
|
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
|
public DemoController
|
|
|
(
|
|
|
ILogger<DemoController> logger,
|
|
|
IOptions<TestOption> tespOption,
|
|
|
IOptionsMonitor<TestOption> monitorTestOption,
|
|
|
IOptionsMonitorCache<TestOption> monitorCacheTestOption,
|
|
|
IOptionsSnapshot<TestOption> snapshotTestOption,
|
|
|
IConfiguration configuration
|
|
|
)
|
|
|
{
|
|
|
_logger = logger;
|
|
|
_tespOption = tespOption;
|
|
|
_monitorTestOption = monitorTestOption;
|
|
|
_monitorCacheTestOption = monitorCacheTestOption;
|
|
|
_snapshotTestOption = snapshotTestOption;
|
|
|
|
|
|
_configuration = configuration;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 服务测试
|
|
|
/// </summary>
|
|
|
[HttpGet]
|
|
|
public IActionResult Ping()
|
|
|
{
|
|
|
return Ok();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 说明:泛型左右尖括号,不能写在注释里,会生成无效的xml
|
|
|
/// 解决:用[]或{}或中文符号<> 代替,或者使用xml转义字符 <>
|
|
|
/// 获取 IOptions<TestOption>
|
|
|
/// </summary>
|
|
|
[HttpGet]
|
|
|
public TestOption TestOption()
|
|
|
{
|
|
|
return _tespOption.Value;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取 IOptionsMonitor<TestOptionT>
|
|
|
/// </summary>
|
|
|
[HttpGet]
|
|
|
public TestOption MonitorTestOption()
|
|
|
{
|
|
|
_monitorTestOption.OnChange(_ => _logger.LogWarning(_monitorTestOption.CurrentValue.AppId));
|
|
|
|
|
|
return _monitorTestOption.CurrentValue;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取 IOptionsMonitorCache<TestOption>
|
|
|
/// </summary>
|
|
|
[HttpGet]
|
|
|
public TestOption MonitorCacheTestOption()
|
|
|
{
|
|
|
return _monitorCacheTestOption.GetOrAdd("demo",()=> new TestOption());
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取 IOptionsSnapshot<TestOption>
|
|
|
/// </summary>
|
|
|
[HttpGet]
|
|
|
public TestOption SnapshotTestOption()
|
|
|
{
|
|
|
return _snapshotTestOption.Value;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取命令参数
|
|
|
/// (仅支持字符串或Key=value两种形式)
|
|
|
/// </summary>
|
|
|
[HttpGet]
|
|
|
public IActionResult CmdPara(string cmdName)
|
|
|
{
|
|
|
var cmdArgs = System.Environment.GetCommandLineArgs();
|
|
|
if (cmdArgs == null)
|
|
|
{
|
|
|
return Ok("启动时没有设置:命令行参数");
|
|
|
}
|
|
|
|
|
|
var kvArgs = new Dictionary<string,string>();
|
|
|
var singleArgs= new List<string>();
|
|
|
|
|
|
foreach (var arg in cmdArgs)
|
|
|
{
|
|
|
var item = arg.Split('=').ToList();
|
|
|
if (item.Count ==1)
|
|
|
{
|
|
|
singleArgs.Add(item[0]);
|
|
|
}
|
|
|
|
|
|
if (item.Count == 2)
|
|
|
{
|
|
|
kvArgs.Add(item[0], item[1]);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
var result =string.Empty;
|
|
|
|
|
|
var kvArg = kvArgs.FirstOrDefault(q=>q.Key == cmdName);
|
|
|
|
|
|
if (kvArg.Key == cmdName)
|
|
|
{
|
|
|
result = kvArg.Value;
|
|
|
|
|
|
return Ok($"{cmdName} 命令参数值为:{kvArg.Value} ");
|
|
|
}
|
|
|
|
|
|
var singleArg = singleArgs.FirstOrDefault(q=> q == cmdName);
|
|
|
return Ok(singleArg ?? $"无 {cmdName} 命令参数 ");
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取环境变量
|
|
|
/// </summary>
|
|
|
[HttpGet]
|
|
|
public IActionResult Evn(string envName)
|
|
|
{
|
|
|
var envValue = System.Environment.GetEnvironmentVariable(envName);
|
|
|
return Ok(envValue??"无此环境变量");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取配置项
|
|
|
/// </summary>
|
|
|
[HttpGet]
|
|
|
public IActionResult ConfigItem(string itemName)
|
|
|
{
|
|
|
var envValue = _configuration.GetValue<string>(itemName);
|
|
|
return Ok(envValue ?? "无此配置变量");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取所有配置项
|
|
|
/// </summary>
|
|
|
[HttpGet]
|
|
|
public IActionResult Info()
|
|
|
{
|
|
|
var cnfigText = Newtonsoft.Json.JsonConvert.SerializeObject(_configuration.GetChildren());
|
|
|
return Ok(cnfigText);
|
|
|
}
|
|
|
}
|
|
|
}
|