105 lines
2.3 KiB
Markdown
105 lines
2.3 KiB
Markdown
|
|
# 配置管理
|
||
|
|
|
||
|
|
> 所属模块:[架构概览](./概览.md)
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 1. 配置文件结构
|
||
|
|
|
||
|
|
```json
|
||
|
|
// Configuration/appsettings.json
|
||
|
|
{
|
||
|
|
"Database": {
|
||
|
|
"Type": "sqlite",
|
||
|
|
"Path": "./data/database/wctminer.db"
|
||
|
|
},
|
||
|
|
"Parser": {
|
||
|
|
"BatchSize": 1000
|
||
|
|
},
|
||
|
|
"Logging": {
|
||
|
|
"LogLevel": {
|
||
|
|
"Default": "Information",
|
||
|
|
"WCTDataMiner": "Debug"
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"Paths": {
|
||
|
|
"LogDir": "./data/logs",
|
||
|
|
"OutputDir": "./data/output"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 2. 配置类定义
|
||
|
|
|
||
|
|
```csharp
|
||
|
|
// Configuration/AppSettings.cs
|
||
|
|
namespace WCTDataMiner.Configuration;
|
||
|
|
|
||
|
|
public class AppSettings
|
||
|
|
{
|
||
|
|
public DatabaseSettings Database { get; set; } = new();
|
||
|
|
public ParserSettings Parser { get; set; } = new();
|
||
|
|
public PathSettings Paths { get; set; } = new();
|
||
|
|
}
|
||
|
|
|
||
|
|
public class DatabaseSettings
|
||
|
|
{
|
||
|
|
public string Type { get; set; } = "sqlite";
|
||
|
|
public string Path { get; set; } = "./data/database/wctminer.db";
|
||
|
|
|
||
|
|
// PostgreSQL 配置(可选)
|
||
|
|
public string? Host { get; set; }
|
||
|
|
public int Port { get; set; } = 5432;
|
||
|
|
public string? Name { get; set; }
|
||
|
|
public string? User { get; set; }
|
||
|
|
public string? Password { get; set; }
|
||
|
|
}
|
||
|
|
|
||
|
|
public class ParserSettings
|
||
|
|
{
|
||
|
|
public int BatchSize { get; set; } = 1000;
|
||
|
|
}
|
||
|
|
|
||
|
|
public class PathSettings
|
||
|
|
{
|
||
|
|
public string LogDir { get; set; } = "./data/logs";
|
||
|
|
public string OutputDir { get; set; } = "./data/output";
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 3. 配置加载
|
||
|
|
|
||
|
|
```csharp
|
||
|
|
// Program.cs 配置加载部分
|
||
|
|
using Microsoft.Extensions.Configuration;
|
||
|
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
|
using WCTDataMiner.Configuration;
|
||
|
|
|
||
|
|
var builder = Host.CreateDefaultBuilder(args);
|
||
|
|
|
||
|
|
builder.ConfigureAppConfiguration(config =>
|
||
|
|
{
|
||
|
|
config.AddJsonFile("appsettings.json", optional: false);
|
||
|
|
config.AddJsonFile($"appsettings.{environment}.json", optional: true);
|
||
|
|
config.AddEnvironmentVariables();
|
||
|
|
config.AddCommandLine(args);
|
||
|
|
});
|
||
|
|
|
||
|
|
builder.ConfigureServices((context, services) =>
|
||
|
|
{
|
||
|
|
services.Configure<AppSettings>(context.Configuration.GetSection(""));
|
||
|
|
|
||
|
|
// 注册数据库服务
|
||
|
|
services.AddDatabaseServices(context.Configuration);
|
||
|
|
|
||
|
|
// 注册其他服务
|
||
|
|
services.AddScoped<QfodParser>();
|
||
|
|
services.AddScoped<PlossParser>();
|
||
|
|
services.AddScoped<ParseService>();
|
||
|
|
services.AddScoped<ErrorHandlingService>();
|
||
|
|
});
|
||
|
|
```
|