diff --git a/internal/handlers/handler.go b/internal/handlers/handler.go index 6312e39..739372a 100644 --- a/internal/handlers/handler.go +++ b/internal/handlers/handler.go @@ -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" @@ -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 @@ -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): diff --git a/internal/handlers/handler_test.go b/internal/handlers/handler_test.go index 9627cbc..63c1370 100644 --- a/internal/handlers/handler_test.go +++ b/internal/handlers/handler_test.go @@ -17,8 +17,6 @@ package handlers // } // defer aofH.Close() - - // // set := []resp.Value{ // // {Type: "bulk", Bulk: "key"}, // // {Type: "bulk", Bulk: "value"}, @@ -44,4 +42,4 @@ package handlers // handler(args) // }) // } -// } \ No newline at end of file +// } diff --git a/internal/handlers/store/config/config.go b/internal/handlers/store/config/config.go index be3debb..9973f97 100644 --- a/internal/handlers/store/config/config.go +++ b/internal/handlers/store/config/config.go @@ -32,7 +32,7 @@ package config // type ConfigSubCmd string // var ( -// GetCmd ConfigSubCmd = "get" +// GetCmd ConfigSubCmd = "get" // SETCmd ConfigSubCmd = "set" // ) @@ -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"} -// } \ No newline at end of file +// } diff --git a/internal/handlers/store/config/get.go b/internal/handlers/store/config/get.go index 90e15da..3c5bbb5 100644 --- a/internal/handlers/store/config/get.go +++ b/internal/handlers/store/config/get.go @@ -40,4 +40,4 @@ package config // } // return resp.Value{Type: "array", Array: vals} -// } \ No newline at end of file +// } diff --git a/internal/handlers/store/nested/hset.go b/internal/handlers/store/nested/hset.go index 83ed210..3a981a7 100644 --- a/internal/handlers/store/nested/hset.go +++ b/internal/handlers/store/nested/hset.go @@ -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:]) } @@ -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) @@ -93,4 +92,3 @@ func (handler *HSetCmd) postProcess(req *resp.Value) *resp.Value { return nil } - diff --git a/internal/handlers/store/nested/nested.go b/internal/handlers/store/nested/nested.go index 8d885d8..71d596d 100644 --- a/internal/handlers/store/nested/nested.go +++ b/internal/handlers/store/nested/nested.go @@ -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, } } diff --git a/internal/handlers/store/std/get.go b/internal/handlers/store/std/get.go index eddfee9..a85e548 100644 --- a/internal/handlers/store/std/get.go +++ b/internal/handlers/store/std/get.go @@ -26,7 +26,7 @@ type GetCmd struct { func NewGetCmd(handler *StdStoreHandler) *GetCmd { return &GetCmd{ - Name: "GET", + Name: "GET", StdStoreHandler: handler, } } @@ -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} -} \ No newline at end of file +} diff --git a/internal/handlers/store/std/get_all.go b/internal/handlers/store/std/get_all.go index 7a4230a..63b6a1d 100644 --- a/internal/handlers/store/std/get_all.go +++ b/internal/handlers/store/std/get_all.go @@ -23,10 +23,9 @@ type GetAllCmd struct { *StdStoreHandler } - func NewGetAllCmd(handler *StdStoreHandler) *GetAllCmd { return &GetAllCmd{ - Name: "GETALL", + Name: "GETALL", StdStoreHandler: handler, } } @@ -38,7 +37,7 @@ func (handler *GetAllCmd) Handle(req *resp.Value) *resp.Value { } res = handler.run(req.Array[1:]) - + return handler.postProcess(res) } @@ -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()} @@ -82,4 +81,4 @@ func (handler *GetAllCmd) postProcess(req *resp.Value) *resp.Value { } return &resp.Value{Type: "array", Array: decryptedVals} -} \ No newline at end of file +} diff --git a/internal/handlers/store/std/set.go b/internal/handlers/store/std/set.go index f07c95f..7e53d24 100644 --- a/internal/handlers/store/std/set.go +++ b/internal/handlers/store/std/set.go @@ -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:]) } @@ -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 @@ -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()} diff --git a/internal/handlers/store/std/std.go b/internal/handlers/store/std/std.go index 1efee87..19e4245 100644 --- a/internal/handlers/store/std/std.go +++ b/internal/handlers/store/std/std.go @@ -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, } } - diff --git a/internal/security/decrypt.go b/internal/security/decrypt.go index efa9aed..6a9947d 100644 --- a/internal/security/decrypt.go +++ b/internal/security/decrypt.go @@ -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, } } @@ -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)) diff --git a/internal/security/encrypt.go b/internal/security/encrypt.go index cfb2093..82489dd 100644 --- a/internal/security/encrypt.go +++ b/internal/security/encrypt.go @@ -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() { diff --git a/internal/security/security.go b/internal/security/security.go index 9d4dd4f..c48b687 100644 --- a/internal/security/security.go +++ b/internal/security/security.go @@ -25,4 +25,3 @@ func NewSecurity(privateKey string, bytes []byte) *Security { Decrypter: NewDecrypter(privateKey, bytes), } } -