options.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. 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 WithStats() Option {
  52. return func(o *Options) {
  53. o.EnableStats = true
  54. }
  55. }
  56. func WithHttpDebug() Option {
  57. return func(o *Options) {
  58. o.EnableHttpPProf = true
  59. }
  60. }
  61. func WithCli() Option {
  62. return func(o *Options) {
  63. o.EnableCli = true
  64. }
  65. }
  66. func WithRegistry(r registry.Registry) Option {
  67. return func(o *Options) {
  68. o.registry = r
  69. }
  70. }
  71. func WithServer(s Server) Option {
  72. return func(o *Options) {
  73. o.Server = s
  74. }
  75. }
  76. func WithoutHttp() Option {
  77. return func(o *Options) {
  78. o.EnableHttp = false
  79. }
  80. }
  81. func WithoutRegister() Option {
  82. return func(o *Options) {
  83. o.DisableRegister = true
  84. }
  85. }
  86. func WithoutLogPrefix() Option {
  87. return func(o *Options) {
  88. o.EnableLogPrefix = false
  89. }
  90. }
  91. func WithoutRPC() Option {
  92. return func(o *Options) {
  93. o.EnableRPC = false
  94. }
  95. }
  96. func NewOptions() *Options {
  97. opts := &Options{
  98. Zone: "default",
  99. Version: "1.0.1",
  100. EnableHttp: true,
  101. EnableRPC: true,
  102. EnableInternalListener: true,
  103. EnableLogPrefix: true,
  104. Context: context.Background(),
  105. registry: registry.DefaultRegistry,
  106. }
  107. return opts
  108. }