global.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 Node() *registry.ServiceNode {
  17. if std == nil{
  18. panic("init first")
  19. }
  20. return std.Node()
  21. }
  22. func HttpServe() *http.Server {
  23. if std == nil{
  24. panic("init first")
  25. }
  26. return std.HttpServe()
  27. }
  28. func RPCServe() *rpc.Server {
  29. if std == nil{
  30. panic("init first")
  31. }
  32. return std.RPCServe()
  33. }
  34. func CliServe() *cli.Server {
  35. if std == nil{
  36. panic("init first")
  37. }
  38. return std.CliServe()
  39. }
  40. func PeekService(name string) ([]*registry.ServiceNode, error) {
  41. if std == nil{
  42. panic("init first")
  43. }
  44. return std.PeekService(name)
  45. }
  46. func Handle(method string, cb HandleFunc, opts ...HandleOption) {
  47. if std == nil{
  48. panic("init first")
  49. }
  50. std.Handle(method, cb, opts...)
  51. }
  52. func NewRequest(name, method string, body interface{}) (req *Request, err error) {
  53. if std == nil{
  54. panic("init first")
  55. }
  56. return std.NewRequest(name, method, body)
  57. }
  58. func DeferTick(duration time.Duration, callback HandleTickerFunc, opts ...TickOption) int64 {
  59. if std == nil{
  60. panic("init first")
  61. }
  62. return std.DeferTick(duration, callback, opts...)
  63. }
  64. func Environment() string {
  65. if std == nil{
  66. panic("init first")
  67. }
  68. return std.Environment()
  69. }