Documentation
¶
Overview ¶
Package config 定义 core 的配置文件模型、加载与校验。
配置只描述事实,不承载行为:它不知道渠道客户端与插件宿主的存在,装配由 server 层完成。凭据一律只写环境变量名,配置文件里不出现密钥。
Index ¶
Constants ¶
const ( DefaultListen = "127.0.0.1:8787" DefaultLogFormat = "json" DefaultLogLevel = "info" DefaultTimeout = 120 * time.Second DefaultPluginDir = "plugins" DefaultMaxBodyBytes = 32 << 20 // MaxRetries 是渠道重试次数的上限,与 channel.MaxRetries 保持一致。 MaxRetries = 10 )
默认值。只在这里写一次,Load 与文档都以它为准。
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Auth ¶
type Auth struct {
// Style 取 bearer、x-api-key、query、custom 或 none。
Style string `yaml:"style"`
// KeyEnv 是凭据所在的环境变量名。
KeyEnv string `yaml:"key_env"`
// Header 是 Style 为 custom 时的请求头名。
Header string `yaml:"header"`
// Query 是 Style 为 query 时的查询参数名。
Query string `yaml:"query"`
}
Auth 是渠道的鉴权方式。凭据值只从 KeyEnv 指定的环境变量读取。
type Channel ¶
type Channel struct {
Name string `yaml:"name"`
Dialect string `yaml:"dialect"`
BaseURL string `yaml:"base_url"`
Path string `yaml:"path"`
Auth Auth `yaml:"auth"`
Timeout Duration `yaml:"timeout"`
MaxRetries *int `yaml:"max_retries"`
Headers map[string]string `yaml:"headers"`
}
Channel 是一个上游渠道。BaseURL 是渠道基址,Path 为空时按方言取默认路径。
type Config ¶
type Config struct {
// Path 是配置文件的绝对路径,由 Load 填入。Parse 不填。
Path string `yaml:"-"`
Listen string `yaml:"listen"`
Log Log `yaml:"log"`
Channels []Channel `yaml:"channels"`
Models []Model `yaml:"models"`
Plugins Plugins `yaml:"plugins"`
}
Config 是一份完整的服务配置。
func Load ¶
Load 读取并校验配置文件。
解码开启 KnownFields:拼错的字段在加载时报错,而不是被静默忽略。相对路径一律 以配置文件所在目录为基准,因此工作目录变化不会改变行为。
func (*Config) ChannelByName ¶
ChannelByName 按名字取渠道配置。
func (*Config) EnabledPlugins ¶
EnabledPlugins 返回启用中的插件,按 order 升序排列;order 相同时保持配置顺序。
func (*Config) PluginConfig ¶
PluginConfig 返回指定插件的配置对象,未配置时返回空 map。
type Duration ¶
Duration 是配置里可读的时长,写法与 time.ParseDuration 一致,例如 120s、1m30s。 为兼容只写数字的配置,纯数字按秒解释。
func (Duration) MarshalYAML ¶
MarshalYAML 按 time.Duration 的字符串形式写出。
func (*Duration) UnmarshalJSON ¶
UnmarshalJSON 让 Duration 在 JSON 里也能工作,便于 CLI 输出配置快照。
type Matcher ¶
type Matcher struct {
// contains filtered or unexported fields
}
Matcher 是一个已校验的模型名匹配器。
func CompileMatch ¶
CompileMatch 校验匹配式并返回匹配器。
支持两个通配符:* 匹配任意长度的一段(含 / 与 :),? 匹配一个字符。 其余字符按字面比较。模型名按 UTF-8 字符逐个比较。
type Model ¶
type Model struct {
Match string `yaml:"match"`
Channel string `yaml:"channel"`
Upstream string `yaml:"upstream"`
Dialect string `yaml:"dialect"`
}
Model 是一条模型路由规则。Match 支持 * 通配,同一个模型可以有多条规则, 按配置顺序依次尝试。
type Plugin ¶
type Plugin struct {
Key string `yaml:"key"`
Enabled bool `yaml:"enabled"`
Order int `yaml:"order"`
Config map[string]any `yaml:"config"`
}
Plugin 是一个插件的启用状态与顺序。