diff --git a/go.mod b/go.mod index 26906be..c963f7f 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,10 @@ -module server +module github.com/DiceDB/playground-mono -go 1.22.5 +go 1.23.0 + +require github.com/dicedb/go-dice v0.0.0-20240929141018-7ef25848d647 require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect - github.com/dicedb/go-dice v0.0.0-20240820180649-d97f15fca831 // indirect ) diff --git a/go.sum b/go.sum index af5b6c5..0b6badb 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,10 @@ +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= -github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= -github.com/dicedb/go-dice v0.0.0-20240820180649-d97f15fca831 h1:Cqyj9WCtoobN6++bFbDSe27q94SPwJD9Z0wmu+SDRuk= -github.com/dicedb/go-dice v0.0.0-20240820180649-d97f15fca831/go.mod h1:8+VZrr14c2LW8fW4tWZ8Bv3P2lfvlg+PpsSn5cWWuiQ= +github.com/dicedb/go-dice v0.0.0-20240929141018-7ef25848d647 h1:tJZD5LdR6GoBZZtBn0bD54xChnnXbedXGjOlCthTw0o= +github.com/dicedb/go-dice v0.0.0-20240929141018-7ef25848d647/go.mod h1:8+VZrr14c2LW8fW4tWZ8Bv3P2lfvlg+PpsSn5cWWuiQ= diff --git a/internal/cmds/cmds.go b/internal/cmds/cmds.go index bb7a275..e139777 100644 --- a/internal/cmds/cmds.go +++ b/internal/cmds/cmds.go @@ -4,3 +4,9 @@ type CommandRequest struct { Cmd string `json:"cmd"` Args []string } + +type CommandRequestArgs struct { + Key string `json:"key"` + Value string `json:"value,omitempty"` + Keys []string `json:"keys,omitempty"` +} diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index e9746f0..a6d7d84 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -2,8 +2,9 @@ package handlers import ( "net/http" - "server/internal/service" - "server/pkg/common" + + "github.com/DiceDB/playground-mono/internal/service" + "github.com/DiceDB/playground-mono/pkg/common" ) const ( @@ -30,28 +31,35 @@ func (h *handler) CLIHandler(w http.ResponseWriter, r *http.Request) { command, err := common.ParseHTTPRequest(r) if err != nil { common.JSONResponse(w, http.StatusBadRequest, map[string]string{"error": err.Error()}) + return } var result any switch command.Cmd { - case OpGET: - result, err = h.service.Get(r.Context(), &service.GetRequest{Key: command.Args.Key}) + if len(command.Args) < 1 { + common.JSONResponse(w, http.StatusBadRequest, map[string]string{"error": "key is required for GET operation"}) + return + } + result, err = h.service.Get(r.Context(), &service.GetRequest{Key: command.Args[0]}) case OpSET: - result, err = h.service.Set(r.Context(), &service.SetRequest{Key: command.Args.Key, Value: command.Args.Value}) + if len(command.Args) < 2 { + common.JSONResponse(w, http.StatusBadRequest, map[string]string{"error": "key and value are required for SET operation"}) + return + } + result, err = h.service.Set(r.Context(), &service.SetRequest{Key: command.Args[0], Value: command.Args[1]}) case OpDEL: - var keys []string - if command.Args.Key != "" { - keys = []string{command.Args.Key} - } else if len(command.Args.Keys) > 0 { - keys = command.Args.Keys - } else { - common.JSONResponse(w, http.StatusBadRequest, map[string]string{"error": "at least one key is required"}) + if len(command.Args) < 1 { + common.JSONResponse(w, http.StatusBadRequest, map[string]string{"error": "at least one key is required for DELETE operation"}) return } - result, err = h.service.Delete(r.Context(), &service.DeleteRequest{Keys: keys}) + result, err = h.service.Delete(r.Context(), &service.DeleteRequest{Keys: command.Args}) + + default: + common.JSONResponse(w, http.StatusBadRequest, map[string]string{"error": "unknown command"}) + return } if err != nil { diff --git a/internal/server/http.go b/internal/server/http.go index b1d04d3..d06cdff 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -11,12 +11,11 @@ import ( "syscall" "time" - "server/config" - "server/internal/handlers" - "server/internal/middleware" - "server/internal/repository" - "server/internal/service" - + "github.com/DiceDB/playground-mono/config" + "github.com/DiceDB/playground-mono/internal/handlers" + "github.com/DiceDB/playground-mono/internal/middleware" + "github.com/DiceDB/playground-mono/internal/repository" + "github.com/DiceDB/playground-mono/internal/service" dice "github.com/dicedb/go-dice" ) diff --git a/internal/service/cli.go b/internal/service/cli.go index 66bae7d..a9fa06b 100644 --- a/internal/service/cli.go +++ b/internal/service/cli.go @@ -3,7 +3,8 @@ package service import ( "context" "errors" - "server/internal/repository" + + "github.com/DiceDB/playground-mono/internal/repository" ) func (s *service) Get(ctx context.Context, req *GetRequest) (*GetResponse, error) { diff --git a/internal/service/types.go b/internal/service/types.go index e6b28e6..f23626c 100644 --- a/internal/service/types.go +++ b/internal/service/types.go @@ -2,7 +2,8 @@ package service import ( "context" - "server/internal/repository" + + "github.com/DiceDB/playground-mono/internal/repository" ) // Service is service layer client interface that should have all the methods diff --git a/main.go b/main.go index bf7c752..ce4bb41 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,9 @@ package main import ( "log" - "server/config" - "server/internal/server" + + "github.com/DiceDB/playground-mono/config" + "github.com/DiceDB/playground-mono/internal/server" ) func main() { diff --git a/pkg/common/common.go b/pkg/common/common.go index ce826f8..d802210 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -1,4 +1,4 @@ -package helpers +package common import ( "encoding/json" @@ -6,8 +6,9 @@ import ( "fmt" "io" "net/http" - "server/internal/cmds" "strings" + + "github.com/DiceDB/playground-mono/internal/cmds" ) const (