global.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package micro
  2. import (
  3. "git.nspix.com/golang/micro/gateway/cli"
  4. "git.nspix.com/golang/micro/gateway/http"
  5. "git.nspix.com/golang/micro/gateway/rpc"
  6. "git.nspix.com/golang/micro/registry"
  7. "time"
  8. )
  9. var (
  10. std *Service
  11. )
  12. func Init(opts ...Option) *Service {
  13. std = New(opts...)
  14. return std
  15. }
  16. func Name() string {
  17. if std == nil {
  18. panic("init first")
  19. }
  20. return std.opts.Name
  21. }
  22. func Version() string {
  23. if std == nil {
  24. panic("init first")
  25. }
  26. return std.opts.Version
  27. }
  28. func Node() *registry.ServiceNode {
  29. if std == nil {
  30. panic("init first")
  31. }
  32. return std.Node()
  33. }
  34. func HttpServe() *http.Server {
  35. if std == nil {
  36. panic("init first")
  37. }
  38. return std.HttpServe()
  39. }
  40. func RPCServe() *rpc.Server {
  41. if std == nil {
  42. panic("init first")
  43. }
  44. return std.RPCServe()
  45. }
  46. func CliServe() *cli.Server {
  47. if std == nil {
  48. panic("init first")
  49. }
  50. return std.CliServe()
  51. }
  52. func PeekService(name string) ([]*registry.ServiceNode, error) {
  53. if std == nil {
  54. panic("init first")
  55. }
  56. return std.PeekService(name)
  57. }
  58. func Handle(method string, cb HandleFunc, opts ...HandleOption) {
  59. if std == nil {
  60. panic("init first")
  61. }
  62. std.Handle(method, cb, opts...)
  63. }
  64. func NewRequest(name, method string, body interface{}) (req *Request, err error) {
  65. if std == nil {
  66. panic("init first")
  67. }
  68. return std.NewRequest(name, method, body)
  69. }
  70. func DeferTick(duration time.Duration, callback HandleTickerFunc, opts ...TickOption) int64 {
  71. if std == nil {
  72. panic("init first")
  73. }
  74. return std.DeferTick(duration, callback, opts...)
  75. }
  76. func Environment() string {
  77. if std == nil {
  78. panic("init first")
  79. }
  80. return std.Environment()
  81. }