Apq.Cfg.WebApi
1.2.1
dotnet add package Apq.Cfg.WebApi --version 1.2.1
NuGet\Install-Package Apq.Cfg.WebApi -Version 1.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Apq.Cfg.WebApi" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Apq.Cfg.WebApi" Version="1.2.1" />
<PackageReference Include="Apq.Cfg.WebApi" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Apq.Cfg.WebApi --version 1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Apq.Cfg.WebApi, 1.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Apq.Cfg.WebApi@1.2.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Apq.Cfg.WebApi&version=1.2.1
#tool nuget:?package=Apq.Cfg.WebApi&version=1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Apq.Cfg.WebApi
为 Apq.Cfg 提供 RESTful API 接口,支持远程配置管理,内置 OpenAPI 文档。
📖 在线文档:https://apq-cfg.vercel.app/
安装
dotnet add package Apq.Cfg.WebApi
API 文档 UI
根据目标框架自动选择最适合的 API 文档 UI:
| 目标框架 | UI 库 | 访问路径 |
|---|---|---|
| .NET 8 | Swagger UI | /swagger |
| .NET 10+ | Scalar | /scalar/v1 |
快速开始
方式一:服务集合扩展
using Apq.Cfg;
using Apq.Cfg.WebApi;
var builder = WebApplication.CreateBuilder(args);
// 构建配置
var cfg = new CfgBuilder()
.AddJsonFile("config.json")
.AddJsonFile("config.local.json", level: 5, writeable: true, isPrimaryWriter: true)
.Build();
// 添加服务
builder.Services.AddApqCfgWebApi(cfg, options =>
{
options.Authentication = AuthenticationType.ApiKey;
options.ApiKey = "your-secret-key";
});
var app = builder.Build();
app.UseApqCfgWebApi();
app.MapApqCfgWebApi();
app.Run();
方式二:链式调用
using Apq.Cfg;
using Apq.Cfg.WebApi;
var builder = WebApplication.CreateBuilder(args);
// 构建配置并添加 WebApi(链式调用)
var cfg = new CfgBuilder()
.AddJsonFile("config.json")
.AddJsonFile("config.local.json", level: 5, writeable: true, isPrimaryWriter: true)
.Build()
.AddWebApi(builder.Services, options =>
{
options.Authentication = AuthenticationType.ApiKey;
options.ApiKey = "your-secret-key";
});
var app = builder.Build();
app.UseApqCfgWebApi();
app.MapApqCfgWebApi();
app.Run();
启动后访问 API 文档 UI 即可查看接口文档。
配置方式
WebApi 选项可以通过两种方式配置:
1. 从 ICfgRoot 读取(推荐)
在配置文件中添加 ApqCfg:WebApi 节:
{
"ApqCfg": {
"WebApi": {
"Authentication": "ApiKey",
"ApiKey": "your-secret-key",
"AllowWrite": true,
"EnableCors": true,
"CorsOrigins": ["*"]
}
}
}
然后直接调用:
builder.Services.AddApqCfgWebApi(cfg);
2. 代码配置(覆盖配置文件)
builder.Services.AddApqCfgWebApi(cfg, options =>
{
options.Authentication = AuthenticationType.ApiKey;
options.ApiKey = "your-secret-key";
});
配置优先级(从低到高):
- 默认值
- 从
ICfgRoot的ApqCfg:WebApi节读取的配置 configure回调中的配置
OpenAPI 文档配置
API 文档默认启用,可通过配置调整:
builder.Services.AddApqCfgWebApi(cfg, options =>
{
options.OpenApiEnabled = true; // 是否启用 API 文档(默认 true)
options.OpenApiTitle = "My Config API"; // 文档标题
options.OpenApiDescription = "配置管理 API"; // 文档描述
options.OpenApiVersion = "v1"; // API 版本
options.OpenApiRoutePrefix = "swagger"; // UI 路由前缀
options.OpenApiShowAuthorizationButton = true; // 显示认证按钮
});
认证方式
// API Key 认证
options.Authentication = AuthenticationType.ApiKey;
options.ApiKey = "your-secret-key";
options.ApiKeyHeaderName = "X-Api-Key"; // 默认
// JWT Bearer 认证
options.Authentication = AuthenticationType.JwtBearer;
options.Jwt = new JwtOptions
{
Authority = "https://your-auth-server",
Audience = "your-api"
};
// 无认证(仅开发环境)
options.Authentication = AuthenticationType.None;
API 端点
合并配置(Merged)
| 方法 | 路径 | 说明 |
|---|---|---|
| GET | /api/apqcfg/merged |
获取合并后的所有配置 |
| GET | /api/apqcfg/merged/tree |
获取合并后的配置树 |
| GET | /api/apqcfg/merged/keys/{key} |
获取合并配置的单个值 |
| GET | /api/apqcfg/merged/sections/{section} |
获取合并配置的配置节 |
| PUT | /api/apqcfg/merged/keys/{key} |
设置合并配置值 |
| PUT | /api/apqcfg/merged/batch |
批量更新合并配置 |
| DELETE | /api/apqcfg/merged/keys/{key} |
删除合并配置值 |
| GET | /api/apqcfg/merged/export/{format} |
导出合并配置(json/env/kv) |
配置源(Sources)
| 方法 | 路径 | 说明 |
|---|---|---|
| GET | /api/apqcfg/sources |
获取所有配置源信息 |
| GET | /api/apqcfg/sources/{level}/{name} |
获取指定配置源内容 |
| GET | /api/apqcfg/sources/{level}/{name}/tree |
获取指定配置源的配置树 |
| GET | /api/apqcfg/sources/{level}/{name}/keys/{key} |
获取指定配置源的单个配置值 |
| PUT | /api/apqcfg/sources/{level}/{name}/keys/{key} |
设置指定配置源的配置值 |
| DELETE | /api/apqcfg/sources/{level}/{name}/keys/{key} |
删除指定配置源的配置值 |
| GET | /api/apqcfg/sources/{level}/{name}/export/{format} |
导出指定配置源(json/env/kv) |
管理操作
| 方法 | 路径 | 说明 |
|---|---|---|
| POST | /api/apqcfg/save |
保存所有可写配置源 |
| POST | /api/apqcfg/reload |
重新加载所有配置源 |
配置选项
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Enabled |
bool | true | 是否启用 API |
RoutePrefix |
string | /api/apqcfg |
API 路由前缀 |
Authentication |
enum | None | 认证类型 |
EnableCors |
bool | true | 是否启用 CORS(允许任意来源) |
CorsOrigins |
string[] | ["*"] |
CORS 允许的来源 |
OpenApiEnabled |
bool | true | 是否启用 API 文档 |
OpenApiTitle |
string | Apq.Cfg Web API |
API 文档标题 |
OpenApiRoutePrefix |
string | swagger / scalar/v1 |
API 文档 UI 路由前缀 |
许可证
MIT License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- Apq.Cfg (>= 1.2.1)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.2)
- Microsoft.AspNetCore.OpenApi (>= 10.0.2)
- Scalar.AspNetCore (>= 2.12.11)
-
net8.0
- Apq.Cfg (>= 1.2.1)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.23)
- Swashbuckle.AspNetCore (>= 10.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.