options.go 2.6 KB

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