options.go 2.3 KB

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