Documentation ¶
Index ¶
- Constants
- func AddClient(section string, handler Handler)
- func AddServer(section string, handler Handler)
- func LoadConfig(section string, v interface{})
- func LoadConfigFile(path string, section string, v interface{})
- func Main()
- func OnExit(handler func(ctx context.Context))
- func OnStart(handler func(ctx context.Context) error)
- func WithStats(ctx context.Context, stats *Stats) context.Context
- type Handler
- type MetaInfo
- type Stats
Constants ¶
const ( // ExitCodeOK 是正常退出时候的错误码。 ExitCodeOK = iota // ExitCodeInvalidHandler 是系统错误码,表示执行的 handler 类型非法。 ExitCodeInvalidHandler // ExitCodeInvalidConfig 是配置文件解析失败时候的错误码。 ExitCodeInvalidConfig // ExitCodeHandlerError 是 handler 执行出错或者 panic 返回的错误码。 ExitCodeHandlerError )
Variables ¶
This section is empty.
Functions ¶
func LoadConfig ¶
func LoadConfig(section string, v interface{})
LoadConfig 将 v 注册到启动逻辑里面,一旦配置文件读取之后,会从指定的 secion 给 v 赋值。
例如:
type FooConfig struct { Bar int `config:"bar"` } // 声明一个全局变量,用来方便业务代码读取配置。 var Foo *FooConfig func init() { // 在自启动函数里面注册 Foo,这样服务启动后就会自动从配置里面读取 Foo 的值。 // 假如读取过程中出现任何问题,比如配置里面没有 foo 这个字段,Foo 为 nil。 runner.LoadConfig("foo", &Foo) }
func LoadConfigFile ¶
LoadConfigFile 将 v 注册到启动逻辑里面,一旦配置文件读取之后,会从指定的 secion 给 v 赋值。 跟 LoadConfig 不一样的是,通过指定 path,可以指定一个跟默认配置文件不一样的配置文件。
Types ¶
type Handler ¶
type Handler interface{}
Handler 是一个可以执行的函数。
允许的函数形式包括:
func(ctx context.Context): 执行一个函数,ctx 由 runner 传入。
func(ctx context.Context) error:与上面的形式类似,只是允许返回一个 error,框架会自动报错。
func(ctx context.Context, config *Config): 这里的 `Config` 是配置文件里面对应的数据结构, Run 会自动解析配置文件并反序列化到 Config 里面去。
func(ctx context.Context, config *Config) error:与上面的形式类似,只是允许返回一个 error,框架会自动报错。
type MetaInfo ¶
type MetaInfo struct { Project string `json:"project"` Namespace string `json:"namespace"` Env string `json:"env"` Type string `json:"type"` GitRefName string `json:"git_ref_name"` GitRevision string `json:"git_revision"` }
MetaInfo 是当前环境的信息,由 CI 系统自动生成。
type Stats ¶
type Stats struct {
// contains filtered or unexported fields
}
Stats 用于记录运行时的统计信息。
func StatsFromContext ¶
StatsFromContext 返回 ctx 中存放的 stats,如果之前没有设置 stats 则返回 nil。 由于 stats 做了 nil 兼容,所有函数在 stats == nil 时候依然可以正常调用, 所以业务代码永远不需要检查这个返回值是否为 nil 就可以正常使用。