command.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package rest
  2. type IApplication interface {
  3. Addr() string
  4. Secret() []byte
  5. Executer(r IRequest) (IExecuter, bool)
  6. }
  7. ///////////////////////////////////////////////
  8. type IValidator interface {
  9. Validate(r IRequest) IResponse
  10. }
  11. type IExecuter interface {
  12. Execute(r IRequest) IResponse
  13. }
  14. type ICommand interface {
  15. IValidator
  16. IExecuter
  17. }
  18. ///////////////////////////////////////////////
  19. func NewCommand(validator IValidator, executer IExecuter) *Command {
  20. return &Command{
  21. validator,
  22. executer,
  23. }
  24. }
  25. type Command struct {
  26. validator IValidator
  27. executer IExecuter
  28. }
  29. func (s *Command) Validate(r IRequest) IResponse {
  30. return s.validator.Validate(r)
  31. }
  32. func (s *Command) Execute(r IRequest) IResponse {
  33. return s.executer.Execute(r)
  34. }
  35. ///////////////////////////////////////////////
  36. func NewValidator(method func(r IRequest) IResponse) *Validator {
  37. return &Validator{
  38. method,
  39. }
  40. }
  41. type Validator struct {
  42. method func(r IRequest) IResponse
  43. }
  44. func (s *Validator) Validate(r IRequest) IResponse {
  45. return s.method(r)
  46. }
  47. ///////////////////////////////////////////////
  48. func NewExecuter(method func(r IRequest) IResponse) *Executer {
  49. return &Executer{
  50. method,
  51. }
  52. }
  53. type Executer struct {
  54. method func(r IRequest) IResponse
  55. }
  56. func (s *Executer) Execute(r IRequest) IResponse {
  57. return s.method(r)
  58. }