Skip to content

Commit

Permalink
feat: playground cli api (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: pshubham <[email protected]>
  • Loading branch information
Dev79844 and lucifercr07 authored Sep 28, 2024
1 parent 58a6980 commit 98b5742
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 5 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ module server

go 1.22.5

require github.com/dicedb/go-dice v0.0.0-20240820180649-d97f15fca831

require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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=
Expand Down
15 changes: 13 additions & 2 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ package api
import (
"encoding/json"
"net/http"

"server/internal/db"
helpers "server/pkg/util"
)

func JSONResponse(w http.ResponseWriter, r *http.Request, status int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

func RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("/health", HealthCheck)
mux.HandleFunc("/cli", cliHandler)
mux.HandleFunc("/cli/{cmd}", cliHandler)
mux.HandleFunc("/search", searchHandler)
}

Expand All @@ -24,7 +28,14 @@ func HealthCheck(w http.ResponseWriter, r *http.Request) {
}

func cliHandler(w http.ResponseWriter, r *http.Request) {
JSONResponse(w, r, http.StatusOK, map[string]string{"message": "cli handler"})
diceCmds, err := helpers.ParseHTTPRequest(r)
if err != nil {
http.Error(w, "Error parsing HTTP request", http.StatusBadRequest)
return
}

resp := db.ExecuteCommand(diceCmds)
JSONResponse(w, r, http.StatusOK, resp)
}

func searchHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
12 changes: 12 additions & 0 deletions internal/cmds/cmds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cmds

type CommandRequest struct {
Cmd string `json:"cmd"`
Args CommandRequestArgs
}

type CommandRequestArgs struct {
Key string `json:"key"`
Value string `json:"value,omitempty"`
Keys []string `json:"keys,omitempty"`
}
16 changes: 16 additions & 0 deletions internal/db/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package db

func getKey(key string) (string, error) {
val, err := rdb.Get(ctx, key).Result()
return val, err
}

func setKey(key, value string) error {
err := rdb.Set(ctx, key, value, 0).Err()
return err
}

func deleteKeys(keys []string) error {
err := rdb.Del(ctx, keys...).Err()
return err
}
59 changes: 59 additions & 0 deletions internal/db/dicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,62 @@ this will be the DiceDB client
*/

package db

import (
"context"
"log"
"server/internal/cmds"

dice "github.com/dicedb/go-dice"
)

var rdb *dice.Client
var ctx = context.Background()

func CloseDiceDB() {
err := rdb.Close()
if err != nil {
log.Fatalf("error closing DiceDB connection: %v", err)
}
}

func errorResponse(response string) map[string]string {
return map[string]string{"error": response}
}

func ExecuteCommand(command *cmds.CommandRequest) interface{} {
switch command.Cmd {
case "get":
if command.Args.Key == "" {
return errorResponse("key is required")
}
val, err := getKey(command.Args.Key)
if err != nil {
return errorResponse("error running get command")
}
return map[string]string{"value": val}

case "set":
if command.Args.Key == "" || command.Args.Value == "" {
return errorResponse("key and value are required")
}
err := setKey(command.Args.Key, command.Args.Value)
if err != nil {
return errorResponse("failed to set key")
}
return map[string]string{"result": "OK"}

case "del":
if len(command.Args.Keys) == 0 {
return map[string]string{"error": "atleast one key is required"}
}
err := deleteKeys(command.Args.Keys)
if err != nil {
return map[string]string{"error": "failed to delete keys"}
}

return map[string]string{"result": "OK"}
default:
return errorResponse("unknown command")
}
}
32 changes: 32 additions & 0 deletions pkg/util/helpers.go
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
package helpers

import (
"encoding/json"
"errors"
"io"
"log"
"net/http"
"server/internal/cmds"
)

func ParseHTTPRequest(r *http.Request) (*cmds.CommandRequest, error) {
command := r.PathValue("cmd")
if command == "" {
return nil, errors.New("invalid command")
}

body, err := io.ReadAll(r.Body)
if err != nil {
log.Fatalf("error reading body: %v", err)
}

var commandRequestArgs *cmds.CommandRequestArgs
err = json.Unmarshal(body, &commandRequestArgs)
if err != nil {
log.Fatalf("error unmarshalling body: %v", err)
}

return &cmds.CommandRequest{
Cmd: command,
Args: *commandRequestArgs,
}, nil
}

0 comments on commit 98b5742

Please sign in to comment.