Skip to content

Commit

Permalink
feat: allow commands to register themselves to the request handler
Browse files Browse the repository at this point in the history
  • Loading branch information
PapePathe committed Apr 13, 2024
1 parent 0a74190 commit 4cd11fd
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 25 deletions.
43 changes: 18 additions & 25 deletions internal/commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@ package commands
import (
"fmt"
"strings"
"sync"
)

var (
defaultCommands map[string]CommandHandler
defaultCommandsLock sync.Mutex
)

func RegisterCommand(cmd string, h CommandHandler) {
defaultCommandsLock.Lock()
defer defaultCommandsLock.Unlock()

if defaultCommands == nil {
defaultCommands = make(map[string]CommandHandler)
}

defaultCommands[cmd] = h
}

type RequestHandler struct {
subcommands map[string]CommandHandler
}
Expand All @@ -14,31 +31,7 @@ func NewRequestHandler() *RequestHandler {
}

func DefaultRequestHandler() *RequestHandler {
subcommands := map[string]CommandHandler{
// cluster commands
"cluster": ClusterHandler{},

// acl commands
"acl": AclHandler{},

"del": DelHandler{},
"get": GetHandler{},
"set": SetHandler{},

// hash related commands
"hexists": HExistsHandler{},
"hget": HGetHandler{},
"hkeys": HKeysHandler{},
"hlen": HLenHandler{},
"hset": HSetHandler{},
"hvals": HValsHandler{},

"config": ConfigHandler{},
"hello": HelloHandler{},
"auth": AuthHandler{},
"ping": PingHandler{},
}
return &RequestHandler{subcommands}
return &RequestHandler{defaultCommands}
}

func (s RequestHandler) Run(request ClientRequest) {
Expand Down
4 changes: 4 additions & 0 deletions internal/commands/handler_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ func (aclService) users(r ClientRequest) error {

return nil
}

func init() {
RegisterCommand("acl", AclHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ func (ch AuthHandler) Handle(r ClientRequest) {

r.WriteOK()
}

func init() {
RegisterCommand("auth", AuthHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ func (ch ClusterHandler) Handle(r ClientRequest) {
}

}

func init() {
RegisterCommand("cluster", ClusterHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ func (ch ConfigHandler) Persistent() bool {
func (ch ConfigHandler) Handle(r ClientRequest) {
r.WriteError("not yet implemented")
}

func init() {
RegisterCommand("config", ConfigHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_del.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ func (ch DelHandler) Handle(r ClientRequest) {

r.WriteNumber(fmt.Sprintf("%d", delCount))
}

func init() {
RegisterCommand("del", DelHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ func (ch GetHandler) Handle(r ClientRequest) {
rr := renderer.BulkStringRenderer{}
r.Write(rr.Render(val))
}

func init() {
RegisterCommand("get", GetHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ func (ch HelloHandler) Handle(r ClientRequest) {
log.Println(err)
}
}

func init() {
RegisterCommand("hello", HelloHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_hexists.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ func (ch HExistsHandler) Handle(r ClientRequest) {

_ = r.WriteNumber("1")
}

func init() {
RegisterCommand("hexists", HExistsHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_hget.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ func (ch HGetHandler) Handle(r ClientRequest) {

_ = r.WriteString(value)
}

func init() {
RegisterCommand("hget", HGetHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_hkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ func (ch HKeysHandler) Handle(r ClientRequest) {

_ = r.WriteArray(hs.Keys())
}

func init() {
RegisterCommand("hkeys", HKeysHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_hlen.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ func (ch HLenHandler) Handle(r ClientRequest) {

_ = r.WriteNumber(fmt.Sprintf("%d", hs.Len()))
}

func init() {
RegisterCommand("hlen", HLenHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_hset.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@ func chunkSlice(slice [][]byte, chunkSize int) hset {

return chunks
}

func init() {
RegisterCommand("hset", HSetHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_hvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ func (ch HValsHandler) Handle(r ClientRequest) {

_ = r.WriteArray(hs.Values())
}

func init() {
RegisterCommand("hvals", HValsHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ func (ch PingHandler) Handle(r ClientRequest) {

r.WriteString(data[0])
}

func init() {
RegisterCommand("ping", PingHandler{})
}
4 changes: 4 additions & 0 deletions internal/commands/handler_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ func (ch SetHandler) Handle(r ClientRequest) {

_, _ = r.Write([]byte("+OK\r\n"))
}

func init() {
RegisterCommand("set", SetHandler{})
}

0 comments on commit 4cd11fd

Please sign in to comment.