micro.go 871 B

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