Documentation
¶
Index ¶
- Constants
- Variables
- func GetLogger() *zap.Logger
- func InitRuntime() (*zap.Logger, error)
- func NewTempDBDir(dir string) (*models.TorrentDB, error)
- func SetV2Broadcaster(b V2Broadcaster)
- func ValidateAttendanceWindow(start, end string) error
- func ValidateDailyReportTime(v string) error
- type CloakConfigSnapshot
- type ConfigStore
- func (s *ConfigStore) AdminCount() (int64, error)
- func (s *ConfigStore) AppendRSSToSite(siteName string, entry models.RSSConfig) (models.RSSConfig, error)
- func (s *ConfigStore) AttendanceWindow() (string, string, error)
- func (s *ConfigStore) DailyReportOffset(randN func(n int64) int64) (time.Duration, error)
- func (s *ConfigStore) DailyReportSettings() (DailyReportSettings, error)
- func (s *ConfigStore) DecryptCookie(cipherText string) (string, error)
- func (s *ConfigStore) DeleteRSSFromSite(siteName string, rssID uint) (models.RSSConfig, error)
- func (s *ConfigStore) DeleteSite(name string) error
- func (s *ConfigStore) EnabledSiteNames() (map[string]bool, error)
- func (s *ConfigStore) EncryptCookie(plain string) (string, error)
- func (s *ConfigStore) EnsureAdmin(username, passwordHash string) error
- func (s *ConfigStore) GetAdmin(username string) (*models.AdminUser, error)
- func (s *ConfigStore) GetCloakConfig() (CloakConfigSnapshot, error)
- func (s *ConfigStore) GetCloakEndpoint() (string, error)
- func (s *ConfigStore) GetCloakToken() (string, error)
- func (s *ConfigStore) GetGlobalOnly() (models.SettingsGlobal, error)
- func (s *ConfigStore) GetGlobalSettings() (models.SettingsGlobal, error)
- func (s *ConfigStore) GetQbitOnly() (models.QbitSettings, error)
- func (s *ConfigStore) GetQbitSettings() (models.QbitSettings, error)
- func (s *ConfigStore) GetSiteConf(name models.SiteGroup) (models.SiteConfig, error)
- func (s *ConfigStore) ListRSSForSite(siteName string) ([]models.RSSConfig, error)
- func (s *ConfigStore) ListSites() (map[models.SiteGroup]models.SiteConfig, error)
- func (s *ConfigStore) Load() (*models.Config, error)
- func (s *ConfigStore) ReplaceSiteRSS(siteID uint, rss []models.RSSConfig) error
- func (s *ConfigStore) SaveAttendanceWindow(start, end string) error
- func (s *ConfigStore) SaveCloakConfig(endpoint, plaintextToken string, clearToken bool, profileID *string) error
- func (s *ConfigStore) SaveDailyReportSettings(in DailyReportSettings) error
- func (s *ConfigStore) SaveGlobal(gl models.SettingsGlobal) error
- func (s *ConfigStore) SaveGlobalSettings(gs models.SettingsGlobal) error
- func (s *ConfigStore) SaveGlobalSettingsWithPatch(gs models.SettingsGlobal, patch *GlobalSettingsPatch) error
- func (s *ConfigStore) SaveQbit(qb models.QbitSettings) error
- func (s *ConfigStore) SaveQbitSettings(q models.QbitSettings) error
- func (s *ConfigStore) SetCloakEndpoint(endpoint string) error
- func (s *ConfigStore) SetCloakToken(plaintext string) error
- func (s *ConfigStore) SetSiteAttendanceEnabled(siteName string, enabled bool) error
- func (s *ConfigStore) SetSiteEnabled(site models.SiteGroup, enabled bool) error
- func (s *ConfigStore) SiteCookiePlaintext(site models.SiteSetting) (string, error)
- func (s *ConfigStore) SyncSites(registeredSites []models.RegisteredSite) error
- func (s *ConfigStore) UpdateAdmin(u *models.AdminUser) error
- func (s *ConfigStore) UpdateAdminPassword(username, newHash string) error
- func (s *ConfigStore) UpsertSite(site models.SiteGroup, sc models.SiteConfig) (uint, error)
- func (s *ConfigStore) UpsertSiteWithRSS(site models.SiteGroup, sc models.SiteConfig) error
- type DailyReportSettings
- type GlobalSettingsPatch
- type V2BroadcastResult
- type V2Broadcaster
- type V2BroadcasterFunc
Constants ¶
const ( DefaultAttendanceWindowStart = "08:00" DefaultAttendanceWindowEnd = "10:00" )
每日签到时间窗的默认值(进程时区)。
const DefaultDailyReportTime = "22:00"
DefaultDailyReportTime 是战报默认的发送时刻。
const V2BroadcastFreshnessWindow = 5 * time.Minute
V2BroadcastFreshnessWindow guards against re-broadcasting on stale rows. If MigrationState.CompletedAt is older than this window, the broadcast is suppressed even when BroadcastSent=false (e.g. the row was migrated from a previous v2 release line that did not yet implement the sentinel flag).
const V2BroadcastSchemaVersion = 10
V2BroadcastSchemaVersion is the schema version that triggers the v2 upgrade broadcast. Centralized here so tests reference the same constant.
Variables ¶
var ( // ErrSecretKeyUnusable 表示 secret.key 存在却无法使用(读不出或格式不对)。 ErrSecretKeyUnusable = errors.New("secret.key 无法使用") // ErrSecretKeyRegenerated 表示 secret.key 是本次新生成的,但数据库里已有用原密钥加密的数据。 ErrSecretKeyRegenerated = errors.New("secret.key 是新生成的,数据库里却已有加密数据") )
var ErrDailyReportNoChannel = errors.New("开启每日战报至少要选一个通知通道")
ErrDailyReportNoChannel 表示开启了战报却没有选通道。
var ErrKeyMissing = errors.New("AES secret key missing or corrupt")
var ErrNoBroadcaster = errors.New("v2 broadcaster not configured")
ErrNoBroadcaster is returned by tests that intentionally exercise the "broadcaster missing" branch.
Functions ¶
func InitRuntime ¶ added in v0.1.0
取消 viper 文件读取,统一改为 DB 初始化与迁移(保留 once 初始化) InitViper 改造为:初始化日志、数据库,然后优先从 DB 加载全局配置;若 DB 为空且提供了文件,则允许后续迁移命令将文件写入 DB
func NewTempDBDir ¶ added in v0.1.0
NewTempDBDir creates a temporary sqlite db under the given directory and migrates schema
func SetV2Broadcaster ¶ added in v0.35.0
func SetV2Broadcaster(b V2Broadcaster)
SetV2Broadcaster registers the broadcaster used by InitRuntime. Production wiring (e.g. cmd/web.go) installs a concrete implementation after the notify.Router is constructed; if no broadcaster is registered, InitRuntime skips the dispatch step silently.
func ValidateAttendanceWindow ¶
ValidateAttendanceWindow 校验签到时间窗:HH:MM,开始早于结束,不跨午夜。
func ValidateDailyReportTime ¶
ValidateDailyReportTime 校验 HH:MM。
Types ¶
type CloakConfigSnapshot ¶ added in v0.35.0
CloakConfigSnapshot is the in-memory view of CloakBrowser-Manager接入配置。 仅在 ConfigStore 内部及调用方使用;HTTP 层通过 web.api_cloak.go 暴露的响应类型 控制对外字段,token 永远不会以明文出现在 API 响应里。
type ConfigStore ¶ added in v0.1.0
type ConfigStore struct {
// contains filtered or unexported fields
}
func NewConfigStore ¶ added in v0.1.0
func NewConfigStore(db *models.TorrentDB) *ConfigStore
func (*ConfigStore) AdminCount ¶ added in v0.1.0
func (s *ConfigStore) AdminCount() (int64, error)
func (*ConfigStore) AppendRSSToSite ¶ added in v0.35.0
func (*ConfigStore) AttendanceWindow ¶
func (s *ConfigStore) AttendanceWindow() (string, string, error)
AttendanceWindow 返回签到时间窗;还没有全局设置或存的值不合法时返回默认的 08:00–10:00。
func (*ConfigStore) DailyReportOffset ¶
DailyReportOffset 返回本安装固定的发送偏移(1–600 秒)。还没分配时用 randN(返回 [0, n))分配一个并保存; 全局设置行还不存在时返回 0,不分配。
func (*ConfigStore) DailyReportSettings ¶
func (s *ConfigStore) DailyReportSettings() (DailyReportSettings, error)
DailyReportSettings 读出战报配置;全局设置行还不存在时返回默认值(关闭、22:00、无通道)。
func (*ConfigStore) DecryptCookie ¶ added in v0.35.0
func (s *ConfigStore) DecryptCookie(cipherText string) (string, error)
func (*ConfigStore) DeleteRSSFromSite ¶ added in v0.35.0
DeleteRSSFromSite 原子删除指定站点下的单条 RSS 订阅,并清理其过滤规则关联。
func (*ConfigStore) DeleteSite ¶ added in v0.1.0
func (s *ConfigStore) DeleteSite(name string) error
DeleteSite 删除站点(预置站点禁止删除)。
判定看的是这一行的 IsBuiltin —— SyncSites 会把站点定义注册表里的每个站点都标上它 (models/presets.go),所以这一条覆盖全部内置站点。原来只硬编码了三个名字 (springsunday / hdsky / mteam),别的内置站点从任何入口调进来都会被真删掉, 连带它的 RSS 订阅一起没。前端各页的 is_builtin 拦截只是提示,服务端必须自己挡住。
func (*ConfigStore) EnabledSiteNames ¶
func (s *ConfigStore) EnabledSiteNames() (map[string]bool, error)
ReloadGlobal 从 DB 加载并刷新全局配置与目录缓存 已弃用:由各业务按需读取 DB 并更新目录缓存 ListSites 从 DB 读取站点与 RSS 配置(不依赖 global.GlobalCfg) EnabledSiteNames 返回已启用站点名(小写)的集合。用户数据的增量、走势与每日战报只算这些站点, 与 /api/v2/userinfo/aggregated 口径一致。只读 name 与 enabled 两列:不经过 ListSites, 密钥缺失或 Cookie 解不开时照样能用。
func (*ConfigStore) EncryptCookie ¶ added in v0.35.0
func (s *ConfigStore) EncryptCookie(plain string) (string, error)
func (*ConfigStore) EnsureAdmin ¶ added in v0.1.0
func (s *ConfigStore) EnsureAdmin(username, passwordHash string) error
func (*ConfigStore) GetAdmin ¶ added in v0.1.0
func (s *ConfigStore) GetAdmin(username string) (*models.AdminUser, error)
func (*ConfigStore) GetCloakConfig ¶ added in v0.35.0
func (s *ConfigStore) GetCloakConfig() (CloakConfigSnapshot, error)
GetCloakConfig 返回 endpoint + 是否已设置 token。永远不返回 token 明文。
func (*ConfigStore) GetCloakEndpoint ¶ added in v0.35.0
func (s *ConfigStore) GetCloakEndpoint() (string, error)
GetCloakEndpoint 返回当前持久化的 endpoint,记录不存在时返回空字符串。
func (*ConfigStore) GetCloakToken ¶ added in v0.35.0
func (s *ConfigStore) GetCloakToken() (string, error)
GetCloakToken 解密并返回 token 明文。 仅 test-connection / driver 内部调用,绝不能写到任何 HTTP 响应或日志。
func (*ConfigStore) GetGlobalOnly ¶ added in v0.1.0
func (s *ConfigStore) GetGlobalOnly() (models.SettingsGlobal, error)
func (*ConfigStore) GetGlobalSettings ¶ added in v0.1.0
func (s *ConfigStore) GetGlobalSettings() (models.SettingsGlobal, error)
Unified API structures: use models.SettingsGlobal for external I/O
func (*ConfigStore) GetQbitOnly ¶ added in v0.1.0
func (s *ConfigStore) GetQbitOnly() (models.QbitSettings, error)
func (*ConfigStore) GetQbitSettings ¶ added in v0.1.0
func (s *ConfigStore) GetQbitSettings() (models.QbitSettings, error)
func (*ConfigStore) GetSiteConf ¶ added in v0.1.0
func (s *ConfigStore) GetSiteConf(name models.SiteGroup) (models.SiteConfig, error)
GetSiteConf 获取指定站点配置
func (*ConfigStore) ListRSSForSite ¶ added in v0.35.0
func (s *ConfigStore) ListRSSForSite(siteName string) ([]models.RSSConfig, error)
ListRSSForSite 返回指定站点的所有 RSS 订阅。
func (*ConfigStore) ListSites ¶ added in v0.1.0
func (s *ConfigStore) ListSites() (map[models.SiteGroup]models.SiteConfig, error)
func (*ConfigStore) Load ¶ added in v0.1.0
func (s *ConfigStore) Load() (*models.Config, error)
Load 将 SQLite 中的配置组装为运行时 Config
func (*ConfigStore) ReplaceSiteRSS ¶ added in v0.1.0
func (s *ConfigStore) ReplaceSiteRSS(siteID uint, rss []models.RSSConfig) error
func (*ConfigStore) SaveAttendanceWindow ¶
func (s *ConfigStore) SaveAttendanceWindow(start, end string) error
SaveAttendanceWindow 保存签到时间窗,只写这两列。全局设置行要先存在(首次启动时保存全局设置建行)。
func (*ConfigStore) SaveCloakConfig ¶ added in v0.35.0
func (s *ConfigStore) SaveCloakConfig(endpoint, plaintextToken string, clearToken bool, profileID *string) error
SaveCloakConfig 在单次事务里同时更新 endpoint + token + profile ID; emptyToken=true 时显式清空 token,否则 plaintextToken=="" 表示保持不变(部分更新)。 profileID 为 nil 表示保持不变,指向空串表示清除。
func (*ConfigStore) SaveDailyReportSettings ¶
func (s *ConfigStore) SaveDailyReportSettings(in DailyReportSettings) error
SaveDailyReportSettings 保存战报配置,只写这三列。开启时必须选了通道,且通道都存在。
func (*ConfigStore) SaveGlobal ¶ added in v0.1.0
func (s *ConfigStore) SaveGlobal(gl models.SettingsGlobal) error
func (*ConfigStore) SaveGlobalSettings ¶ added in v0.1.0
func (s *ConfigStore) SaveGlobalSettings(gs models.SettingsGlobal) error
func (*ConfigStore) SaveGlobalSettingsWithPatch ¶ added in v0.42.1
func (s *ConfigStore) SaveGlobalSettingsWithPatch(gs models.SettingsGlobal, patch *GlobalSettingsPatch) error
SaveGlobalSettingsWithPatch 在 SaveGlobalSettings 基础上增加部分更新通道。
- patch == nil:与旧 SaveGlobalSettings 完全一致(4 字段从 gs 全量赋值),保持所有既有调用方语义。
- patch != nil:DefaultEnabled/RetainHours/MaxRetry/DefaultConcurrency 仅在对应指针非 nil 时写入, nil 时保留 DB 现值(更新分支)或落合理默认(INSERT 分支)。
func (*ConfigStore) SaveQbit ¶ added in v0.1.0
func (s *ConfigStore) SaveQbit(qb models.QbitSettings) error
func (*ConfigStore) SaveQbitSettings ¶ added in v0.1.0
func (s *ConfigStore) SaveQbitSettings(q models.QbitSettings) error
func (*ConfigStore) SetCloakEndpoint ¶ added in v0.35.0
func (s *ConfigStore) SetCloakEndpoint(endpoint string) error
SetCloakEndpoint 仅更新 endpoint 字段,不影响已存 token。
func (*ConfigStore) SetCloakToken ¶ added in v0.35.0
func (s *ConfigStore) SetCloakToken(plaintext string) error
SetCloakToken 加密后落库;空字符串表示清除 token。
func (*ConfigStore) SetSiteAttendanceEnabled ¶
func (s *ConfigStore) SetSiteAttendanceEnabled(siteName string, enabled bool) error
SetSiteAttendanceEnabled 打开或关闭站点的每日自动签到,只写这一列,不影响站点的其他配置。
func (*ConfigStore) SetSiteEnabled ¶
func (s *ConfigStore) SetSiteEnabled(site models.SiteGroup, enabled bool) error
SetSiteEnabled 只改站点的启用开关,认证方式、凭证、地址、限速和 RSS 都不动。
func (*ConfigStore) SiteCookiePlaintext ¶
func (s *ConfigStore) SiteCookiePlaintext(site models.SiteSetting) (string, error)
SiteCookiePlaintext 按 ListSites 的同一规则取站点 Cookie 明文:有密文时解密,否则用旧版明文列。 只给可信边界(CloakBrowser 后备注入浏览器)用,不要写进响应或日志。
func (*ConfigStore) SyncSites ¶ added in v0.2.0
func (s *ConfigStore) SyncSites(registeredSites []models.RegisteredSite) error
SyncSites 从注册的站点列表同步到数据库 应在应用启动时调用,确保内存中注册的站点都存在于数据库中
func (*ConfigStore) UpdateAdmin ¶ added in v0.1.0
func (s *ConfigStore) UpdateAdmin(u *models.AdminUser) error
func (*ConfigStore) UpdateAdminPassword ¶ added in v0.1.0
func (s *ConfigStore) UpdateAdminPassword(username, newHash string) error
func (*ConfigStore) UpsertSite ¶ added in v0.1.0
func (s *ConfigStore) UpsertSite(site models.SiteGroup, sc models.SiteConfig) (uint, error)
UpsertSite 整体覆盖站点的认证信息:sc 里为空的认证方式、Cookie、API Key、地址和 Passkey 会清掉已存的值。 只想改启用开关时用 SetSiteEnabled。
func (*ConfigStore) UpsertSiteWithRSS ¶ added in v0.1.0
func (s *ConfigStore) UpsertSiteWithRSS(site models.SiteGroup, sc models.SiteConfig) error
UpsertSiteWithRSS 原子性保存站点与 RSS,并进行严格校验
type DailyReportSettings ¶
type DailyReportSettings struct {
Enabled bool `json:"enabled"`
// Time 是每天发送的时刻(HH:MM,进程时区)。
Time string `json:"time"`
// ChannelIDs 是接收战报的通知通道。开启时至少一个:不沿用「空列表即全部通道」。
ChannelIDs []uint `json:"channel_ids"`
}
DailyReportSettings 是每日战报的配置。
type GlobalSettingsPatch ¶ added in v0.42.1
type GlobalSettingsPatch struct {
DefaultEnabled *bool
RetainHours *int
MaxRetry *int
DefaultConcurrency *int32
}
GlobalSettingsPatch 表达全局设置的“部分更新”语义(方式 ii)。 这些字段在 Web API 中通过指针区分“未提供”(nil → 保留 DB 现值)与 “显式提供(含 0)”(非 nil → 写入)。仅用于阻止 apiGlobal 漏传导致的静默清零, 不改变 SaveGlobalSettings 对其余字段的全量赋值语义。
type V2BroadcastResult ¶ added in v0.35.0
V2BroadcastResult describes the outcome of MaybeSendV2Broadcast.
func MaybeSendV2Broadcast ¶ added in v0.35.0
func MaybeSendV2Broadcast(ctx context.Context, db *gorm.DB, broadcaster V2Broadcaster, logger *zap.SugaredLogger, now time.Time) V2BroadcastResult
MaybeSendV2Broadcast inspects MigrationState for V2BroadcastSchemaVersion and dispatches the upgrade broadcast at most once. It is safe to call from every startup; subsequent calls become no-ops once BroadcastSent=true. The freshness window prevents broadcasts on databases whose v10 row was written long ago without the sentinel column.
Errors from the broadcaster are logged but never propagated upward — startup must not be blocked by a transient notification failure.
type V2Broadcaster ¶ added in v0.35.0
V2Broadcaster delivers the one-time "v2.0 升级完成" notification. Implementations typically wrap an internal/notify.Router but the interface is kept here to avoid a dependency cycle and to make tests trivial.
type V2BroadcasterFunc ¶ added in v0.35.0
V2BroadcasterFunc adapts a plain function into a V2Broadcaster.
func (V2BroadcasterFunc) BroadcastV2Upgrade ¶ added in v0.35.0
func (f V2BroadcasterFunc) BroadcastV2Upgrade(ctx context.Context) error
BroadcastV2Upgrade satisfies V2Broadcaster.