From 4cd11fdd8990963731998613832e621e97303f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Papa=20Path=C3=A9=20SENE?= Date: Sat, 13 Apr 2024 09:17:12 +0000 Subject: [PATCH] feat: allow commands to register themselves to the request handler --- internal/commands/command.go | 43 ++++++++++++---------------- internal/commands/handler_acl.go | 4 +++ internal/commands/handler_auth.go | 4 +++ internal/commands/handler_cluster.go | 4 +++ internal/commands/handler_config.go | 4 +++ internal/commands/handler_del.go | 4 +++ internal/commands/handler_get.go | 4 +++ internal/commands/handler_hello.go | 4 +++ internal/commands/handler_hexists.go | 4 +++ internal/commands/handler_hget.go | 4 +++ internal/commands/handler_hkeys.go | 4 +++ internal/commands/handler_hlen.go | 4 +++ internal/commands/handler_hset.go | 4 +++ internal/commands/handler_hvals.go | 4 +++ internal/commands/handler_ping.go | 4 +++ internal/commands/handler_set.go | 4 +++ 16 files changed, 78 insertions(+), 25 deletions(-) diff --git a/internal/commands/command.go b/internal/commands/command.go index e450597..11c5e33 100644 --- a/internal/commands/command.go +++ b/internal/commands/command.go @@ -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 } @@ -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) { diff --git a/internal/commands/handler_acl.go b/internal/commands/handler_acl.go index a2f7e13..74348ad 100644 --- a/internal/commands/handler_acl.go +++ b/internal/commands/handler_acl.go @@ -103,3 +103,7 @@ func (aclService) users(r ClientRequest) error { return nil } + +func init() { + RegisterCommand("acl", AclHandler{}) +} diff --git a/internal/commands/handler_auth.go b/internal/commands/handler_auth.go index 7a0997b..18670d3 100644 --- a/internal/commands/handler_auth.go +++ b/internal/commands/handler_auth.go @@ -41,3 +41,7 @@ func (ch AuthHandler) Handle(r ClientRequest) { r.WriteOK() } + +func init() { + RegisterCommand("auth", AuthHandler{}) +} diff --git a/internal/commands/handler_cluster.go b/internal/commands/handler_cluster.go index 6d1e4af..94eadcc 100644 --- a/internal/commands/handler_cluster.go +++ b/internal/commands/handler_cluster.go @@ -58,3 +58,7 @@ func (ch ClusterHandler) Handle(r ClientRequest) { } } + +func init() { + RegisterCommand("cluster", ClusterHandler{}) +} diff --git a/internal/commands/handler_config.go b/internal/commands/handler_config.go index ed96ba0..42fce14 100644 --- a/internal/commands/handler_config.go +++ b/internal/commands/handler_config.go @@ -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{}) +} diff --git a/internal/commands/handler_del.go b/internal/commands/handler_del.go index 77d3142..cc52d61 100644 --- a/internal/commands/handler_del.go +++ b/internal/commands/handler_del.go @@ -36,3 +36,7 @@ func (ch DelHandler) Handle(r ClientRequest) { r.WriteNumber(fmt.Sprintf("%d", delCount)) } + +func init() { + RegisterCommand("del", DelHandler{}) +} diff --git a/internal/commands/handler_get.go b/internal/commands/handler_get.go index 9231448..b4795ea 100644 --- a/internal/commands/handler_get.go +++ b/internal/commands/handler_get.go @@ -27,3 +27,7 @@ func (ch GetHandler) Handle(r ClientRequest) { rr := renderer.BulkStringRenderer{} r.Write(rr.Render(val)) } + +func init() { + RegisterCommand("get", GetHandler{}) +} diff --git a/internal/commands/handler_hello.go b/internal/commands/handler_hello.go index d7c6e58..a6c1633 100644 --- a/internal/commands/handler_hello.go +++ b/internal/commands/handler_hello.go @@ -34,3 +34,7 @@ func (ch HelloHandler) Handle(r ClientRequest) { log.Println(err) } } + +func init() { + RegisterCommand("hello", HelloHandler{}) +} diff --git a/internal/commands/handler_hexists.go b/internal/commands/handler_hexists.go index fa096ad..6f79004 100644 --- a/internal/commands/handler_hexists.go +++ b/internal/commands/handler_hexists.go @@ -34,3 +34,7 @@ func (ch HExistsHandler) Handle(r ClientRequest) { _ = r.WriteNumber("1") } + +func init() { + RegisterCommand("hexists", HExistsHandler{}) +} diff --git a/internal/commands/handler_hget.go b/internal/commands/handler_hget.go index 36beeee..b26d6aa 100644 --- a/internal/commands/handler_hget.go +++ b/internal/commands/handler_hget.go @@ -37,3 +37,7 @@ func (ch HGetHandler) Handle(r ClientRequest) { _ = r.WriteString(value) } + +func init() { + RegisterCommand("hget", HGetHandler{}) +} diff --git a/internal/commands/handler_hkeys.go b/internal/commands/handler_hkeys.go index 6646ee4..1620b5e 100644 --- a/internal/commands/handler_hkeys.go +++ b/internal/commands/handler_hkeys.go @@ -29,3 +29,7 @@ func (ch HKeysHandler) Handle(r ClientRequest) { _ = r.WriteArray(hs.Keys()) } + +func init() { + RegisterCommand("hkeys", HKeysHandler{}) +} diff --git a/internal/commands/handler_hlen.go b/internal/commands/handler_hlen.go index 42817ce..226db71 100644 --- a/internal/commands/handler_hlen.go +++ b/internal/commands/handler_hlen.go @@ -33,3 +33,7 @@ func (ch HLenHandler) Handle(r ClientRequest) { _ = r.WriteNumber(fmt.Sprintf("%d", hs.Len())) } + +func init() { + RegisterCommand("hlen", HLenHandler{}) +} diff --git a/internal/commands/handler_hset.go b/internal/commands/handler_hset.go index 5879685..e7485b4 100644 --- a/internal/commands/handler_hset.go +++ b/internal/commands/handler_hset.go @@ -140,3 +140,7 @@ func chunkSlice(slice [][]byte, chunkSize int) hset { return chunks } + +func init() { + RegisterCommand("hset", HSetHandler{}) +} diff --git a/internal/commands/handler_hvals.go b/internal/commands/handler_hvals.go index d38589c..e8ea55f 100644 --- a/internal/commands/handler_hvals.go +++ b/internal/commands/handler_hvals.go @@ -29,3 +29,7 @@ func (ch HValsHandler) Handle(r ClientRequest) { _ = r.WriteArray(hs.Values()) } + +func init() { + RegisterCommand("hvals", HValsHandler{}) +} diff --git a/internal/commands/handler_ping.go b/internal/commands/handler_ping.go index 03e973c..62f69a3 100644 --- a/internal/commands/handler_ping.go +++ b/internal/commands/handler_ping.go @@ -25,3 +25,7 @@ func (ch PingHandler) Handle(r ClientRequest) { r.WriteString(data[0]) } + +func init() { + RegisterCommand("ping", PingHandler{}) +} diff --git a/internal/commands/handler_set.go b/internal/commands/handler_set.go index 528d7e6..f5e856c 100644 --- a/internal/commands/handler_set.go +++ b/internal/commands/handler_set.go @@ -50,3 +50,7 @@ func (ch SetHandler) Handle(r ClientRequest) { _, _ = r.Write([]byte("+OK\r\n")) } + +func init() { + RegisterCommand("set", SetHandler{}) +}