mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 04:10:51 +08:00
refactor(access): migrate config-api-key provider to internal package
- Moved `config-api-key` provider logic from SDK to the internal `config_access` package. - Updated provider registration and initialization to ensure proper management via `Register` function. - Removed redundant `config-api-key` documentation, simplifying configuration examples. - Adjusted related imports and reconciliations for seamless integration with the new structure.
This commit is contained in:
14
README.md
14
README.md
@@ -282,11 +282,6 @@ The server uses a YAML configuration file (`config.yaml`) located in the project
|
|||||||
| `debug` | boolean | false | Enable debug mode for verbose logging. |
|
| `debug` | boolean | false | Enable debug mode for verbose logging. |
|
||||||
| `logging-to-file` | boolean | true | Write application logs to rotating files instead of stdout. Set to `false` to log to stdout/stderr. |
|
| `logging-to-file` | boolean | true | Write application logs to rotating files instead of stdout. Set to `false` to log to stdout/stderr. |
|
||||||
| `usage-statistics-enabled` | boolean | true | Enable in-memory usage aggregation for management APIs. Disable to drop all collected usage metrics. |
|
| `usage-statistics-enabled` | boolean | true | Enable in-memory usage aggregation for management APIs. Disable to drop all collected usage metrics. |
|
||||||
| `auth` | object | {} | Request authentication configuration. |
|
|
||||||
| `auth.providers` | object[] | [] | Authentication providers. Includes built-in `config-api-key` for inline keys. |
|
|
||||||
| `auth.providers.*.name` | string | "" | Provider instance name. |
|
|
||||||
| `auth.providers.*.type` | string | "" | Provider implementation identifier (for example `config-api-key`). |
|
|
||||||
| `auth.providers.*.api-keys` | string[] | [] | Inline API keys consumed by the `config-api-key` provider. |
|
|
||||||
| `api-keys` | string[] | [] | Legacy shorthand for inline API keys. Values are mirrored into the `config-api-key` provider for backwards compatibility. |
|
| `api-keys` | string[] | [] | Legacy shorthand for inline API keys. Values are mirrored into the `config-api-key` provider for backwards compatibility. |
|
||||||
| `generative-language-api-key` | string[] | [] | List of Generative Language API keys. |
|
| `generative-language-api-key` | string[] | [] | List of Generative Language API keys. |
|
||||||
| `codex-api-key` | object | {} | List of Codex API keys. |
|
| `codex-api-key` | object | {} | List of Codex API keys. |
|
||||||
@@ -354,15 +349,6 @@ gemini-web:
|
|||||||
code-mode: false # Enable code mode
|
code-mode: false # Enable code mode
|
||||||
max-chars-per-request: 1000000 # Max characters per request
|
max-chars-per-request: 1000000 # Max characters per request
|
||||||
|
|
||||||
# Request authentication providers
|
|
||||||
auth:
|
|
||||||
providers:
|
|
||||||
- name: "default"
|
|
||||||
type: "config-api-key"
|
|
||||||
api-keys:
|
|
||||||
- "your-api-key-1"
|
|
||||||
- "your-api-key-2"
|
|
||||||
|
|
||||||
# API keys for official Generative Language API
|
# API keys for official Generative Language API
|
||||||
generative-language-api-key:
|
generative-language-api-key:
|
||||||
- "AIzaSy...01"
|
- "AIzaSy...01"
|
||||||
|
|||||||
14
README_CN.md
14
README_CN.md
@@ -294,11 +294,6 @@ console.log(await claudeResponse.json());
|
|||||||
| `debug` | boolean | false | 启用调试模式以获取详细日志。 |
|
| `debug` | boolean | false | 启用调试模式以获取详细日志。 |
|
||||||
| `logging-to-file` | boolean | true | 是否将应用日志写入滚动文件;设为 false 时输出到 stdout/stderr。 |
|
| `logging-to-file` | boolean | true | 是否将应用日志写入滚动文件;设为 false 时输出到 stdout/stderr。 |
|
||||||
| `usage-statistics-enabled` | boolean | true | 是否启用内存中的使用统计;设为 false 时直接丢弃所有统计数据。 |
|
| `usage-statistics-enabled` | boolean | true | 是否启用内存中的使用统计;设为 false 时直接丢弃所有统计数据。 |
|
||||||
| `auth` | object | {} | 请求鉴权配置。 |
|
|
||||||
| `auth.providers` | object[] | [] | 鉴权提供方列表,内置 `config-api-key` 支持内联密钥。 |
|
|
||||||
| `auth.providers.*.name` | string | "" | 提供方实例名称。 |
|
|
||||||
| `auth.providers.*.type` | string | "" | 提供方实现标识(例如 `config-api-key`)。 |
|
|
||||||
| `auth.providers.*.api-keys` | string[] | [] | `config-api-key` 提供方使用的内联密钥。 |
|
|
||||||
| `api-keys` | string[] | [] | 兼容旧配置的简写,会自动同步到默认 `config-api-key` 提供方。 |
|
| `api-keys` | string[] | [] | 兼容旧配置的简写,会自动同步到默认 `config-api-key` 提供方。 |
|
||||||
| `generative-language-api-key` | string[] | [] | 生成式语言API密钥列表。 |
|
| `generative-language-api-key` | string[] | [] | 生成式语言API密钥列表。 |
|
||||||
| `codex-api-key` | object | {} | Codex API密钥列表。 |
|
| `codex-api-key` | object | {} | Codex API密钥列表。 |
|
||||||
@@ -366,15 +361,6 @@ gemini-web:
|
|||||||
code-mode: false # 启用代码模式
|
code-mode: false # 启用代码模式
|
||||||
max-chars-per-request: 1000000 # 单次请求最大字符数
|
max-chars-per-request: 1000000 # 单次请求最大字符数
|
||||||
|
|
||||||
# 请求鉴权提供方
|
|
||||||
auth:
|
|
||||||
providers:
|
|
||||||
- name: "default"
|
|
||||||
type: "config-api-key"
|
|
||||||
api-keys:
|
|
||||||
- "your-api-key-1"
|
|
||||||
- "your-api-key-2"
|
|
||||||
|
|
||||||
# AIStduio Gemini API 的 API 密钥
|
# AIStduio Gemini API 的 API 密钥
|
||||||
generative-language-api-key:
|
generative-language-api-key:
|
||||||
- "AIzaSy...01"
|
- "AIzaSy...01"
|
||||||
|
|||||||
@@ -35,15 +35,6 @@ quota-exceeded:
|
|||||||
switch-project: true # Whether to automatically switch to another project when a quota is exceeded
|
switch-project: true # Whether to automatically switch to another project when a quota is exceeded
|
||||||
switch-preview-model: true # Whether to automatically switch to a preview model when a quota is exceeded
|
switch-preview-model: true # Whether to automatically switch to a preview model when a quota is exceeded
|
||||||
|
|
||||||
# Request authentication providers
|
|
||||||
auth:
|
|
||||||
providers:
|
|
||||||
- name: "default"
|
|
||||||
type: "config-api-key"
|
|
||||||
api-keys:
|
|
||||||
- "your-api-key-1"
|
|
||||||
- "your-api-key-2"
|
|
||||||
|
|
||||||
# API keys for official Generative Language API
|
# API keys for official Generative Language API
|
||||||
generative-language-api-key:
|
generative-language-api-key:
|
||||||
- "AIzaSy...01"
|
- "AIzaSy...01"
|
||||||
|
|||||||
@@ -1,27 +1,33 @@
|
|||||||
package configapikey
|
package configaccess
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access"
|
sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
|
sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var registerOnce sync.Once
|
||||||
|
|
||||||
|
// Register ensures the config-access provider is available to the access manager.
|
||||||
|
func Register() {
|
||||||
|
registerOnce.Do(func() {
|
||||||
|
sdkaccess.RegisterProvider(sdkconfig.AccessProviderTypeConfigAPIKey, newProvider)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type provider struct {
|
type provider struct {
|
||||||
name string
|
name string
|
||||||
keys map[string]struct{}
|
keys map[string]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func newProvider(cfg *sdkconfig.AccessProvider, _ *sdkconfig.SDKConfig) (sdkaccess.Provider, error) {
|
||||||
sdkaccess.RegisterProvider(config.AccessProviderTypeConfigAPIKey, newProvider)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newProvider(cfg *config.AccessProvider, _ *config.SDKConfig) (sdkaccess.Provider, error) {
|
|
||||||
name := cfg.Name
|
name := cfg.Name
|
||||||
if name == "" {
|
if name == "" {
|
||||||
name = config.DefaultAccessProviderName
|
name = sdkconfig.DefaultAccessProviderName
|
||||||
}
|
}
|
||||||
keys := make(map[string]struct{}, len(cfg.APIKeys))
|
keys := make(map[string]struct{}, len(cfg.APIKeys))
|
||||||
for _, key := range cfg.APIKeys {
|
for _, key := range cfg.APIKeys {
|
||||||
@@ -35,7 +41,7 @@ func newProvider(cfg *config.AccessProvider, _ *config.SDKConfig) (sdkaccess.Pro
|
|||||||
|
|
||||||
func (p *provider) Identifier() string {
|
func (p *provider) Identifier() string {
|
||||||
if p == nil || p.name == "" {
|
if p == nil || p.name == "" {
|
||||||
return config.DefaultAccessProviderName
|
return sdkconfig.DefaultAccessProviderName
|
||||||
}
|
}
|
||||||
return p.name
|
return p.name
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
configaccess "github.com/router-for-me/CLIProxyAPI/v6/internal/access/config_access"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||||
sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access"
|
sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access"
|
||||||
sdkConfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
|
sdkConfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
|
||||||
@@ -21,6 +22,8 @@ func ReconcileProviders(oldCfg, newCfg *config.Config, existing []sdkaccess.Prov
|
|||||||
return nil, nil, nil, nil, nil
|
return nil, nil, nil, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configaccess.Register()
|
||||||
|
|
||||||
existingMap := make(map[string]sdkaccess.Provider, len(existing))
|
existingMap := make(map[string]sdkaccess.Provider, len(existing))
|
||||||
for _, provider := range existing {
|
for _, provider := range existing {
|
||||||
if provider == nil {
|
if provider == nil {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package cliproxy
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
configaccess "github.com/router-for-me/CLIProxyAPI/v6/internal/access/config_access"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/api"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/api"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||||
sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access"
|
sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access"
|
||||||
@@ -184,6 +185,8 @@ func (b *Builder) Build() (*Service, error) {
|
|||||||
if accessManager == nil {
|
if accessManager == nil {
|
||||||
accessManager = sdkaccess.NewManager()
|
accessManager = sdkaccess.NewManager()
|
||||||
}
|
}
|
||||||
|
configaccess.Register()
|
||||||
|
|
||||||
providers, err := sdkaccess.BuildProviders(&b.cfg.SDKConfig)
|
providers, err := sdkaccess.BuildProviders(&b.cfg.SDKConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ import (
|
|||||||
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
|
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/watcher"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/watcher"
|
||||||
sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access"
|
sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access"
|
||||||
_ "github.com/router-for-me/CLIProxyAPI/v6/sdk/access/providers/configapikey"
|
|
||||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
|
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
|
||||||
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
|
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/usage"
|
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/usage"
|
||||||
|
|||||||
Reference in New Issue
Block a user