options.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package micro
  2. import (
  3. "context"
  4. "strings"
  5. "git.nspix.com/golang/micro/registry"
  6. "git.nspix.com/golang/micro/utils/metric"
  7. )
  8. type (
  9. ReadMetricFunc func() metric.Values
  10. Options struct {
  11. Zone string //注册域
  12. Name string //名称
  13. Version string //版本号
  14. EnableHttp bool //启用HTTP功能
  15. EnableRPC bool //启用RPC功能
  16. EnableInternalListener bool //启用内置网络监听服务
  17. DisableRegister bool //禁用注册
  18. registry registry.Registry //注册仓库
  19. Server Server //加载的服务
  20. Port int //绑定端口
  21. Address string //绑定地址
  22. TSDBUrl string //TSDB数据上报URL
  23. MetricCallback ReadMetricFunc //读取系统数据的回调
  24. Context context.Context
  25. shortName string
  26. }
  27. Option func(o *Options)
  28. )
  29. func (o *Options) ShortName() string {
  30. if o.shortName != "" {
  31. return o.shortName
  32. }
  33. if pos := strings.LastIndex(o.Name, "/"); pos != -1 {
  34. o.shortName = o.Name[pos+1:]
  35. } else {
  36. o.shortName = o.Name
  37. }
  38. return o.shortName
  39. }
  40. func WithName(name string, version string) Option {
  41. return func(o *Options) {
  42. o.Name = name
  43. o.Version = version
  44. }
  45. }
  46. func WithPort(port int) Option {
  47. return func(o *Options) {
  48. o.Port = port
  49. }
  50. }
  51. func WithRegistry(r registry.Registry) Option {
  52. return func(o *Options) {
  53. o.registry = r
  54. }
  55. }
  56. func WithTSDBUrl(uri string) Option {
  57. return func(o *Options) {
  58. o.TSDBUrl = uri
  59. }
  60. }
  61. func WithMetricCallback(cb ReadMetricFunc) Option {
  62. return func(o *Options) {
  63. o.MetricCallback = cb
  64. }
  65. }
  66. func WithServer(s Server) Option {
  67. return func(o *Options) {
  68. o.Server = s
  69. }
  70. }
  71. func WithoutHttp() Option {
  72. return func(o *Options) {
  73. o.EnableHttp = false
  74. }
  75. }
  76. func WithoutRegister() Option {
  77. return func(o *Options) {
  78. o.DisableRegister = true
  79. }
  80. }
  81. func WithoutRPC() Option {
  82. return func(o *Options) {
  83. o.EnableRPC = false
  84. }
  85. }
  86. func NewOptions() *Options {
  87. return &Options{
  88. Zone: "default",
  89. Version: "1.0.1",
  90. EnableHttp: true,
  91. EnableRPC: true,
  92. EnableInternalListener: true,
  93. Context: context.Background(),
  94. registry: registry.DefaultRegistry,
  95. }
  96. }