options.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. RegistryArguments map[string]string //注册参数
  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 WithStats() Option {
  54. return func(o *Options) {
  55. o.EnableStats = true
  56. }
  57. }
  58. func WithHttpDebug() Option {
  59. return func(o *Options) {
  60. o.EnableHttpPProf = true
  61. }
  62. }
  63. func WithCli() Option {
  64. return func(o *Options) {
  65. o.EnableCli = true
  66. }
  67. }
  68. func WithRegistry(r registry.Registry) Option {
  69. return func(o *Options) {
  70. o.registry = r
  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 WithoutLogPrefix() Option {
  89. return func(o *Options) {
  90. o.EnableLogPrefix = false
  91. }
  92. }
  93. func WithoutRPC() Option {
  94. return func(o *Options) {
  95. o.EnableRPC = false
  96. }
  97. }
  98. func NewOptions() *Options {
  99. opts := &Options{
  100. Zone: "default",
  101. Version: "1.0.1",
  102. EnableHttp: true,
  103. EnableRPC: true,
  104. EnableInternalListener: true,
  105. EnableLogPrefix: true,
  106. EnableReport: true,
  107. Context: context.Background(),
  108. registry: registry.DefaultRegistry,
  109. }
  110. return opts
  111. }