micro.go 967 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package micro
  2. import (
  3. "context"
  4. )
  5. type (
  6. applicationKey struct {
  7. }
  8. HandleOptions struct {
  9. DisableRpc bool //禁用RPC功能
  10. DisableHttp bool //禁用HTTP功能
  11. HttpPath string //重定向HTTP路由
  12. HttpMethod string //HTTP路径
  13. }
  14. HandleOption func(o *HandleOptions)
  15. HandleFunc func(ctx Context) (err error)
  16. Application interface {
  17. Handle(method string, cb HandleFunc, opts ...HandleOption) //注册一个处理器
  18. CreateRequest(name, method string, body interface{}) (req *Request, err error) //创建一个rpc请求
  19. }
  20. Server interface {
  21. Start(ctx context.Context) (err error)
  22. Stop() (err error)
  23. }
  24. )
  25. var (
  26. contextKey = applicationKey{}
  27. )
  28. func FromContext(ctx context.Context) Application {
  29. if v := ctx.Value(contextKey); v != nil {
  30. return v.(Application)
  31. }
  32. return nil
  33. }
  34. func WithContext(ctx context.Context, app Application) context.Context {
  35. return context.WithValue(ctx, contextKey, app)
  36. }