options.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package micro
  2. import (
  3. "context"
  4. "strings"
  5. "git.nspix.com/golang/micro/registry"
  6. )
  7. type (
  8. Options struct {
  9. Zone string //注册域
  10. Name string //名称
  11. Version string //版本号
  12. EnableHttp bool //启用HTTP功能
  13. EnableRPC bool //启用RPC功能
  14. EnableInternalListener bool //启用内置网络监听服务
  15. DisableRegister bool //禁用注册
  16. registry registry.Registry //注册仓库
  17. Server Server //加载的服务
  18. Port int //绑定端口
  19. Address string //绑定地址
  20. EnableHttpPProf bool //启用HTTP调试工具
  21. EnableStats bool //启用数据统计
  22. EnableLogPrefix bool //启用日志前缀
  23. EnableCli bool //启用cli模式
  24. EnableReport bool //启用数据上报
  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 WithStats() Option {
  53. return func(o *Options) {
  54. o.EnableStats = true
  55. }
  56. }
  57. func WithHttpDebug() Option {
  58. return func(o *Options) {
  59. o.EnableHttpPProf = true
  60. }
  61. }
  62. func WithCli() Option {
  63. return func(o *Options) {
  64. o.EnableCli = true
  65. }
  66. }
  67. func WithRegistry(r registry.Registry) Option {
  68. return func(o *Options) {
  69. o.registry = r
  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 WithoutLogPrefix() Option {
  88. return func(o *Options) {
  89. o.EnableLogPrefix = false
  90. }
  91. }
  92. func WithoutRPC() Option {
  93. return func(o *Options) {
  94. o.EnableRPC = false
  95. }
  96. }
  97. func NewOptions() *Options {
  98. opts := &Options{
  99. Zone: "default",
  100. Version: "1.0.1",
  101. EnableHttp: true,
  102. EnableRPC: true,
  103. EnableInternalListener: true,
  104. EnableLogPrefix: true,
  105. EnableReport: true,
  106. Context: context.Background(),
  107. registry: registry.DefaultRegistry,
  108. }
  109. return opts
  110. }