settings

package
v0.7.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package settings 管理 config.toml 的 schema 与优先级解析。

Index

Constants

View Source
const (
	DefaultReverseSearchSource         = "builtin"
	DefaultReverseSearchCacheTTL       = "720h"
	DefaultReverseSearchRetries        = 3
	DefaultReverseSearchRetryWait      = "30s"
	DefaultReverseSearchRequestTimeout = "60s"
)

reverse_search 区域默认值;超时/重试沿用参考服务行为且可配置。

View Source
const (
	HostAuto   = "auto"
	HostMirror = "mirror"
	HostMain   = "main"
)

Host names accepted by --host / config.

Variables

View Source
var HostURLs = map[string]string{
	HostMirror: "https://jdforrepam.com",
	HostMain:   "https://javdb.com",
}

HostURLs maps logical host names to base URLs.

Functions

func ExpandEnvValue added in v0.7.0

func ExpandEnvValue(value string, getenv func(string) string) (string, error)

ExpandEnvValue 展开 header 值中的 ${ENV:NAME} 引用。缺失或空的环境变量 报错只包含变量名,绝不包含展开后的值。

func NormalizeHost

func NormalizeHost(host string) (string, error)

NormalizeHost 校验并规范逻辑 host 或绝对 HTTP(S) URL。绝对 URL 必须不含 query 或 fragment,否则 transport 以字符串拼接追加 API path 时会把 endpoint 拼进 query 或根本不 发给服务器。

func ResolveProxy

func ResolveProxy(file Settings, flagProxy string) (string, error)

ResolveProxy 按 flag > env > file 的优先级解析代理,供无需 JavDB host 的独立流程复用。

func SaveFile

func SaveFile(path string, s Settings) error

SaveFile writes settings sparsely to path (0600). 保存采用结构化合并:先读 既有配置树,只覆盖已知顶层键,未修改的表、source 数组与未知键保持原样。

func ValidateHost

func ValidateHost(host string) error

ValidateHost returns error if host is not a known name or absolute URL.

Types

type Document added in v0.7.0

type Document struct {
	// contains filtered or unexported fields
}

Document 是 config.toml 的结构化树视图:保存时保留未修改的表、source 数组 与未知键的语义值,不再整体重编码 typed struct。

func LoadDocument added in v0.7.0

func LoadDocument(path string) (*Document, error)

LoadDocument 读取配置树;文件缺失返回空文档。

func (*Document) Delete added in v0.7.0

func (d *Document) Delete(key string) error

Delete 通过点路径删除键;键不存在是 no-op。

func (*Document) Save added in v0.7.0

func (d *Document) Save(path string) error

Save 把整棵树以原子方式写回 path(0600)。

func (*Document) Set added in v0.7.0

func (d *Document) Set(key string, value any) error

Set 通过点路径写入标量值,必要时创建中间表。

type ResolvedReverseSearch added in v0.7.0

type ResolvedReverseSearch struct {
	DefaultSource  string
	Cache          bool
	CacheTTL       time.Duration
	Retries        int
	RetryWait      time.Duration
	RequestTimeout time.Duration
	Sources        []ReverseSearchSource
}

ResolvedReverseSearch 是校验并展开后的反搜配置;Headers 已完成环境展开。

func ResolveReverseSearch added in v0.7.0

func ResolveReverseSearch(s Settings, getenv func(string) string) (ResolvedReverseSearch, error)

ResolveReverseSearch 校验 reverse_search 区域并展开 header 环境引用。

type ReverseSearchSettings added in v0.7.0

type ReverseSearchSettings struct {
	DefaultSource  string                `toml:"default_source"`
	Cache          *bool                 `toml:"cache"`
	CacheTTL       string                `toml:"cache_ttl"`
	Retries        int                   `toml:"retries"`
	RetryWait      string                `toml:"retry_wait"`
	RequestTimeout string                `toml:"request_timeout"`
	Sources        []ReverseSearchSource `toml:"sources"`
}

ReverseSearchSettings 是 [reverse_search] 表的 typed 视图。

func (ReverseSearchSettings) CacheEnabled added in v0.7.0

func (r ReverseSearchSettings) CacheEnabled() bool

CacheEnabled 报告反搜文件缓存是否启用(默认 true)。

type ReverseSearchSource added in v0.7.0

type ReverseSearchSource struct {
	Name    string            `toml:"name"`
	URL     string            `toml:"url"`
	Headers map[string]string `toml:"headers"`
}

ReverseSearchSource 是 config.toml 中 [[reverse_search.sources]] 的一项。 Headers 的值只允许静态文本与 ${ENV:NAME} 引用,禁止明文 secret。

type Runtime

type Runtime struct {
	Host        string // auto | mirror | main | URL
	BaseURL     string
	Proxy       string
	AutoRelogin bool
	Lang        string
	DeviceUUID  string
}

Runtime is the resolved config after flag > env > file > default.

func Resolve

func Resolve(file Settings, flagHost, flagProxy string, flagAutoRelogin *bool) (Runtime, error)

Resolve merges flag/env/file/defaults. flagHost / flagProxy empty means "not set". flagAutoRelogin: nil = not set; non-nil overrides.

type Settings

type Settings struct {
	Host          string                `toml:"host"`
	HTTPSProxy    string                `toml:"https_proxy,omitempty"`
	AutoRelogin   bool                  `toml:"auto_relogin"`
	Lang          string                `toml:"lang,omitempty"`
	DeviceUUID    string                `toml:"device_uuid,omitempty"` // optional override; else file/device_uuid
	ReverseSearch ReverseSearchSettings `toml:"reverse_search"`
}

Settings is the on-disk config.toml schema.

func Defaults

func Defaults() Settings

Defaults returns baseline settings.

func LoadFile

func LoadFile(path string) (Settings, error)

LoadFile reads config.toml; missing file returns Defaults().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL