package micro import ( "context" "strings" "git.nspix.com/golang/micro/registry" "git.nspix.com/golang/micro/utils/metric" ) type ( ReadMetricFunc func() metric.Values Options struct { Zone string //注册域 Name string //名称 Version string //版本号 EnableHttp bool //启用HTTP功能 EnableRPC bool //启用RPC功能 EnableInternalListener bool //启用内置网络监听服务 DisableRegister bool //禁用注册 registry registry.Registry //注册仓库 Server Server //加载的服务 Port int //绑定端口 Address string //绑定地址 TSDBUrl string //TSDB数据上报URL MetricCallback ReadMetricFunc //读取系统数据的回调 EnableHttpPProf bool //启用HTTP调试工具 Context context.Context shortName string } Option func(o *Options) ) func (o *Options) ShortName() string { if o.shortName != "" { return o.shortName } if pos := strings.LastIndex(o.Name, "/"); pos != -1 { o.shortName = o.Name[pos+1:] } else { o.shortName = o.Name } return o.shortName } func WithName(name string, version string) Option { return func(o *Options) { o.Name = name o.Version = version } } func WithPort(port int) Option { return func(o *Options) { o.Port = port } } func WithHttpDebug() Option { return func(o *Options) { o.EnableHttpPProf = true } } func WithRegistry(r registry.Registry) Option { return func(o *Options) { o.registry = r } } func WithTSDBUrl(uri string) Option { return func(o *Options) { o.TSDBUrl = uri } } func WithMetricCallback(cb ReadMetricFunc) Option { return func(o *Options) { o.MetricCallback = cb } } func WithServer(s Server) Option { return func(o *Options) { o.Server = s } } func WithoutHttp() Option { return func(o *Options) { o.EnableHttp = false } } func WithoutRegister() Option { return func(o *Options) { o.DisableRegister = true } } func WithoutRPC() Option { return func(o *Options) { o.EnableRPC = false } } func NewOptions() *Options { return &Options{ Zone: "default", Version: "1.0.1", EnableHttp: true, EnableRPC: true, EnableInternalListener: true, Context: context.Background(), registry: registry.DefaultRegistry, } }