123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package rest
- import "sync"
- func NewCommandStore() *CommandStore {
- return &CommandStore{
- commands: make(map[string]ICommand),
- locker: &sync.RWMutex{},
- }
- }
- type CommandStore struct {
- commands map[string]ICommand
- locker *sync.RWMutex
- }
- func (s *CommandStore) AddCommand(uri string, command ICommand) {
- s.locker.Lock()
- s.commands[uri] = command
- s.locker.Unlock()
- }
- func (s *CommandStore) AddCommandObjects(uri string, validator IValidator, executor IExecuter) {
- s.AddCommand(uri, NewCommand(validator, executor))
- }
- func (s *CommandStore) GetCommand(uri string) (ICommand, bool) {
- s.locker.RLock()
- command, check := s.commands[uri]
- s.locker.RUnlock()
- return command, check
- }
- func (s *CommandStore) AddCommandMethods(uri string, validate func(*Request) *Response, execute func(*Request) *Response) {
- s.AddCommand(
- uri,
- NewCommand(
- NewValidator(validate),
- NewExecuter(execute),
- ),
- )
- }
|