command_store.go 919 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package rest
  2. import "sync"
  3. func NewCommandStore() *CommandStore {
  4. return &CommandStore{
  5. commands: make(map[string]ICommand),
  6. locker: &sync.RWMutex{},
  7. }
  8. }
  9. type CommandStore struct {
  10. commands map[string]ICommand
  11. locker *sync.RWMutex
  12. }
  13. func (s *CommandStore) AddCommand(uri string, command ICommand) {
  14. s.locker.Lock()
  15. s.commands[uri] = command
  16. s.locker.Unlock()
  17. }
  18. func (s *CommandStore) AddCommandObjects(uri string, validator IValidator, executor IExecuter) {
  19. s.AddCommand(uri, NewCommand(validator, executor))
  20. }
  21. func (s *CommandStore) GetCommand(uri string) (ICommand, bool) {
  22. s.locker.RLock()
  23. command, check := s.commands[uri]
  24. s.locker.RUnlock()
  25. return command, check
  26. }
  27. func (s *CommandStore) AddCommandMethods(uri string, validate func(*Request) *Response, execute func(*Request) *Response) {
  28. s.AddCommand(
  29. uri,
  30. NewCommand(
  31. NewValidator(validate),
  32. NewExecuter(execute),
  33. ),
  34. )
  35. }