From 76464b3e7628f9408f6f2756b8271c58cc9fdb70 Mon Sep 17 00:00:00 2001 From: Yash Budhia Date: Sun, 6 Oct 2024 19:59:20 +0530 Subject: [PATCH] minors --- internal/server/http.go | 20 ++++++++------------ util/helpers.go | 30 +++++++++++++++--------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/internal/server/http.go b/internal/server/http.go index 191c83a..9dab2c5 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -4,15 +4,15 @@ import ( "context" "encoding/json" "errors" - "fmt" + "fmt" "log/slog" "net/http" "strings" "sync" "time" - "server/internal/middleware" "server/internal/db" + "server/internal/middleware" util "server/util" ) @@ -21,8 +21,6 @@ type HTTPServer struct { DiceClient *db.DiceDB } -// HandlerMux wraps ServeMux and forces REST paths to lowercase -// and attaches a rate limiter with the handler type HandlerMux struct { mux *http.ServeMux rateLimiter func(http.ResponseWriter, *http.Request, http.Handler) @@ -48,10 +46,8 @@ func errorResponse(response string) string { } func (cim *HandlerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { - // Convert the path to lowercase before passing to the underlying mux. middleware.TrailingSlashMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.URL.Path = strings.ToLower(r.URL.Path) - // Apply rate limiter cim.rateLimiter(w, r, cim.mux) })).ServeHTTP(w, r) } @@ -110,11 +106,11 @@ func (s *HTTPServer) CliHandler(w http.ResponseWriter, r *http.Request) { return } -// Check if the command is blocklisted -if err := util.BlockListedCommand(diceCmd.Cmd); err != nil { - http.Error(w, errorResponse(fmt.Sprintf("ERR unknown command '%s'", diceCmd.Cmd)), http.StatusForbidden) - return -} + // Check if the command is blocklisted + if err := util.BlockListedCommand(diceCmd.Cmd); err != nil { + http.Error(w, errorResponse(fmt.Sprintf("ERR unknown command '%s'", diceCmd.Cmd)), http.StatusForbidden) + return + } resp, err := s.DiceClient.ExecuteCommand(diceCmd) if err != nil { @@ -126,7 +122,7 @@ if err := util.BlockListedCommand(diceCmd.Cmd); err != nil { respStr, ok := resp.(string) if !ok { slog.Error("error: response is not a string", "error", slog.Any("err", err)) - http.Error(w, errorResponse("internal Server Error"), http.StatusInternalServerError) + http.Error(w, errorResponse("internal server error"), http.StatusInternalServerError) return } diff --git a/util/helpers.go b/util/helpers.go index 1ed8890..3e3aea8 100644 --- a/util/helpers.go +++ b/util/helpers.go @@ -16,24 +16,24 @@ import ( // Map of blocklisted commands var blocklistedCommands = map[string]bool{ - "FLUSHALL": true, - "FLUSHDB": true, - "DUMP": true, - "ABORT": true, + "FLUSHALL": true, + "FLUSHDB": true, + "DUMP": true, + "ABORT": true, "AUTH": true, - "CONFIG": true, - "SAVE": true, - "BGSAVE": true, - "BGREWRITEAOF": true, + "CONFIG": true, + "SAVE": true, + "BGSAVE": true, + "BGREWRITEAOF": true, "RESTORE": true, - "MULTI": true, - "EXEC": true, - "DISCARD": true, - "QWATCH": true, + "MULTI": true, + "EXEC": true, + "DISCARD": true, + "QWATCH": true, "QUNWATCH": true, - "LATENCY": true, - "CLIENT": true, - "SLEEP": true, + "LATENCY": true, + "CLIENT": true, + "SLEEP": true, "PERSIST": true, }