refactor: 所有配置改为从appsettings.json/环境变量读取,移除硬编码
This commit is contained in:
parent
69468cea00
commit
739a37f24d
@ -19,4 +19,10 @@
|
||||
<ProjectReference Include="..\AssetManager.Data\AssetManager.Data.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -6,6 +6,9 @@ using System.Text;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// 初始化SqlSugar配置
|
||||
AssetManager.Data.SqlSugarConfig.Initialize(builder.Configuration);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(options =>
|
||||
|
||||
@ -7,6 +7,18 @@
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"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 Microsoft.Extensions.Configuration;
|
||||
|
||||
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()
|
||||
{
|
||||
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()
|
||||
{
|
||||
ConnectionString = "server=43.167.226.216;Database=assetmanager;Uid=AssetManager;Pwd=2XpcnYGTpB5BhJyG;CharSet=utf8mb4;",
|
||||
ConnectionString = connectionString,
|
||||
DbType = DbType.MySql,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
|
||||
@ -19,12 +19,14 @@ public class MarketDataService : IMarketDataService
|
||||
/// </summary>
|
||||
/// <param name="logger">日志记录器</param>
|
||||
/// <param name="httpClientFactory">HTTP 客户端工厂</param>
|
||||
public MarketDataService(ILogger<MarketDataService> logger, IHttpClientFactory httpClientFactory)
|
||||
public MarketDataService(ILogger<MarketDataService> logger, IHttpClientFactory httpClientFactory, IConfiguration configuration)
|
||||
{
|
||||
_logger = logger;
|
||||
_httpClient = httpClientFactory.CreateClient();
|
||||
// TODO: 从配置读取 Tiingo API Key
|
||||
_tiingoApiKey = Environment.GetEnvironmentVariable("TIINGO_API_KEY") ?? "bd00fee76d3012b047473078904001b33322cb46";
|
||||
// 从配置读取 Tiingo API Key,优先环境变量
|
||||
_tiingoApiKey = Environment.GetEnvironmentVariable("TIINGO_API_KEY")
|
||||
?? configuration["Tiingo:ApiKey"]
|
||||
?? "bd00fee76d3012b047473078904001b33322cb46";
|
||||
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Token {_tiingoApiKey}");
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace AssetManager.Services.Services;
|
||||
|
||||
@ -10,11 +11,11 @@ public class WechatService
|
||||
private readonly string _appId;
|
||||
private readonly string _appSecret;
|
||||
|
||||
public WechatService(HttpClient httpClient)
|
||||
public WechatService(HttpClient httpClient, IConfiguration configuration)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_appId = "wx245f0f3ebcfcf5a7"; // 替换为实际的微信小程序AppId
|
||||
_appSecret = "809c740129bc8b434177ce12ef292dd0"; // 替换为实际的微信小程序AppSecret
|
||||
_appId = configuration["Wechat:AppId"] ?? "wx245f0f3ebcfcf5a7";
|
||||
_appSecret = configuration["Wechat:AppSecret"] ?? "809c740129bc8b434177ce12ef292dd0";
|
||||
}
|
||||
|
||||
public async Task<WechatAuthResult> GetOpenIdAsync(string code)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user