micro.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package micro
  2. import (
  3. "context"
  4. "git.nspix.com/golang/micro/gateway/cli"
  5. "git.nspix.com/golang/micro/gateway/http"
  6. "git.nspix.com/golang/micro/gateway/rpc"
  7. "git.nspix.com/golang/micro/registry"
  8. )
  9. const (
  10. EnvironmentDocker string = "docker"
  11. EnvironmentHost = "host"
  12. )
  13. type (
  14. applicationKey struct {
  15. }
  16. HandleOptions struct {
  17. DisableRpc bool //禁用RPC功能
  18. DisableHttp bool //禁用HTTP功能
  19. DisableCli bool //禁用CLI功能
  20. HttpPath string //重定向HTTP路由
  21. HttpMethod string //HTTP路径
  22. }
  23. HandleOption func(o *HandleOptions)
  24. HandleFunc func(ctx Context) (err error)
  25. Application interface {
  26. Node() *registry.ServiceNode //获取节点信息
  27. HttpServe() *http.Server //获取HTTP实例
  28. RPCServe() *rpc.Server //获取RPC实例
  29. CliServe() *cli.Server //获取cli服务端
  30. PeekService(name string) ([]*registry.ServiceNode, error) //选择一个服务
  31. Handle(method string, cb HandleFunc, opts ...HandleOption) //注册一个处理器
  32. NewRequest(name, method string, body interface{}) (req *Request, err error) //创建一个rpc请求
  33. Environment() string
  34. }
  35. Server interface {
  36. Start(ctx context.Context) (err error)
  37. Stop() (err error)
  38. }
  39. )
  40. var (
  41. contextKey = applicationKey{}
  42. )
  43. func FromContext(ctx context.Context) Application {
  44. if v := ctx.Value(contextKey); v != nil {
  45. return v.(Application)
  46. }
  47. return nil
  48. }
  49. func WithContext(ctx context.Context, app Application) context.Context {
  50. return context.WithValue(ctx, contextKey, app)
  51. }