-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: pshubham <[email protected]>
- Loading branch information
1 parent
58a6980
commit 98b5742
Showing
7 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |