refactor: 所有配置改为从appsettings.json/环境变量读取,移除硬编码
This commit is contained in:
parent
69468cea00
commit
739a37f24d
@ -19,4 +19,10 @@
|
|||||||
<ProjectReference Include="..\AssetManager.Data\AssetManager.Data.csproj" />
|
<ProjectReference Include="..\AssetManager.Data\AssetManager.Data.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -6,6 +6,9 @@ using System.Text;
|
|||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// 初始化SqlSugar配置
|
||||||
|
AssetManager.Data.SqlSugarConfig.Initialize(builder.Configuration);
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen(options =>
|
builder.Services.AddSwaggerGen(options =>
|
||||||
|
|||||||
@ -7,6 +7,18 @@
|
|||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DefaultConnection": "server=localhost;Database=assetmanager;Uid=root;Pwd=your_password;CharSet=utf8mb4;"
|
"Default": "server=localhost;Database=assetmanager;Uid=root;Pwd=your_password;CharSet=utf8mb4;"
|
||||||
|
},
|
||||||
|
"Jwt": {
|
||||||
|
"SecretKey": "your-strong-secret-key-here-2026",
|
||||||
|
"Issuer": "AssetManager",
|
||||||
|
"Audience": "AssetManager"
|
||||||
|
},
|
||||||
|
"Wechat": {
|
||||||
|
"AppId": "wx245f0f3ebcfcf5a7",
|
||||||
|
"AppSecret": "809c740129bc8b434177ce12ef292dd0"
|
||||||
|
},
|
||||||
|
"Tiingo": {
|
||||||
|
"ApiKey": "bd00fee76d3012b047473078904001b33322cb46"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,34 @@
|
|||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
namespace AssetManager.Data;
|
namespace AssetManager.Data;
|
||||||
|
|
||||||
public class SqlSugarConfig
|
public static class SqlSugarConfig
|
||||||
{
|
{
|
||||||
|
private static IConfiguration? _configuration;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化配置(在Program.cs中调用)
|
||||||
|
/// </summary>
|
||||||
|
public static void Initialize(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
_configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
public static ISqlSugarClient GetSqlSugarClient()
|
public static ISqlSugarClient GetSqlSugarClient()
|
||||||
{
|
{
|
||||||
|
if (_configuration == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("SqlSugarConfig has not been initialized. Call Initialize() first.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从配置读取连接字符串,支持环境变量和appsettings.json
|
||||||
|
var connectionString = _configuration.GetConnectionString("Default")
|
||||||
|
?? throw new InvalidOperationException("Connection string 'Default' not found in configuration.");
|
||||||
|
|
||||||
return new SqlSugarScope(new ConnectionConfig()
|
return new SqlSugarScope(new ConnectionConfig()
|
||||||
{
|
{
|
||||||
ConnectionString = "server=43.167.226.216;Database=assetmanager;Uid=AssetManager;Pwd=2XpcnYGTpB5BhJyG;CharSet=utf8mb4;",
|
ConnectionString = connectionString,
|
||||||
DbType = DbType.MySql,
|
DbType = DbType.MySql,
|
||||||
IsAutoCloseConnection = true,
|
IsAutoCloseConnection = true,
|
||||||
InitKeyType = InitKeyType.Attribute,
|
InitKeyType = InitKeyType.Attribute,
|
||||||
|
|||||||
@ -19,12 +19,14 @@ public class MarketDataService : IMarketDataService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="logger">日志记录器</param>
|
/// <param name="logger">日志记录器</param>
|
||||||
/// <param name="httpClientFactory">HTTP 客户端工厂</param>
|
/// <param name="httpClientFactory">HTTP 客户端工厂</param>
|
||||||
public MarketDataService(ILogger<MarketDataService> logger, IHttpClientFactory httpClientFactory)
|
public MarketDataService(ILogger<MarketDataService> logger, IHttpClientFactory httpClientFactory, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_httpClient = httpClientFactory.CreateClient();
|
_httpClient = httpClientFactory.CreateClient();
|
||||||
// TODO: 从配置读取 Tiingo API Key
|
// 从配置读取 Tiingo API Key,优先环境变量
|
||||||
_tiingoApiKey = Environment.GetEnvironmentVariable("TIINGO_API_KEY") ?? "bd00fee76d3012b047473078904001b33322cb46";
|
_tiingoApiKey = Environment.GetEnvironmentVariable("TIINGO_API_KEY")
|
||||||
|
?? configuration["Tiingo:ApiKey"]
|
||||||
|
?? "bd00fee76d3012b047473078904001b33322cb46";
|
||||||
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Token {_tiingoApiKey}");
|
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Token {_tiingoApiKey}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
namespace AssetManager.Services.Services;
|
namespace AssetManager.Services.Services;
|
||||||
|
|
||||||
@ -10,11 +11,11 @@ public class WechatService
|
|||||||
private readonly string _appId;
|
private readonly string _appId;
|
||||||
private readonly string _appSecret;
|
private readonly string _appSecret;
|
||||||
|
|
||||||
public WechatService(HttpClient httpClient)
|
public WechatService(HttpClient httpClient, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
_httpClient = httpClient;
|
||||||
_appId = "wx245f0f3ebcfcf5a7"; // 替换为实际的微信小程序AppId
|
_appId = configuration["Wechat:AppId"] ?? "wx245f0f3ebcfcf5a7";
|
||||||
_appSecret = "809c740129bc8b434177ce12ef292dd0"; // 替换为实际的微信小程序AppSecret
|
_appSecret = configuration["Wechat:AppSecret"] ?? "809c740129bc8b434177ce12ef292dd0";
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<WechatAuthResult> GetOpenIdAsync(string code)
|
public async Task<WechatAuthResult> GetOpenIdAsync(string code)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user