options.go 2.5 KB

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