Documentation ¶
Index ¶
- Constants
- Variables
- func Check(a interface{}) error
- func LoadViperConfigFromEnv(viperCfg *viper.Viper, envPrefix string, envKeys []string, ...) error
- func LoadViperConfigFromFile(viperCfg *viper.Viper, configFilePath string) error
- func LoadViperConfigFromRemote(viperCfg *viper.Viper, ...) error
- func NotifySignal(logger IStarterLogger)
- func OpenFile(fileName string, flag int, perm os.FileMode) (*os.File, error)
- func Register(name string, fn func())
- func RegisterStarter(s Starter)
- func SortStarters()
- func ViperLoader() *viper.Viper
- type Application
- type BaseStarter
- func (*BaseStarter) Check(*StarterContext) bool
- func (*BaseStarter) Init(*StarterContext)
- func (*BaseStarter) Name() string
- func (s *BaseStarter) Priority() int
- func (s *BaseStarter) PriorityGroup() PriorityGroup
- func (*BaseStarter) Setup(*StarterContext)
- func (*BaseStarter) Start(*StarterContext)
- func (*BaseStarter) StartBlocking() bool
- func (*BaseStarter) Stop() error
- type CommandLineStarterLogger
- type CommonStarterLogger
- type Global
- type IStarterLogger
- type LogFormatter
- type LogFormatterParams
- type PriorityGroup
- type Starter
- type StarterContext
- func (s StarterContext) Configs() *viper.Viper
- func (s StarterContext) Global() Global
- func (s StarterContext) Item(key string) interface{}
- func (s StarterContext) Logger() IStarterLogger
- func (s StarterContext) PassError(name, step string, err error) bool
- func (s StarterContext) PassFail(name, step string, err error) bool
- func (s StarterContext) PassWarning(name, step string, err error) bool
- func (s StarterContext) SetConfigs(vpcfg *viper.Viper)
- func (s StarterContext) SetGlobal(g Global)
- func (s StarterContext) SetItem(key string, item interface{})
- func (s StarterContext) SetLogger(logger IStarterLogger)
- type StarterLogger
- func (l *StarterLogger) Debug(name, step, msg string)
- func (l *StarterLogger) Error(name, step string, err error)
- func (l *StarterLogger) Fail(name, step string, err error)
- func (l *StarterLogger) Info(name, step, msg string)
- func (l *StarterLogger) OK(name, step, msg string)
- func (l *StarterLogger) Warning(name, step, msg string)
- type StarterLoggerOutput
Constants ¶
View Source
const ( KeyConfig = "_vpcfg" KeyLogger = "_logger" KeyGlobal = "_global" )
View Source
const ( Env = "env" // 允许环境:dev、testing、product Host = "host" // 主机地址 Endpoint = "endpoint" // 节点 AppName = "appname" // 应用名 Version = "version" // 应用版本 )
初始全局配置
View Source
const ( ConfigFilePathFlag = "config_file" RemoteProviderFlag = "remote_provider" RemoteEndpointFlag = "remote_endpoint" RemoteKVPathFlag = "remote_kv_path" RemoteTypeFlag = "remote_type" RemoteWatchDurationFlag = "remote_watch_duration" EnvPrefixFlag = "env_prefix" EnvKeysFlag = "env_keys" EnvAllowEmptyFlag = "env_allow_empty" EnvAutomaticFlag = "env_automatic" )
View Source
const ( StepInit = "Init" StepSetup = "Setup" StepStart = "Start" StepCheck = "Check" StepStop = "Hook" )
启动步骤常量
View Source
const ( DebugLevel = "Debug" InfoLevel = "Info" OKLevel = "OK" WarningLevel = "Warning" ErrorLevel = "Error" FailLevel = "Fail" )
日志等级命名常量
View Source
const ( BasicGroup PriorityGroup = 30 // 基础组件级别,适用设置优先启动的组件,如日志等 ResourcesGroup PriorityGroup = 20 // 资源组件级别,适用设置项目需要的外围资源组件优先级,如数据库连接池等 AppGroup PriorityGroup = 10 // 应用组件级别,适用设置应用程序启动或运行时启动的组件,如web引擎、数据传输对象验证器、JWT 令牌验证工具等 INT_MAX = int(^uint(0) >> 1) // 最优先启动级别 INT_MIN = 0 // 最末位启动级别 DEFAULT_PRIORITY = 100 // 默认 )
View Source
const (
StarterPosition = "Starter"
)
记录位置命名常量
Variables ¶
View Source
var StarterManager = new(starterManager)
View Source
var (
ViperCfg *viper.Viper // viper 配置实例
)
Functions ¶
func Check ¶
func Check(a interface{}) error
结构体指针检查验证,如果传入的interface为nil,就通过log.Panic函数抛出一个异常,它被用在starter中检查组件资源是否已启动
func LoadViperConfigFromEnv ¶
func LoadViperConfigFromEnv(viperCfg *viper.Viper, envPrefix string, envKeys []string, envAllowEmpty, envAutomatic bool, envKeyReplacer *strings.Replacer) error
Viper读取环境变量 *
- @Description: Viper读取环境变量
- @param viperCfg Viper实例
- @param envPrefix 指定需读取的环境变量前缀
- @param envKeys 绑定特定环境变量
- @param envAllowEmpty 是否允许读取空值的环境变量
- @param envAutomatic 是否载入所有环境变量,如设置Prefix,则只筛选有前缀的载入
- @param envKeyReplacer 键名字符替换器,常用语替换键名连接符
- @return error
func LoadViperConfigFromFile ¶
*
- @Description: Viper 读取配置文件
- @param viperCfg Viper实例
- @param configFilePath 配置文件路径及文件名
- @return error
func LoadViperConfigFromRemote ¶
func LoadViperConfigFromRemote(viperCfg *viper.Viper, remoteProvider, remoteEndpoint, remotePath, remoteType string, etcdWatchDuration time.Duration) error
*
- @Description: Viper 读取远程配置系统
- @param viperCfg Viper实例
- @param remoteProvider 远程配置提供者etcd/consult
- @param remoteEndpoint 远程配置主机节点
- @param remotePath 远程配置键节点
- @param remoteType 因为在字节流中没有文件扩展名,所以这里需要设置下类型。支持的扩展名有 "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv"
- @param etcdWatchDuration etcd watch 监听时间间隔
- @return error
func ViperLoader ¶
Types ¶
type Application ¶
type Application struct {
Sctx *StarterContext // 应用启动器上下文
}
应用程序启动管理器
func NewApplicationWithStarterLoggerWriter ¶
func NewApplicationWithStarterLoggerWriter(vpcfg *viper.Viper, logWriters ...io.Writer) *Application
创建一个带输出启动日志的应用管理器
func (*Application) Up ¶
func (app *Application) Up()
启动应用程序所有基础资源 (初始化 -> 安装 -> 检查 -> 启动 -> 监听系统退出信号)
type BaseStarter ¶
type BaseStarter struct{}
基础空启动器,可便于被其他具体的基础资源嵌入
func (*BaseStarter) Name ¶
func (*BaseStarter) Name() string
type CommandLineStarterLogger ¶
type CommandLineStarterLogger struct {
StarterLogger
}
标准颜色输出日志记录器
func NewCommandLineStarterLogger ¶
func NewCommandLineStarterLogger(env string) *CommandLineStarterLogger
针对终端输出的默认启动日志记录器
type CommonStarterLogger ¶
type CommonStarterLogger struct {
StarterLogger
}
通用启动日志记录器(输出到终端和启动日志文件)
func NewStarterLoggerWithWriters ¶
func NewStarterLoggerWithWriters(env string, writers ...io.Writer) *CommonStarterLogger
type Global ¶ added in v1.2.4
type Global map[string]interface{}
func (Global) GetAppName ¶ added in v1.2.4
func (Global) GetEndpoint ¶ added in v1.2.4
func (Global) GetVersion ¶ added in v1.2.4
type IStarterLogger ¶
type LogFormatterParams ¶
type LogFormatterParams struct { Position string // 日志记录位置 Name string // 启动器名称 Step string // 启动器步骤 LogLevel string // 记录日志级别 TimeStamp time.Time // 记录时间戳 Message string // 记录信息 }
格式化输出参数
func (*LogFormatterParams) LogLevelColor ¶
func (p *LogFormatterParams) LogLevelColor() string
每种错误级别输出不同的颜色
func (*LogFormatterParams) LogPositionColor ¶
func (p *LogFormatterParams) LogPositionColor() string
日志输出位置颜色标示
func (*LogFormatterParams) LogStepColor ¶
func (p *LogFormatterParams) LogStepColor() string
启动器步骤颜色标示
type Starter ¶
type Starter interface { // 组件名 Name() string // 初始化:资源组件读取配置信息 Init(sctx *StarterContext) // 安装:检查该组件的前置依赖 Setup(sctx *StarterContext) // 启动:该资源组件的连接或启动以供应用程序后续使用 Start(sctx *StarterContext) // 启动状态检查 Check(sctx *StarterContext) bool // 阻塞启动:设置需要后置启动的资源组件,默认为false StartBlocking() bool // 资源停止: // 通常在启动时遇到异常时或者启用远程管理时,用于释放资源和终止资源的使用, // 通常要优雅的释放,等待正在进行的任务继续,但不再接受新的任务 Stop() error // 优先组:从高到低分:系统级别、基本资源级别、应用级别三组 PriorityGroup() PriorityGroup // 设置该资源组件的启动优先级,默认为DEFAULT_PRIORITY,最大为INT_MAX Priority() int }
启动器接口
type StarterContext ¶
type StarterContext map[string]interface{}
资源启动器上下文,用来在服务资源初始化、安装、启动和停止的生命周期中变量和对象的传递
func CreateDefaultStarterContext ¶
func CreateDefaultStarterContext(vpcfg *viper.Viper, logger IStarterLogger) *StarterContext
创建一个默认最少配置启动器上下文
func (StarterContext) Configs ¶
func (s StarterContext) Configs() *viper.Viper
func (StarterContext) Global ¶ added in v1.2.4
func (s StarterContext) Global() Global
func (StarterContext) Item ¶ added in v1.1.6
func (s StarterContext) Item(key string) interface{}
func (StarterContext) Logger ¶
func (s StarterContext) Logger() IStarterLogger
func (StarterContext) PassError ¶
func (s StarterContext) PassError(name, step string, err error) bool
err == nil 返回 true,否则记录启动器错误日志并返回false
func (StarterContext) PassFail ¶
func (s StarterContext) PassFail(name, step string, err error) bool
err == nil,返回true ;err != nil 致命错误处理,直接panic
func (StarterContext) PassWarning ¶
func (s StarterContext) PassWarning(name, step string, err error) bool
有错误则记录启动器警告日志
func (StarterContext) SetConfigs ¶
func (s StarterContext) SetConfigs(vpcfg *viper.Viper)
func (StarterContext) SetGlobal ¶ added in v1.2.4
func (s StarterContext) SetGlobal(g Global)
func (StarterContext) SetItem ¶ added in v1.1.6
func (s StarterContext) SetItem(key string, item interface{})
func (StarterContext) SetLogger ¶
func (s StarterContext) SetLogger(logger IStarterLogger)
type StarterLogger ¶
type StarterLogger struct { Outputs []*StarterLoggerOutput // contains filtered or unexported fields }
启动器日志记录器
func (*StarterLogger) Debug ¶ added in v1.2.0
func (l *StarterLogger) Debug(name, step, msg string)
func (*StarterLogger) Error ¶ added in v1.2.0
func (l *StarterLogger) Error(name, step string, err error)
func (*StarterLogger) Fail ¶ added in v1.2.0
func (l *StarterLogger) Fail(name, step string, err error)
func (*StarterLogger) Info ¶ added in v1.2.0
func (l *StarterLogger) Info(name, step, msg string)
func (*StarterLogger) OK ¶ added in v1.2.0
func (l *StarterLogger) OK(name, step, msg string)
func (*StarterLogger) Warning ¶ added in v1.2.0
func (l *StarterLogger) Warning(name, step, msg string)
type StarterLoggerOutput ¶
type StarterLoggerOutput struct { Formatter LogFormatter // 格式转化器 Writers io.Writer // 输出 }
可定义多个输出
Source Files ¶
Click to show internal directories.
Click to hide internal directories.