micro.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package micro
  2. import (
  3. "context"
  4. )
  5. type (
  6. Context interface {
  7. Bind(i interface{}) (err error)
  8. Error(code int, msg string) (err error)
  9. Success(i interface{}) (err error)
  10. }
  11. HandleOptions struct {
  12. HttpMethod string
  13. DisableRpc bool
  14. DisableHttp bool
  15. }
  16. HandleOption func(o *HandleOptions)
  17. HandleFunc func(ctx Context) (err error)
  18. Application interface {
  19. Handle(method string, cb HandleFunc) (err error) //注册一个处理器
  20. NewRequest(service, method string, payload interface{}) (err error) //创建一个rpc请求
  21. }
  22. Server interface {
  23. Start(ctx context.Context) (err error)
  24. Stop() (err error)
  25. }
  26. Request struct {
  27. ServiceName string
  28. Method string
  29. Body interface{}
  30. client *Client
  31. }
  32. Response interface {
  33. StatusCode() int
  34. Error() error
  35. Decode(i interface{}) error
  36. }
  37. )
  38. func (r *Request) Do(ctx context.Context) (Response, error) {
  39. return r.client.Do(ctx, r)
  40. }
  41. func WithHttpMethod(method string) HandleOption {
  42. return func(o *HandleOptions) {
  43. o.HttpMethod = method
  44. }
  45. }