github.com/zly-app/zapp@v1.3.3/opts.go (about) 1 /* 2 ------------------------------------------------- 3 Author : zlyuancn 4 date: 2020/7/2 5 Description : 6 ------------------------------------------------- 7 */ 8 9 package zapp 10 11 import ( 12 "fmt" 13 14 "go.uber.org/zap" 15 16 "github.com/zly-app/zapp/config" 17 "github.com/zly-app/zapp/core" 18 ) 19 20 type Option func(opt *option) 21 22 type option struct { 23 // 配置选项 24 ConfigOpts []config.Option 25 // 日志选项 26 LogOpts []zap.Option 27 28 // 启用守护 29 EnableDaemon bool 30 // handlers 31 Handlers map[HandlerType][]Handler 32 33 // 忽略未启用的插件注入 34 IgnoreInjectOfDisablePlugin bool 35 // 插件 36 Plugins []core.PluginType 37 // 自定义启用插件函数 38 CustomEnablePluginsFn func(app core.IApp, plugins []core.PluginType) []core.PluginType 39 40 // 在服务不稳定观察阶段中出现错误则退出 41 ExitOnErrOfObserveServiceUnstable bool 42 // 忽略未启用的服务注入 43 IgnoreInjectOfDisableService bool 44 // 服务 45 Services []core.ServiceType 46 // 自定义启用服务函数 47 CustomEnableServicesFn func(app core.IApp, services []core.ServiceType) []core.ServiceType 48 49 // 自定义组件函数 50 CustomComponentFn func(app core.IApp) core.IComponent 51 } 52 53 func newOption(opts ...Option) *option { 54 opt := &option{ 55 EnableDaemon: false, 56 Handlers: make(map[HandlerType][]Handler), 57 ExitOnErrOfObserveServiceUnstable: true, 58 59 IgnoreInjectOfDisablePlugin: false, 60 Plugins: make([]core.PluginType, 0), 61 62 IgnoreInjectOfDisableService: false, 63 Services: make([]core.ServiceType, 0), 64 } 65 for _, o := range opts { 66 o(opt) 67 } 68 return opt 69 } 70 71 // 检查自定义启用插件 72 func (o *option) CheckPlugins(app core.IApp) error { 73 if o.CustomEnablePluginsFn != nil { 74 o.Plugins = o.CustomEnablePluginsFn(app, o.Plugins) 75 } 76 77 mm := make(map[core.PluginType]struct{}, len(o.Plugins)) 78 for _, t := range o.Plugins { 79 l := len(mm) 80 mm[t] = struct{}{} 81 if len(mm) == l { 82 return fmt.Errorf("插件启用重复: %v", t) 83 } 84 } 85 return nil 86 } 87 88 // 检查自定义启用服务 89 func (o *option) CheckServices(app core.IApp) error { 90 if o.CustomEnableServicesFn != nil { 91 o.Services = o.CustomEnableServicesFn(app, o.Services) 92 } 93 94 mm := make(map[core.ServiceType]struct{}, len(o.Services)) 95 for _, t := range o.Services { 96 l := len(mm) 97 mm[t] = struct{}{} 98 if len(mm) == l { 99 return fmt.Errorf("服务启用重复: %v", t) 100 } 101 } 102 return nil 103 } 104 105 // 设置config选项 106 func WithConfigOption(opts ...config.Option) Option { 107 return func(opt *option) { 108 opt.ConfigOpts = append(opt.ConfigOpts, opts...) 109 } 110 } 111 112 // 日志选项 113 func WithLoggerOptions(opts ...zap.Option) Option { 114 return func(opt *option) { 115 opt.LogOpts = append(opt.LogOpts, opts...) 116 } 117 } 118 119 // 启用守护进程模块 120 func WithEnableDaemon() Option { 121 return func(opt *option) { 122 opt.EnableDaemon = true 123 } 124 } 125 126 // 添加handler 127 func WithHandler(t HandlerType, hs ...Handler) Option { 128 return func(opt *option) { 129 opt.Handlers[t] = append(opt.Handlers[t], hs...) 130 } 131 } 132 133 // 忽略未启用的插件注入 134 func WithIgnoreInjectOfDisablePlugin(ignore ...bool) Option { 135 return func(opt *option) { 136 opt.IgnoreInjectOfDisablePlugin = len(ignore) == 0 || ignore[0] 137 } 138 } 139 140 // 启动插件(使用者不要主动调用这个函数, 应该由plugin包装, 因为plugin的选项无法通过这个函数传递) 141 func WithPlugin(pluginType core.PluginType, enable ...bool) Option { 142 return func(opt *option) { 143 if len(enable) == 0 || enable[0] { 144 opt.Plugins = append(opt.Plugins, pluginType) 145 } 146 } 147 } 148 149 // 自定义启用插件 150 // 151 // 如果要启用某个插件, 必须先注册该插件 152 func WithCustomEnablePlugin(fn func(app core.IApp, plugins []core.PluginType) []core.PluginType) Option { 153 return func(opt *option) { 154 opt.CustomEnablePluginsFn = fn 155 } 156 } 157 158 // 在服务不稳定观察阶段中出现错误则退出, 默认true 159 func WithExitOnErrOfObserveServiceUnstable(exit ...bool) Option { 160 return func(opt *option) { 161 opt.ExitOnErrOfObserveServiceUnstable = len(exit) == 0 || exit[0] 162 } 163 } 164 165 // 忽略未启用的服务注入 166 func WithIgnoreInjectOfDisableService(ignore ...bool) Option { 167 return func(opt *option) { 168 opt.IgnoreInjectOfDisableService = len(ignore) == 0 || ignore[0] 169 } 170 } 171 172 // 启动服务(使用者不要主动调用这个函数, 应该由service包装, 因为service的选项无法通过这个函数传递) 173 func WithService(serviceType core.ServiceType, enable ...bool) Option { 174 return func(opt *option) { 175 if len(enable) == 0 || enable[0] { 176 opt.Services = append(opt.Services, serviceType) 177 } 178 } 179 } 180 181 // 自定义启用服务 182 // 183 // 如果要启用某个服务, 必须先注册该服务 184 func WithCustomEnableService(fn func(app core.IApp, services []core.ServiceType) []core.ServiceType) Option { 185 return func(opt *option) { 186 opt.CustomEnableServicesFn = fn 187 } 188 } 189 190 // 自定义组件 191 func WithCustomComponent(creator func(app core.IApp) core.IComponent) Option { 192 return func(opt *option) { 193 opt.CustomComponentFn = creator 194 } 195 }