module

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package module 定义功能模块的统一接口。

每个模块拥有独立的配置文件(config.Store 中以 ID 命名)、独立的日志缓冲, 以及挂载在 /api/<ID>/ 下的管理接口。

Index

Constants

View Source
const (
	MetricCounter = "counter"
	MetricGauge   = "gauge"
	// MetricHistogram 的采样点由 Family 归组:_bucket、_sum、_count 共用同一组 HELP / TYPE
	MetricHistogram = "histogram"
)

指标类型

View Source
const (
	CheckOK   = "ok"
	CheckInfo = "info"
	CheckWarn = "warn"
	CheckFail = "fail"
)

自检结果状态

Variables

This section is empty.

Functions

func DocOf added in v0.3.0

func DocOf(r Router, pattern string, d Doc)

DocOf 在 Router 支持文档时登记接口文档,否则忽略。

func WriteSamples added in v0.5.0

func WriteSamples(w io.Writer, samples []Sample) error

WriteSamples 以 Prometheus 文本格式(0.0.4)写出采样点。同名指标聚合在一起,仅输出一次 HELP / TYPE。

Types

type AccessLogger added in v0.6.5

type AccessLogger interface {
	AccessLogs() bool
}

AccessLogger 由写访问日志的模块实现。访问日志量大且需要回看,落盘时按天归档并压缩, 按“访问日志保留天数”与“访问日志总大小”清理,其他模块只保留一份历史文件。

type CheckEnv added in v0.5.0

type CheckEnv struct {
	// PublicIPv4 / PublicIPv6 为查询到的出口公网地址,未知时为空
	PublicIPv4 string
	PublicIPv6 string
}

CheckEnv 是自检时由框架提供的公共信息。

type CheckResult added in v0.5.0

type CheckResult struct {
	ID     string            `json:"id"`
	Status string            `json:"status"`
	Params map[string]string `json:"params,omitempty"`
}

CheckResult 是一项自检结果。ID 与 Params 由前端翻译成可读说明,后端不拼接面向人的文案。

type Doc added in v0.3.0

type Doc struct {
	Summary string
	Req     any
	Resp    any
}

Doc 描述一个接口的文档信息,Req / Resp 为示例值(通常是结构体指针或切片),用于生成数据结构说明。

type DocRouter added in v0.3.0

type DocRouter interface {
	Doc(pattern string, d Doc)
}

DocRouter 由支持接口文档的 Router 实现。模块可通过类型断言为接口补充文档信息, pattern 与 HandleFunc 的写法一致(相对模块前缀)。

type Env

type Env struct {
	Store *config.Store
	Logs  *logbuf.Buffer
}

Env 是框架注入给模块的运行环境。

type HTTPClientAware added in v0.5.9

type HTTPClientAware interface {
	SetHTTPClient(fn func() *http.Client)
}

HTTPClientAware 由需要访问外网(例如下载托管程序)的模块实现。 框架注入的函数每次调用都返回遵循“更新代理”设置的客户端,代理修改后立即生效。

type Info added in v0.6.4

type Info struct {
	ID    string `json:"id"`
	Title string `json:"title"`
}

Info 是模块的标识与界面显示名称。

type LogSourcer added in v0.6.5

type LogSourcer interface {
	LogSources() []string
}

LogSourcer 由日志来源来自配置(监听器、规则、实例等名称)的模块实现,返回当前配置中的全部来源标签。 日志页的来源下拉据此列出还没有写过日志、或日志已被挤出内存的来源,与写日志时使用的标签保持一致。

type Metricer added in v0.5.0

type Metricer interface {
	Metrics() []Sample
}

Metricer 由希望导出 Prometheus 指标的模块实现。

type Migrator added in v0.6.2

type Migrator interface {
	Migrate(store *config.Store) (from string, err error)
}

Migrator 由需要在全部模块初始化之前迁移配置的模块实现,例如从其他模块的配置文件中接收拆分出来的规则。 返回的 from 非空表示本次有配置从模块 from 迁来:已有 from 权限的 API 令牌会同时获得本模块的同级权限, from 被关闭时本模块也一并关闭,保持迁移前的行为。没有需要迁移的内容时返回空串。

type Module

type Module interface {
	// ID 是模块唯一标识,同时作为配置文件名和 API 前缀
	ID() string
	// Title 是界面显示名称
	Title() string
	// Init 加载配置,不启动任何网络服务
	Init(env Env) error
	// Start 按配置启动服务,ctx 在程序退出时取消
	Start(ctx context.Context) error
	// Stop 停止全部服务并释放端口
	Stop() error
	// Routes 注册管理接口
	Routes(r Router)
}

Module 是所有功能模块需要实现的接口。

type PublicRouter added in v0.6.6

type PublicRouter interface {
	PublicRoutes(r Router)
	PublicAuth(r *http.Request) bool
}

PublicRouter 由需要提供免登录接口的模块实现,例如被控设备上的客户端向管理后台取指令。 PublicRoutes 注册的接口挂载在 /api/<ID>/public 下(pattern 写法同 Router,路径相对该前缀), 不经过登录鉴权、角色检查与审计,由模块在处理函数中自行校验凭据;模块关闭时不注册。 PublicAuth 判断请求是否带有该模块认可的有效凭据,管理后台据此让这类请求越过安全入口, 须是不访问网络、开销很小的检查;读取请求体时要还原 r.Body,供处理函数再次读取。

type Router

type Router interface {
	HandleFunc(pattern string, h http.HandlerFunc)
}

Router 用于注册模块接口。pattern 采用 Go 1.22 ServeMux 语法, 但路径部分相对模块前缀,例如 "GET /rules" 实际挂载为 "GET /api/<ID>/rules"。

type Sample added in v0.5.0

type Sample struct {
	Name string
	// Family 为 HELP / TYPE 所属的指标族名称,留空时取 Name。直方图的 _bucket / _sum / _count 填基础名称
	Family string
	// Help 与 Type 取同名指标第一个采样点的值
	Help   string
	Type   string
	Labels map[string]string
	Value  float64
}

Sample 是一个 Prometheus 指标采样点。

type SelfChecker added in v0.5.0

type SelfChecker interface {
	SelfCheck(ctx context.Context, env CheckEnv) []CheckResult
}

SelfChecker 由能够检查自身配置与外部环境是否一致的模块实现。

type Summarizer

type Summarizer interface {
	Summary() []SummaryItem
}

Summarizer 由希望在总览页展示状态的模块实现。

type SummaryItem

type SummaryItem struct {
	Label string `json:"label"`
	Value string `json:"value"`
	Level string `json:"level,omitempty"`
}

SummaryItem 是总览页展示的一项指标。Level 为 ok / warn / error / info,用于着色。

Jump to

Keyboard shortcuts

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