Skip to content

Commit

Permalink
Merge pull request #15 from PapePathe/refactor-handlers
Browse files Browse the repository at this point in the history
feat: replace ClientRequest with IClientRequest
  • Loading branch information
PapePathe authored Apr 13, 2024
2 parents 8821b31 + d210da9 commit 943638f
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 145 deletions.
41 changes: 35 additions & 6 deletions internal/commands/client_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"pedis/internal/storage"
"strings"

"github.com/rs/zerolog"
"go.etcd.io/etcd/raft/v3/raftpb"
)

Expand All @@ -20,6 +19,10 @@ type IClientRequest interface {
WriteOK() error
WriteNil() error
Write([]byte) (int, error)
Data() [][]byte
DataRaw() RawRequest
Store() storage.Storage
SendClusterConfigChange(raftpb.ConfChange)
}

type RawRequest []byte
Expand Down Expand Up @@ -58,11 +61,37 @@ func (r RawRequest) ReadArray() []string {

type ClientRequest struct {
Conn net.Conn
Data [][]byte
DataRaw RawRequest
Store storage.Storage
Logger zerolog.Logger
ClusterChangesChan chan<- raftpb.ConfChange
data [][]byte
dataRaw RawRequest
store storage.Storage
clusterChangesChan chan<- raftpb.ConfChange
}

func NewClientRequest(c net.Conn, d [][]byte, s storage.Storage, rd RawRequest, cchan chan<- raftpb.ConfChange) IClientRequest {
return ClientRequest{
Conn: c,
data: d,
store: s,
dataRaw: rd,
clusterChangesChan: cchan,
}

}

func (c ClientRequest) SendClusterConfigChange(cc raftpb.ConfChange) {
c.clusterChangesChan <- cc
}

func (c ClientRequest) Data() [][]byte {
return c.data
}

func (c ClientRequest) DataRaw() RawRequest {
return c.dataRaw
}

func (c ClientRequest) Store() storage.Storage {
return c.store
}

func (c ClientRequest) WriteError(s string) error {
Expand Down
6 changes: 2 additions & 4 deletions internal/commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func DefaultRequestHandler() *RequestHandler {
return &RequestHandler{defaultCommands}
}

func (s RequestHandler) Run(request ClientRequest) {
subcommand := strings.ToLower(string(request.Data[2]))
func (s RequestHandler) Run(request IClientRequest) {
subcommand := strings.ToLower(string(request.Data()[2]))

if h, ok := s.subcommands[subcommand]; ok {
if err := h.Authorize(request); err != nil {
Expand All @@ -47,10 +47,8 @@ func (s RequestHandler) Run(request ClientRequest) {
switch subcommand {
case "client":
request.WriteString("OK")
request.Logger.Debug().Msg("going to execute client options command")
default:
request.WriteError(fmt.Sprintf("command not supported %v", subcommand))
request.Logger.Debug().Str("command", subcommand).Msg("is not yet supported")
}
}
}
8 changes: 4 additions & 4 deletions internal/commands/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package commands

type CommandHandler interface {
// Runs the request and write response to client
Handle(ClientRequest)
Handle(IClientRequest)
// Checks that calling user has the permissions required by the command
Authorize(ClientRequest) error
Authorize(IClientRequest) error
// Returns the list of permissions required to run the command
Permissions() []string
Permissions(IClientRequest) []string
// Returns true if the command is going to persist data
Persistent() bool
Persistent(IClientRequest) bool
}
37 changes: 13 additions & 24 deletions internal/commands/handler_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,20 @@ import (

type AclHandler struct{}

func (ch AclHandler) Authorize(ClientRequest) error {
func (ch AclHandler) Authorize(IClientRequest) error {
return nil
}

func (ch AclHandler) Permissions() []string {
func (ch AclHandler) Permissions(IClientRequest) []string {
return nil
}

func (ch AclHandler) Persistent() bool {
func (ch AclHandler) Persistent(IClientRequest) bool {
return false
}

func (ch AclHandler) Handle(r ClientRequest) {
data := r.DataRaw.ReadArray()
r.Logger.
Debug().
Interface("Data", data).
Interface("RawData", r.DataRaw.String()).
Msg("")

func (ch AclHandler) Handle(r IClientRequest) {
data := r.DataRaw().ReadArray()
svc := aclService{}

switch data[0] {
Expand All @@ -43,13 +37,12 @@ func (ch AclHandler) Handle(r ClientRequest) {

type aclService struct{}

func (aclService) deluser(r ClientRequest) error {
data := r.DataRaw.ReadArray()
r.Logger.Debug().Interface("usernames", data[1:]).Msg("Going to delete")
func (aclService) deluser(r IClientRequest) error {
data := r.DataRaw().ReadArray()
delCount := 0

for _, u := range data[1:] {
err := r.Store.DelUser(u)
err := r.Store().DelUser(u)

if err == nil {
delCount++
Expand All @@ -61,9 +54,8 @@ func (aclService) deluser(r ClientRequest) error {
return nil
}

func (aclService) setuser(r ClientRequest) error {
data := r.DataRaw.ReadArray()
r.Logger.Debug().Msg("Going to create or update existing user")
func (aclService) setuser(r IClientRequest) error {
data := r.DataRaw().ReadArray()
username := data[1]
rules := []storage.AclRule{}

Expand All @@ -89,16 +81,13 @@ func (aclService) setuser(r ClientRequest) error {
}
}

_ = r.Store.SetUser(username, rules)
_ = r.Store().SetUser(username, rules)
r.WriteOK()
return nil
}

func (aclService) users(r ClientRequest) error {
r.Logger.Debug().Msg("Going to list users")

users := r.Store.Users()
r.Logger.Debug().Interface("Users list", users).Msg("")
func (aclService) users(r IClientRequest) error {
users := r.Store().Users()
r.WriteArray(users)

return nil
Expand Down
13 changes: 6 additions & 7 deletions internal/commands/handler_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ package commands

type AuthHandler struct{}

func (ch AuthHandler) Authorize(ClientRequest) error {
func (ch AuthHandler) Authorize(IClientRequest) error {
return nil
}

func (ch AuthHandler) Permissions() []string {
func (ch AuthHandler) Permissions(IClientRequest) []string {
return nil
}

func (ch AuthHandler) Persistent() bool {
func (ch AuthHandler) Persistent(IClientRequest) bool {
return false
}

func (ch AuthHandler) Handle(r ClientRequest) {
data := r.DataRaw.ReadArray()
r.Logger.Info().Interface("auth params", data).Msg("")
func (ch AuthHandler) Handle(r IClientRequest) {
data := r.DataRaw().ReadArray()

user, err := r.Store.GetUser(data[0])
user, err := r.Store().GetUser(data[0])
if err != nil {
r.WriteError(err.Error())
return
Expand Down
14 changes: 7 additions & 7 deletions internal/commands/handler_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (

type ClusterHandler struct{}

func (ch ClusterHandler) Authorize(ClientRequest) error {
func (ch ClusterHandler) Authorize(IClientRequest) error {
return nil
}

func (ch ClusterHandler) Permissions() []string {
func (ch ClusterHandler) Permissions(IClientRequest) []string {
return nil
}

func (ch ClusterHandler) Persistent() bool {
func (ch ClusterHandler) Persistent(IClientRequest) bool {
return false
}

func (ch ClusterHandler) Handle(r ClientRequest) {
data := r.DataRaw.ReadArray()
func (ch ClusterHandler) Handle(r IClientRequest) {
data := r.DataRaw().ReadArray()
log.Println(data)

switch string(data[0]) {
Expand All @@ -37,7 +37,7 @@ func (ch ClusterHandler) Handle(r ClientRequest) {
NodeID: uint64(id),
}
r.WriteOK()
r.ClusterChangesChan <- cc
r.SendClusterConfigChange(cc)

case "meet":
id, err := strconv.Atoi(data[1])
Expand All @@ -52,7 +52,7 @@ func (ch ClusterHandler) Handle(r ClientRequest) {
Context: []byte(data[2]),
}
r.WriteOK()
r.ClusterChangesChan <- cc
r.SendClusterConfigChange(cc)
default:
r.WriteError("subcommand not found")
}
Expand Down
8 changes: 4 additions & 4 deletions internal/commands/handler_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package commands

type ConfigHandler struct{}

func (ch ConfigHandler) Authorize(ClientRequest) error {
func (ch ConfigHandler) Authorize(IClientRequest) error {
return nil
}

func (ch ConfigHandler) Permissions() []string {
func (ch ConfigHandler) Permissions(IClientRequest) []string {
return nil
}

func (ch ConfigHandler) Persistent() bool {
func (ch ConfigHandler) Persistent(IClientRequest) bool {
return false
}

func (ch ConfigHandler) Handle(r ClientRequest) {
func (ch ConfigHandler) Handle(r IClientRequest) {
r.WriteError("not yet implemented")
}

Expand Down
15 changes: 6 additions & 9 deletions internal/commands/handler_del.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@ import "fmt"

type DelHandler struct{}

func (ch DelHandler) Authorize(ClientRequest) error {
func (ch DelHandler) Authorize(IClientRequest) error {
return nil
}

func (ch DelHandler) Permissions() []string {
func (ch DelHandler) Permissions(IClientRequest) []string {
return nil
}

func (ch DelHandler) Persistent() bool {
func (ch DelHandler) Persistent(IClientRequest) bool {
return false
}

func (ch DelHandler) Handle(r ClientRequest) {
r.Logger.Debug().Interface("Data", r.DataRaw.ReadArray()).Str("RawData", r.DataRaw.String()).Msg("del handler")

func (ch DelHandler) Handle(r IClientRequest) {
delCount := 0

for _, key := range r.DataRaw.ReadArray() {
err := r.Store.Del(key)
for _, key := range r.DataRaw().ReadArray() {
err := r.Store().Del(key)

if err != nil {
r.Logger.Error().Str("Key", key).Err(err).Msg("del handler")
continue
}

Expand Down
10 changes: 5 additions & 5 deletions internal/commands/handler_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (

type GetHandler struct{}

func (ch GetHandler) Authorize(ClientRequest) error {
func (ch GetHandler) Authorize(IClientRequest) error {
return nil
}

func (ch GetHandler) Permissions() []string {
func (ch GetHandler) Permissions(IClientRequest) []string {
return nil
}

func (ch GetHandler) Persistent() bool {
func (ch GetHandler) Persistent(IClientRequest) bool {
return true
}

func (ch GetHandler) Handle(r ClientRequest) {
val, err := r.Store.Get(string(r.Data[4]))
func (ch GetHandler) Handle(r IClientRequest) {
val, err := r.Store().Get(string(r.Data()[4]))
if err != nil {
r.Write([]byte("-ERR key not found\r\n"))
}
Expand Down
13 changes: 6 additions & 7 deletions internal/commands/handler_hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ package commands

type HelloHandler struct{}

func (ch HelloHandler) Authorize(ClientRequest) error {
func (ch HelloHandler) Authorize(IClientRequest) error {
return nil
}

func (ch HelloHandler) Permissions() []string {
func (ch HelloHandler) Permissions(IClientRequest) []string {
return nil
}

func (ch HelloHandler) Persistent() bool {
func (ch HelloHandler) Persistent(IClientRequest) bool {
return false
}

func (ch HelloHandler) Handle(r ClientRequest) {
data := r.DataRaw.ReadArray()
r.Logger.Info().Interface("Parameters", data).Msg("")
func (ch HelloHandler) Handle(r IClientRequest) {
data := r.DataRaw().ReadArray()

user, err := r.Store.GetUser(data[1])
user, err := r.Store().GetUser(data[1])
if err != nil {
r.WriteError(err.Error())
return
Expand Down
Loading

0 comments on commit 943638f

Please sign in to comment.