options.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package micro
  2. import (
  3. "context"
  4. "git.nspix.com/golang/micro/registry"
  5. )
  6. type (
  7. Options struct {
  8. Zone string `json:"zone"`
  9. Name string `json:"name"`
  10. Version string `json:"version"`
  11. EnableHttp bool `json:"enable_http"`
  12. EnableRPC bool `json:"enable_rpc"`
  13. registry registry.Registry
  14. Server Server
  15. Context context.Context
  16. }
  17. OptionMiddleware func(o *Options)
  18. )
  19. func WithName(name string, version string) OptionMiddleware {
  20. return func(o *Options) {
  21. o.Name = name
  22. o.Version = version
  23. }
  24. }
  25. func WithRegistry(r registry.Registry) OptionMiddleware {
  26. return func(o *Options) {
  27. o.registry = r
  28. }
  29. }
  30. func WithContext(c context.Context) OptionMiddleware {
  31. return func(o *Options) {
  32. o.Context = c
  33. }
  34. }
  35. func WithServer(s Server) OptionMiddleware {
  36. return func(o *Options) {
  37. o.Server = s
  38. }
  39. }
  40. func WithoutHttp() OptionMiddleware {
  41. return func(o *Options) {
  42. o.EnableHttp = false
  43. }
  44. }
  45. func WithoutRPC() OptionMiddleware {
  46. return func(o *Options) {
  47. o.EnableRPC = false
  48. }
  49. }
  50. func NewOptions() *Options {
  51. return &Options{
  52. Zone: "default",
  53. Version: "1.0.1",
  54. EnableHttp: true,
  55. EnableRPC: true,
  56. Context: context.Background(),
  57. registry: registry.DefaultRegistry,
  58. }
  59. }