options.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package micro
  2. import (
  3. "context"
  4. "os"
  5. "strings"
  6. "syscall"
  7. "git.nspix.com/golang/micro/registry"
  8. )
  9. type (
  10. Options struct {
  11. Zone string //注册域
  12. Name string //名称
  13. Version string //版本号
  14. EnableHttp bool //启用HTTP功能
  15. EnableRPC bool //启用RPC功能
  16. EnableInternalListener bool //启用内置网络监听服务
  17. DisableRegister bool //禁用注册
  18. registry registry.Registry //注册仓库
  19. Server Server //加载的服务
  20. Port int //绑定端口
  21. Address string //绑定地址
  22. EnableHttpPProf bool //启用HTTP调试工具
  23. EnableStats bool //启用数据统计
  24. EnableLogPrefix bool //启用日志前缀
  25. EnableCli bool //启用cli模式
  26. EnableReport bool //启用数据上报
  27. RegistryArguments map[string]string //注册参数
  28. Signals []os.Signal
  29. Context context.Context
  30. shortName string
  31. }
  32. Option func(o *Options)
  33. )
  34. func (o *Options) ShortName() string {
  35. if o.shortName != "" {
  36. return o.shortName
  37. }
  38. if pos := strings.LastIndex(o.Name, "/"); pos != -1 {
  39. o.shortName = o.Name[pos+1:]
  40. } else {
  41. o.shortName = o.Name
  42. }
  43. return o.shortName
  44. }
  45. func WithName(name string, version string) Option {
  46. return func(o *Options) {
  47. o.Name = name
  48. o.Version = version
  49. }
  50. }
  51. func WithPort(port int) Option {
  52. return func(o *Options) {
  53. o.Port = port
  54. }
  55. }
  56. func WithStats() Option {
  57. return func(o *Options) {
  58. o.EnableStats = true
  59. }
  60. }
  61. func WithHttpDebug() Option {
  62. return func(o *Options) {
  63. o.EnableHttpPProf = true
  64. }
  65. }
  66. func WithCli() Option {
  67. return func(o *Options) {
  68. o.EnableCli = true
  69. }
  70. }
  71. func WithRegistry(r registry.Registry) Option {
  72. return func(o *Options) {
  73. o.registry = r
  74. }
  75. }
  76. func WithServer(s Server) Option {
  77. return func(o *Options) {
  78. o.Server = s
  79. }
  80. }
  81. func WithoutHttp() Option {
  82. return func(o *Options) {
  83. o.EnableHttp = false
  84. }
  85. }
  86. func WithoutRegister() Option {
  87. return func(o *Options) {
  88. o.DisableRegister = true
  89. }
  90. }
  91. func WithoutLogPrefix() Option {
  92. return func(o *Options) {
  93. o.EnableLogPrefix = false
  94. }
  95. }
  96. func WithoutRPC() Option {
  97. return func(o *Options) {
  98. o.EnableRPC = false
  99. }
  100. }
  101. func WithReport() Option {
  102. return func(o *Options) {
  103. o.EnableReport = true
  104. }
  105. }
  106. func NewOptions() *Options {
  107. opts := &Options{
  108. Zone: "default",
  109. Version: "1.0.1",
  110. EnableHttp: true,
  111. EnableRPC: true,
  112. EnableInternalListener: true,
  113. EnableLogPrefix: true,
  114. EnableReport: false,
  115. EnableHttpPProf: true,
  116. Context: context.Background(),
  117. registry: registry.DefaultRegistry,
  118. Signals: []os.Signal{syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL},
  119. }
  120. return opts
  121. }