github.com/zly-app/zapp@v1.3.3/core/config.go (about)

     1  /*
     2  -------------------------------------------------
     3     Author :       zlyuancn
     4     date:         2020/7/2
     5     Description :
     6  -------------------------------------------------
     7  */
     8  
     9  package core
    10  
    11  import (
    12  	"github.com/spf13/viper"
    13  )
    14  
    15  // 配置结构
    16  type Config struct {
    17  	// 框架配置
    18  	Frame FrameConfig
    19  
    20  	// 服务配置
    21  	Services ServicesConfig
    22  
    23  	// 插件配置
    24  	Plugins PluginsConfig
    25  
    26  	// 组件配置
    27  	Components ComponentsConfig
    28  }
    29  
    30  // 配置
    31  type IConfig interface {
    32  	// 获取配置
    33  	Config() *Config
    34  	// 获取配置viper结构
    35  	GetViper() *viper.Viper
    36  	/*解析指定key数据到结构中
    37  	  key 配置的key
    38  	  outPtr 接收配置的变量
    39  	  ignoreNotSet 如果未配置key, 则忽略, 默认为false
    40  	*/
    41  	Parse(key string, outPtr interface{}, ignoreNotSet ...bool) error
    42  	/*解析组件配置, key的值为 components.{componentType}.{componentName}
    43  	  componentType 组件类型
    44  	  componentName 组件名
    45  	  outPtr 接收配置的变量
    46  	  ignoreNotSet 如果未配置key, 则忽略, 默认为false
    47  	*/
    48  	ParseComponentConfig(componentType ComponentType, componentName string, outPtr interface{}, ignoreNotSet ...bool) error
    49  	/*解析插件配置, key的值为 plugins.{pluginType}
    50  	  pluginType 插件类型
    51  	  outPtr 接收配置的变量
    52  	  ignoreNotSet 如果未配置key, 则忽略, 默认为false
    53  	*/
    54  	ParsePluginConfig(pluginType PluginType, outPtr interface{}, ignoreNotSet ...bool) error
    55  	/*解析服务配置, key的值为 services.{serviceType}
    56  	  serviceType 服务类型
    57  	  outPtr 接收配置的变量
    58  	  ignoreNotSet 如果未配置key, 则忽略, 默认为false
    59  	*/
    60  	ParseServiceConfig(serviceType ServiceType, outPtr interface{}, ignoreNotSet ...bool) error
    61  	// 检查是否存在flag, 注意: flag是忽略大小写的
    62  	HasFlag(flag string) bool
    63  	// 获取所有的flag, 注意: flag列表是无序的
    64  	GetFlags() []string
    65  	// 获取标签的值, 标签名是忽略大小写的, 标签不存在时返回空字符串
    66  	GetLabel(name string) string
    67  	// 获取标签数据
    68  	GetLabels() map[string]string
    69  	// 观察变更, 失败会fatal
    70  	WatchKey(groupName, keyName string, opts ...ConfigWatchOption) IConfigWatchKeyObject
    71  }
    72  
    73  // frame配置
    74  type FrameConfig struct {
    75  	// debug标志
    76  	Debug bool
    77  	// 环境名
    78  	Env string
    79  	// app 名
    80  	Name string
    81  	// 主动清理内存间隔时间(毫秒), <= 0 表示禁用
    82  	FreeMemoryInterval int
    83  	// 默认等待服务启动阶段, 等待时间(毫秒), 如果时间到未收到服务启动成功信号则将服务标记为不稳定状态然后继续开始工作(我们总不能一直等着吧)
    84  	WaitServiceRunTime int
    85  	// 默认服务不稳定观察时间, 等待时间(毫秒), 如果时间到仍未收到服务启动成功信号也将服务标记为启动成功
    86  	ServiceUnstableObserveTime int
    87  	// flag, 注意: flag是忽略大小写的
    88  	Flags []string
    89  	// 标签, 注意: 标签名是忽略大小写的
    90  	Labels map[string]string
    91  	// log配置
    92  	Log LogConfig
    93  	// app初始时是否打印配置
    94  	PrintConfig bool
    95  }
    96  
    97  type LogConfig struct {
    98  	Level                      string // 日志等级, debug, info, warn, error, dpanic, panic, fatal
    99  	Json                       bool   // 启用json编码器, 输出的每一行日志转为json格式
   100  	WriteToStream              bool   // 输出到屏幕
   101  	WriteToFile                bool   // 日志是否输出到文件
   102  	Name                       string // 日志文件名, 末尾会自动附加 .log 后缀
   103  	AppendPid                  bool   // 是否在日志文件名后附加进程号
   104  	Path                       string // 默认日志存放路径
   105  	FileMaxSize                int    // 每个日志最大尺寸,单位M
   106  	FileMaxBackupsNum          int    // 日志文件最多保存多少个备份, 0表示永久
   107  	FileMaxDurableTime         int    // 文件最多保存多长时间,单位天, 0表示永久
   108  	Compress                   bool   // 是否压缩历史日志
   109  	TimeFormat                 string // 时间显示格式
   110  	Color                      bool   // 是否打印彩色日志等级, 只有关闭json编码器才生效
   111  	CapitalLevel               bool   // 是否大写日志等级
   112  	DevelopmentMode            bool   // 开发者模式, 在开发者模式下日志记录器在写完DPanic消息后程序会感到恐慌
   113  	ShowFileAndLinenum         bool   // 显示文件路径和行号
   114  	ShowFileAndLinenumMinLevel string // 最小显示文件路径和行号的等级. 推荐所有等级都打印代码行, 相对于能快速定位问题来说, 这点性能损耗无关紧要
   115  	MillisDuration             bool   // 对zap.Duration转为毫秒
   116  }
   117  
   118  // 服务配置
   119  type ServicesConfig map[string]interface{}
   120  
   121  // 插件配置
   122  type PluginsConfig map[string]interface{}
   123  
   124  // 组件配置
   125  type ComponentsConfig map[string]map[string]interface{}