options.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. EnableHttpPProf bool //启用HTTP调试工具
  25. Context context.Context
  26. shortName string
  27. }
  28. Option func(o *Options)
  29. )
  30. func (o *Options) ShortName() string {
  31. if o.shortName != "" {
  32. return o.shortName
  33. }
  34. if pos := strings.LastIndex(o.Name, "/"); pos != -1 {
  35. o.shortName = o.Name[pos+1:]
  36. } else {
  37. o.shortName = o.Name
  38. }
  39. return o.shortName
  40. }
  41. func WithName(name string, version string) Option {
  42. return func(o *Options) {
  43. o.Name = name
  44. o.Version = version
  45. }
  46. }
  47. func WithPort(port int) Option {
  48. return func(o *Options) {
  49. o.Port = port
  50. }
  51. }
  52. func WithHttpDebug() Option {
  53. return func(o *Options) {
  54. o.EnableHttpPProf = true
  55. }
  56. }
  57. func WithRegistry(r registry.Registry) Option {
  58. return func(o *Options) {
  59. o.registry = r
  60. }
  61. }
  62. func WithTSDBUrl(uri string) Option {
  63. return func(o *Options) {
  64. o.TSDBUrl = uri
  65. }
  66. }
  67. func WithMetricCallback(cb ReadMetricFunc) Option {
  68. return func(o *Options) {
  69. o.MetricCallback = cb
  70. }
  71. }
  72. func WithServer(s Server) Option {
  73. return func(o *Options) {
  74. o.Server = s
  75. }
  76. }
  77. func WithoutHttp() Option {
  78. return func(o *Options) {
  79. o.EnableHttp = false
  80. }
  81. }
  82. func WithoutRegister() Option {
  83. return func(o *Options) {
  84. o.DisableRegister = true
  85. }
  86. }
  87. func WithoutRPC() Option {
  88. return func(o *Options) {
  89. o.EnableRPC = false
  90. }
  91. }
  92. func NewOptions() *Options {
  93. return &Options{
  94. Zone: "default",
  95. Version: "1.0.1",
  96. EnableHttp: true,
  97. EnableRPC: true,
  98. EnableInternalListener: true,
  99. Context: context.Background(),
  100. registry: registry.DefaultRegistry,
  101. }
  102. }