Skip to content

Commit

Permalink
fix files
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Singh <[email protected]>
  • Loading branch information
theBeginner86 committed Aug 18, 2024
1 parent d6dcca9 commit 172ca85
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 77 deletions.
51 changes: 26 additions & 25 deletions internal/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package handlers
import (
"strings"

"github.com/thebeginner86/hippocampus/internal/handlers/ping"
"github.com/thebeginner86/hippocampus/internal/handlers/store/nested"
"github.com/thebeginner86/hippocampus/internal/handlers/store/std"
"github.com/thebeginner86/hippocampus/internal/handlers/ping"
"github.com/thebeginner86/hippocampus/internal/security"
"github.com/thebeginner86/hippocampus/persistance/aof"
"github.com/thebeginner86/hippocampus/resp"
Expand All @@ -28,30 +28,30 @@ import (
type Cmds string

const (
SET Cmds = "SET"
GET Cmds = "GET"
GETALL Cmds = "GETALL"
HSET Cmds = "HSET"
HGET Cmds = "HGET"
SET Cmds = "SET"
GET Cmds = "GET"
GETALL Cmds = "GETALL"
HSET Cmds = "HSET"
HGET Cmds = "HGET"
HGETALL Cmds = "HGETALL"
CONFIG Cmds = "CONFIG"
PING Cmds = "PING"
CONFIG Cmds = "CONFIG"
PING Cmds = "PING"
)

type IStoreHandler interface {
Handle(req *resp.Value, skip bool) *resp.Value // TODO: skp should be removed
preProcess(req *resp.Value) *resp.Value
preProcess(req *resp.Value) *resp.Value
run(args []resp.Value) *resp.Value
postProcess(req *resp.Value) *resp.Value
}

type CmdsHandler struct {
SetCmdH *std.SetCmd
GetCmdH *std.GetCmd
SetCmdH *std.SetCmd
GetCmdH *std.GetCmd
GetAllCmdH *std.GetAllCmd

HSetCmdH *nested.HSetCmd
HGetCmdH *nested.HGetCmd
HSetCmdH *nested.HSetCmd
HGetCmdH *nested.HGetCmd
HGetAllCmdH *nested.HGetAllCmd

PingCmdH *ping.PingCmd
Expand All @@ -61,42 +61,43 @@ func newCmdHandler(secH *security.Security, aofH *aof.Aof) *CmdsHandler {
stdH := std.NewStdStoreHandler(secH, aofH)
nestedH := nested.NewNestedStoreHandler(secH, aofH)
return &CmdsHandler{
SetCmdH: std.NewSetCmd(stdH),
GetCmdH: std.NewGetCmd(stdH),
GetAllCmdH: std.NewGetAllCmd(stdH),
HGetCmdH: nested.NewHGetCmd(nestedH),
HSetCmdH: nested.NewHSetCmd(nestedH),
SetCmdH: std.NewSetCmd(stdH),
GetCmdH: std.NewGetCmd(stdH),
GetAllCmdH: std.NewGetAllCmd(stdH),
HGetCmdH: nested.NewHGetCmd(nestedH),
HSetCmdH: nested.NewHSetCmd(nestedH),
HGetAllCmdH: nested.NewHGetAllCmd(nestedH),
PingCmdH: ping.NewPingCmd(),
PingCmdH: ping.NewPingCmd(),
}
}

type Handler struct {
CmdHander *CmdsHandler
AofH *aof.Aof
AofH *aof.Aof
}

var byts = []byte{35, 46, 57, 24, 85, 35, 24, 74, 87, 35, 88, 98, 66, 32, 14, 05}

const privateKey string = "abc&1*~#^2^#s0^=)^^7%b34"

func NewHandler(aofFile string) (*Handler, error) {
aofH, err := aof.NewAof(aofFile)
aofH, err := aof.NewAof(aofFile)
if err != nil {
return nil, err
}
}
secH := security.NewSecurity(privateKey, byts)

return &Handler{
CmdHander: newCmdHandler(secH, aofH),
AofH: aofH,
AofH: aofH,
}, nil
}

func (handler *Handler) ExecuteCmd(req *resp.Value, skp bool) *resp.Value {
func (handler *Handler) ExecuteCmd(req *resp.Value, skp bool) *resp.Value {
// fetches the first element of the array and convert to uppercase
// this ensures that the cmds matches properly with our defined standards
cmd := strings.ToUpper(req.Array[0].Bulk)
req.Array[0].Bulk = cmd // TODO: please, fix this oddity
req.Array[0].Bulk = cmd // TODO: please, fix this oddity

switch cmd {
case string(SET):
Expand Down
4 changes: 1 addition & 3 deletions internal/handlers/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package handlers
// }
// defer aofH.Close()



// // set := []resp.Value{
// // {Type: "bulk", Bulk: "key"},
// // {Type: "bulk", Bulk: "value"},
Expand All @@ -44,4 +42,4 @@ package handlers
// handler(args)
// })
// }
// }
// }
4 changes: 2 additions & 2 deletions internal/handlers/store/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ package config
// type ConfigSubCmd string

// var (
// GetCmd ConfigSubCmd = "get"
// GetCmd ConfigSubCmd = "get"
// SETCmd ConfigSubCmd = "set"
// )

Expand All @@ -53,4 +53,4 @@ package config
// return setConfig(args)
// }
// return resp.Value{Type: "error", String: "Error: Invalid subcommand for 'config' command. Only 'get' or 'set'are supported"}
// }
// }
2 changes: 1 addition & 1 deletion internal/handlers/store/config/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ package config
// }
// return resp.Value{Type: "array", Array: vals}

// }
// }
8 changes: 3 additions & 5 deletions internal/handlers/store/nested/hset.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ type HSetCmd struct {

func NewHSetCmd(handler *NestedStoreHandler) *HSetCmd {
return &HSetCmd{
Name: "HSET",
Name: "HSET",
NestedStoreHandler: handler,
}
}


func (handler *HSetCmd) Handle(req *resp.Value, skp bool) (*resp.Value) {
func (handler *HSetCmd) Handle(req *resp.Value, skp bool) *resp.Value {
if skp {
return handler.run(req.Array[1:])
}
Expand Down Expand Up @@ -74,7 +73,7 @@ func (handler *HSetCmd) run(args []resp.Value) *resp.Value {
hash := args[0].Bulk
key := args[1].Bulk
value := args[2].Bulk

handler.mu.Lock()
if _, ok := handler.store[hash]; !ok {
handler.store[hash] = make(map[string]string)
Expand All @@ -93,4 +92,3 @@ func (handler *HSetCmd) postProcess(req *resp.Value) *resp.Value {

return nil
}

8 changes: 4 additions & 4 deletions internal/handlers/store/nested/nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ type NestedStoreHandler struct {
mu sync.RWMutex

securityH *security.Security
aofH *aof.Aof
aofH *aof.Aof
}

func NewNestedStoreHandler(secH *security.Security, aofH *aof.Aof) *NestedStoreHandler {
return &NestedStoreHandler{
store: map[string]map[string]string{},
mu: sync.RWMutex{},
store: map[string]map[string]string{},
mu: sync.RWMutex{},
securityH: secH,
aofH: aofH,
aofH: aofH,
}
}
6 changes: 3 additions & 3 deletions internal/handlers/store/std/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type GetCmd struct {

func NewGetCmd(handler *StdStoreHandler) *GetCmd {
return &GetCmd{
Name: "GET",
Name: "GET",
StdStoreHandler: handler,
}
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func (handler *GetCmd) postProcess(req *resp.Value) *resp.Value {
value, err := handler.securityH.Decrypter.Decrypt(req.Bulk)
if err != nil {
return &resp.Value{Type: "error", String: "Error: " + err.Error()}
}
}

return &resp.Value{Type: "bulk", Bulk: value}
}
}
9 changes: 4 additions & 5 deletions internal/handlers/store/std/get_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ type GetAllCmd struct {
*StdStoreHandler
}


func NewGetAllCmd(handler *StdStoreHandler) *GetAllCmd {
return &GetAllCmd{
Name: "GETALL",
Name: "GETALL",
StdStoreHandler: handler,
}
}
Expand All @@ -38,7 +37,7 @@ func (handler *GetAllCmd) Handle(req *resp.Value) *resp.Value {
}

res = handler.run(req.Array[1:])

return handler.postProcess(res)
}

Expand Down Expand Up @@ -70,7 +69,7 @@ func (handler *GetAllCmd) postProcess(req *resp.Value) *resp.Value {
for idx, val := range req.Array {
if idx%2 == 0 {
decryptedVal, err := handler.securityH.Decrypter.Decrypt(val.Bulk)

// TODO: Handle error less promptly? allow some fields to be returned?
if err != nil {
return &resp.Value{Type: "error", String: "Error: " + err.Error()}
Expand All @@ -82,4 +81,4 @@ func (handler *GetAllCmd) postProcess(req *resp.Value) *resp.Value {
}

return &resp.Value{Type: "array", Array: decryptedVals}
}
}
8 changes: 4 additions & 4 deletions internal/handlers/store/std/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type SetCmd struct {

func NewSetCmd(handler *StdStoreHandler) *SetCmd {
return &SetCmd{
Name: "SET",
Name: "SET",
StdStoreHandler: handler,
}
}

func (handler *SetCmd) Handle(req *resp.Value, skp bool) (*resp.Value) {
func (handler *SetCmd) Handle(req *resp.Value, skp bool) *resp.Value {
if skp {
return handler.run(req.Array[1:])
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func (handler *SetCmd) preProcess(req *resp.Value) *resp.Value {
return nil
}

func (handler *SetCmd) run(args []resp.Value) *resp.Value{
func (handler *SetCmd) run(args []resp.Value) *resp.Value {
key := args[0].Bulk
value := args[1].Bulk

Expand All @@ -79,7 +79,7 @@ func (handler *SetCmd) run(args []resp.Value) *resp.Value{
return nil
}

func (handler *SetCmd) postProcess(req *resp.Value) (*resp.Value) {
func (handler *SetCmd) postProcess(req *resp.Value) *resp.Value {
err := handler.aofH.Write(req)
if err != nil {
return &resp.Value{Type: "error", String: "Error: " + err.Error()}
Expand Down
9 changes: 4 additions & 5 deletions internal/handlers/store/std/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ type StdStoreHandler struct {
mu sync.RWMutex

securityH *security.Security
aofH *aof.Aof
aofH *aof.Aof
}

func NewStdStoreHandler(secH *security.Security, aofH *aof.Aof) *StdStoreHandler {
return &StdStoreHandler{
store: map[string]string{},
mu: sync.RWMutex{},
store: map[string]string{},
mu: sync.RWMutex{},
securityH: secH,
aofH: aofH,
aofH: aofH,
}
}

6 changes: 3 additions & 3 deletions internal/security/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (

type Decrypter struct {
privateKey string
bytes []byte
bytes []byte
}

func NewDecrypter(privateKey string, byts []byte) *Decrypter {
return &Decrypter{
privateKey: privateKey,
bytes: byts,
bytes: byts,
}
}

Expand All @@ -38,7 +38,7 @@ func (decrypt *Decrypter) Decode(s string) ([]byte, error) {
return nil, err
}
return data, nil
}
}

func (decrypt *Decrypter) Decrypt(text string) (string, error) {
block, err := aes.NewCipher([]byte(decrypt.privateKey))
Expand Down
32 changes: 16 additions & 16 deletions internal/security/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ import (
)

type Encrypter struct {
privateKey string
bytes []byte
privateKey string
bytes []byte
}

func NewEncrypter(privateKey string, byts []byte) *Encrypter {
return &Encrypter{
privateKey: privateKey,
bytes: byts,
}
return &Encrypter{
privateKey: privateKey,
bytes: byts,
}
}

func (encrypt *Encrypter) Encode(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
return base64.StdEncoding.EncodeToString(b)
}

func (encrypt *Encrypter) Encrypt(text string) (string, error) {
block, err := aes.NewCipher([]byte(encrypt.privateKey))
if err != nil {
return "", err
}
plainText := []byte(text)
cfb := cipher.NewCFBEncrypter(block, encrypt.bytes)
cipherText := make([]byte, len(plainText))
cfb.XORKeyStream(cipherText, plainText)
return encrypt.Encode(cipherText), nil
block, err := aes.NewCipher([]byte(encrypt.privateKey))
if err != nil {
return "", err
}
plainText := []byte(text)
cfb := cipher.NewCFBEncrypter(block, encrypt.bytes)
cipherText := make([]byte, len(plainText))
cfb.XORKeyStream(cipherText, plainText)
return encrypt.Encode(cipherText), nil
}

// func Encrypter() {
Expand Down
1 change: 0 additions & 1 deletion internal/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ func NewSecurity(privateKey string, bytes []byte) *Security {
Decrypter: NewDecrypter(privateKey, bytes),
}
}

0 comments on commit 172ca85

Please sign in to comment.