You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.5 KiB
125 lines
4.5 KiB
package console
|
|
|
|
import (
|
|
"time"
|
|
|
|
"yunyan/lego/utils/mapstructure"
|
|
"yunyan/modules"
|
|
)
|
|
|
|
// Options console 模块配置,对应 yaml 的 modules.console。
|
|
type Options struct {
|
|
modules.Options
|
|
HTTP HTTPConf
|
|
StaticDir string // console 独立精简前端目录,默认 ./consoleweb
|
|
TokenKey string // JWT 签名密钥(登录签发 + 鉴权校验)
|
|
AdminAccount string // 超管账号(第一期仅超管 + token)
|
|
AdminPassword string // 超管密码
|
|
SiteName string // 控制面站点名称(前端顶栏显示)
|
|
// EncryptKey 依赖服务(svcconfig)加密字段的 AES-256-GCM 主密钥
|
|
// (建议从 ${FIELD_ENCRYPT_KEY} 环境变量注入;生产必须覆盖默认值)。
|
|
EncryptKey string
|
|
// SkipEnsureTables 为 true 时,新建应用连接后不对其设备库跑建表(AutoMigrate)。
|
|
// 指向已存在表结构的生产库(如 Supabase)时打开,避免 console 改动其 schema;只读查看场景安全。
|
|
SkipEnsureTables bool
|
|
// Stat 统计汇总管道配置(消费 analyze 双写到 NATS 的埋点事件,落 stats_global_day)。
|
|
Stat StatConf
|
|
// Inspect 第三方服务巡检配置(定时校验 svc_config 并探测服务可用性,结果落 svc_health)。
|
|
Inspect InspectConf
|
|
}
|
|
|
|
type HTTPConf struct {
|
|
Addr string // HTTP 监听地址,如 ":8080"
|
|
}
|
|
|
|
// StatConf 统计汇总管道配置,对应 yaml 的 modules.console.Stat。
|
|
type StatConf struct {
|
|
Subject string // 订阅的 NATS subject,需与各 analyze 的 modules.analyze.Subject 一致;为空则不启用消费
|
|
Stream string // JetStream 流名(不存在则自动创建,捕获 Subject)
|
|
Durable string // 持久消费者名(断线/重启续传;快照为绝对值,重放幂等)
|
|
Timezone string // 统计日界时区,默认 Asia/Shanghai;须与各 analyze 端一致,决定 0 点切日口径
|
|
// RecvLogCap 「统计日志」面板每天保留的快照接收记录条数上限(按接收时间分天存 Redis,
|
|
// 仅留最新 N 条,供后台核对每天汇报数据是否正常进来)。默认 2000,<=0 时回退默认。
|
|
RecvLogCap int
|
|
}
|
|
|
|
// InspectConf 第三方服务巡检配置,对应 yaml 的 modules.console.Inspect。
|
|
//
|
|
// 巡检会用真实凭据打第三方服务商的鉴权接口(只读免费为主,见 inspect_probe.go),
|
|
// 周期不宜过密:默认 30 分钟一轮足以在故障发生后半小时内发现,也不会让服务商把我们当异常流量。
|
|
type InspectConf struct {
|
|
Enable bool // 是否启用定时巡检;false 时仍可在后台手动触发
|
|
IntervalMin int // 巡检周期(分钟),默认 30
|
|
StartDelaySec int // 服务启动后首轮巡检的延迟(秒),默认 60——避开启动瞬间的网络/依赖抖动
|
|
TimeoutSec int // 单个服务探测超时(秒),默认 10
|
|
Concurrency int // 并发探测数,默认 8
|
|
}
|
|
|
|
func (c InspectConf) IntervalDuration() time.Duration {
|
|
if c.IntervalMin <= 0 {
|
|
return 30 * time.Minute
|
|
}
|
|
return time.Duration(c.IntervalMin) * time.Minute
|
|
}
|
|
|
|
func (c InspectConf) StartDelay() time.Duration {
|
|
if c.StartDelaySec <= 0 {
|
|
return 60 * time.Second
|
|
}
|
|
return time.Duration(c.StartDelaySec) * time.Second
|
|
}
|
|
|
|
func (c InspectConf) TimeoutDuration() time.Duration {
|
|
if c.TimeoutSec <= 0 {
|
|
return 10 * time.Second
|
|
}
|
|
return time.Duration(c.TimeoutSec) * time.Second
|
|
}
|
|
|
|
func (c InspectConf) ConcurrencyOrDefault() int {
|
|
if c.Concurrency <= 0 {
|
|
return 8
|
|
}
|
|
return c.Concurrency
|
|
}
|
|
|
|
func (this *Options) LoadConfig(settings map[string]interface{}) (err error) {
|
|
if settings != nil {
|
|
if err = this.Options.LoadConfig(settings); err != nil {
|
|
return
|
|
}
|
|
if err = mapstructure.Decode(settings, this); err != nil {
|
|
return
|
|
}
|
|
}
|
|
if this.HTTP.Addr == "" {
|
|
this.HTTP.Addr = ":8080"
|
|
}
|
|
if this.StaticDir == "" {
|
|
this.StaticDir = "./consoleweb"
|
|
}
|
|
if this.TokenKey == "" {
|
|
this.TokenKey = "console-default-token-key"
|
|
}
|
|
if this.SiteName == "" {
|
|
this.SiteName = "Console"
|
|
}
|
|
if this.EncryptKey == "" {
|
|
this.EncryptKey = "console-default-encrypt-key-xxxxx!" // 生产必须通过 ${FIELD_ENCRYPT_KEY} 覆盖
|
|
}
|
|
if this.Stat.Stream == "" {
|
|
this.Stat.Stream = "STATS"
|
|
}
|
|
if this.Stat.Durable == "" {
|
|
this.Stat.Durable = "console-stats"
|
|
}
|
|
if this.Stat.RecvLogCap <= 0 {
|
|
this.Stat.RecvLogCap = 2000
|
|
}
|
|
// 巡检默认开启:yaml 里没写 Inspect 段就按默认参数跑(老环境的配置文件无需同步改动);
|
|
// 写了段就完全听 yaml 的,允许显式 Enable:false 关掉。
|
|
if _, ok := settings["Inspect"]; !ok {
|
|
this.Inspect.Enable = true
|
|
}
|
|
return
|
|
}
|
|
|