micro.go 1.4 KB

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