forked from TrueCloudLab/frostfs-node
[#13] services: Refactor UnarySignService
Replace UnaryHandler from structure to method arguments in order to reuse single instance for different service methods. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
b5671c1971
commit
6b4fb3a0aa
5 changed files with 97 additions and 125 deletions
|
@ -10,21 +10,23 @@ import (
|
|||
|
||||
type signService struct {
|
||||
unarySigService *util.UnarySignService
|
||||
|
||||
svc accounting.Service
|
||||
}
|
||||
|
||||
func NewSignService(key *ecdsa.PrivateKey, svc accounting.Service) accounting.Service {
|
||||
return &signService{
|
||||
unarySigService: util.NewUnarySignService(
|
||||
key,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.Balance(ctx, req.(*accounting.BalanceRequest))
|
||||
},
|
||||
),
|
||||
unarySigService: util.NewUnarySignService(key),
|
||||
svc: svc,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signService) Balance(ctx context.Context, req *accounting.BalanceRequest) (*accounting.BalanceResponse, error) {
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.Balance(ctx, req.(*accounting.BalanceRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -9,57 +9,24 @@ import (
|
|||
)
|
||||
|
||||
type signService struct {
|
||||
putSigService *util.UnarySignService
|
||||
getSigService *util.UnarySignService
|
||||
delSigService *util.UnarySignService
|
||||
listSigService *util.UnarySignService
|
||||
setEACLSigService *util.UnarySignService
|
||||
getEACLSigService *util.UnarySignService
|
||||
unarySigService *util.UnarySignService
|
||||
|
||||
svc container.Service
|
||||
}
|
||||
|
||||
func NewSignService(key *ecdsa.PrivateKey, svc container.Service) container.Service {
|
||||
return &signService{
|
||||
putSigService: util.NewUnarySignService(
|
||||
key,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.Put(ctx, req.(*container.PutRequest))
|
||||
},
|
||||
),
|
||||
getSigService: util.NewUnarySignService(
|
||||
key,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.Get(ctx, req.(*container.GetRequest))
|
||||
},
|
||||
),
|
||||
delSigService: util.NewUnarySignService(
|
||||
key,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.Delete(ctx, req.(*container.DeleteRequest))
|
||||
},
|
||||
),
|
||||
listSigService: util.NewUnarySignService(
|
||||
key,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.List(ctx, req.(*container.ListRequest))
|
||||
},
|
||||
),
|
||||
setEACLSigService: util.NewUnarySignService(
|
||||
key,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.SetExtendedACL(ctx, req.(*container.SetExtendedACLRequest))
|
||||
},
|
||||
),
|
||||
getEACLSigService: util.NewUnarySignService(
|
||||
key,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.GetExtendedACL(ctx, req.(*container.GetExtendedACLRequest))
|
||||
},
|
||||
),
|
||||
unarySigService: util.NewUnarySignService(key),
|
||||
svc: svc,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signService) Put(ctx context.Context, req *container.PutRequest) (*container.PutResponse, error) {
|
||||
resp, err := s.putSigService.HandleUnaryRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.Put(ctx, req.(*container.PutRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -68,7 +35,11 @@ func (s *signService) Put(ctx context.Context, req *container.PutRequest) (*cont
|
|||
}
|
||||
|
||||
func (s *signService) Delete(ctx context.Context, req *container.DeleteRequest) (*container.DeleteResponse, error) {
|
||||
resp, err := s.delSigService.HandleUnaryRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.Get(ctx, req.(*container.GetRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -77,7 +48,11 @@ func (s *signService) Delete(ctx context.Context, req *container.DeleteRequest)
|
|||
}
|
||||
|
||||
func (s *signService) Get(ctx context.Context, req *container.GetRequest) (*container.GetResponse, error) {
|
||||
resp, err := s.getSigService.HandleUnaryRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.Delete(ctx, req.(*container.DeleteRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -86,7 +61,11 @@ func (s *signService) Get(ctx context.Context, req *container.GetRequest) (*cont
|
|||
}
|
||||
|
||||
func (s *signService) List(ctx context.Context, req *container.ListRequest) (*container.ListResponse, error) {
|
||||
resp, err := s.listSigService.HandleUnaryRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.List(ctx, req.(*container.ListRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -95,7 +74,11 @@ func (s *signService) List(ctx context.Context, req *container.ListRequest) (*co
|
|||
}
|
||||
|
||||
func (s *signService) SetExtendedACL(ctx context.Context, req *container.SetExtendedACLRequest) (*container.SetExtendedACLResponse, error) {
|
||||
resp, err := s.setEACLSigService.HandleUnaryRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.SetExtendedACL(ctx, req.(*container.SetExtendedACLRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -104,7 +87,11 @@ func (s *signService) SetExtendedACL(ctx context.Context, req *container.SetExte
|
|||
}
|
||||
|
||||
func (s *signService) GetExtendedACL(ctx context.Context, req *container.GetExtendedACLRequest) (*container.GetExtendedACLResponse, error) {
|
||||
resp, err := s.getEACLSigService.HandleUnaryRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.GetExtendedACL(ctx, req.(*container.GetExtendedACLRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -13,13 +13,9 @@ import (
|
|||
type signService struct {
|
||||
key *ecdsa.PrivateKey
|
||||
|
||||
searchSigService *util.UnarySignService
|
||||
getSigService *util.UnarySignService
|
||||
putService object.Service
|
||||
rangeSigService *util.UnarySignService
|
||||
headSigService *util.UnarySignService
|
||||
delSigService *util.UnarySignService
|
||||
rangeHashSigService *util.UnarySignService
|
||||
unarySigService *util.UnarySignService
|
||||
|
||||
svc object.Service
|
||||
}
|
||||
|
||||
type searchStreamSigner struct {
|
||||
|
@ -49,43 +45,8 @@ type getRangeStreamSigner struct {
|
|||
func NewSignService(key *ecdsa.PrivateKey, svc object.Service) object.Service {
|
||||
return &signService{
|
||||
key: key,
|
||||
searchSigService: util.NewUnarySignService(
|
||||
nil, // private key is not needed because service returns stream
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.Search(ctx, req.(*object.SearchRequest))
|
||||
},
|
||||
),
|
||||
getSigService: util.NewUnarySignService(
|
||||
nil, // private key is not needed because service returns stream
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.Get(ctx, req.(*object.GetRequest))
|
||||
},
|
||||
),
|
||||
putService: svc,
|
||||
rangeSigService: util.NewUnarySignService(
|
||||
nil, // private key is not needed because service returns stream
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.GetRange(ctx, req.(*object.GetRangeRequest))
|
||||
},
|
||||
),
|
||||
headSigService: util.NewUnarySignService(
|
||||
key,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.Head(ctx, req.(*object.HeadRequest))
|
||||
},
|
||||
),
|
||||
delSigService: util.NewUnarySignService(
|
||||
key,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.Delete(ctx, req.(*object.DeleteRequest))
|
||||
},
|
||||
),
|
||||
rangeHashSigService: util.NewUnarySignService(
|
||||
key,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.GetRangeHash(ctx, req.(*object.GetRangeHashRequest))
|
||||
},
|
||||
),
|
||||
unarySigService: util.NewUnarySignService(key),
|
||||
svc: svc,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -103,7 +64,11 @@ func (s *getStreamSigner) Recv() (*object.GetResponse, error) {
|
|||
}
|
||||
|
||||
func (s *signService) Get(ctx context.Context, req *object.GetRequest) (object.GetObjectStreamer, error) {
|
||||
resp, err := s.getSigService.HandleServerStreamRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleServerStreamRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.Get(ctx, req.(*object.GetRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -136,7 +101,7 @@ func (s *putStreamSigner) CloseAndRecv() (*object.PutResponse, error) {
|
|||
}
|
||||
|
||||
func (s *signService) Put(ctx context.Context) (object.PutObjectStreamer, error) {
|
||||
stream, err := s.putService.Put(ctx)
|
||||
stream, err := s.svc.Put(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not create Put object streamer")
|
||||
}
|
||||
|
@ -148,7 +113,11 @@ func (s *signService) Put(ctx context.Context) (object.PutObjectStreamer, error)
|
|||
}
|
||||
|
||||
func (s *signService) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) {
|
||||
resp, err := s.headSigService.HandleUnaryRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.Head(ctx, req.(*object.HeadRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -170,7 +139,11 @@ func (s *searchStreamSigner) Recv() (*object.SearchResponse, error) {
|
|||
}
|
||||
|
||||
func (s *signService) Search(ctx context.Context, req *object.SearchRequest) (object.SearchObjectStreamer, error) {
|
||||
resp, err := s.searchSigService.HandleServerStreamRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleServerStreamRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.Search(ctx, req.(*object.SearchRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -182,7 +155,11 @@ func (s *signService) Search(ctx context.Context, req *object.SearchRequest) (ob
|
|||
}
|
||||
|
||||
func (s *signService) Delete(ctx context.Context, req *object.DeleteRequest) (*object.DeleteResponse, error) {
|
||||
resp, err := s.delSigService.HandleUnaryRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.Delete(ctx, req.(*object.DeleteRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -204,7 +181,11 @@ func (s *getRangeStreamSigner) Recv() (*object.GetRangeResponse, error) {
|
|||
}
|
||||
|
||||
func (s *signService) GetRange(ctx context.Context, req *object.GetRangeRequest) (object.GetRangeObjectStreamer, error) {
|
||||
resp, err := s.rangeSigService.HandleServerStreamRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleServerStreamRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.GetRange(ctx, req.(*object.GetRangeRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -216,7 +197,11 @@ func (s *signService) GetRange(ctx context.Context, req *object.GetRangeRequest)
|
|||
}
|
||||
|
||||
func (s *signService) GetRangeHash(ctx context.Context, req *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
|
||||
resp, err := s.rangeHashSigService.HandleUnaryRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.GetRangeHash(ctx, req.(*object.GetRangeHashRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -10,21 +10,22 @@ import (
|
|||
|
||||
type signService struct {
|
||||
unarySigService *util.UnarySignService
|
||||
|
||||
svc session.Service
|
||||
}
|
||||
|
||||
func NewSignService(key *ecdsa.PrivateKey, svc session.Service) session.Service {
|
||||
return &signService{
|
||||
unarySigService: util.NewUnarySignService(
|
||||
key,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return svc.Create(ctx, req.(*session.CreateRequest))
|
||||
},
|
||||
),
|
||||
unarySigService: util.NewUnarySignService(key),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signService) Create(ctx context.Context, req *session.CreateRequest) (*session.CreateResponse, error) {
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req)
|
||||
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
||||
func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.svc.Create(ctx, req.(*session.CreateRequest))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -12,24 +12,21 @@ type UnaryHandler func(context.Context, interface{}) (interface{}, error)
|
|||
|
||||
type UnarySignService struct {
|
||||
key *ecdsa.PrivateKey
|
||||
|
||||
unaryHandler UnaryHandler
|
||||
}
|
||||
|
||||
func NewUnarySignService(key *ecdsa.PrivateKey, handler UnaryHandler) *UnarySignService {
|
||||
func NewUnarySignService(key *ecdsa.PrivateKey) *UnarySignService {
|
||||
return &UnarySignService{
|
||||
key: key,
|
||||
unaryHandler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *UnarySignService) HandleServerStreamRequest(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return s.verifyAndProc(ctx, req)
|
||||
func (s *UnarySignService) HandleServerStreamRequest(ctx context.Context, req interface{}, handler UnaryHandler) (interface{}, error) {
|
||||
return s.verifyAndProc(ctx, req, handler)
|
||||
}
|
||||
|
||||
func (s *UnarySignService) HandleUnaryRequest(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
func (s *UnarySignService) HandleUnaryRequest(ctx context.Context, req interface{}, handler UnaryHandler) (interface{}, error) {
|
||||
// verify and process request
|
||||
resp, err := s.verifyAndProc(ctx, req)
|
||||
resp, err := s.verifyAndProc(ctx, req, handler)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -42,14 +39,14 @@ func (s *UnarySignService) HandleUnaryRequest(ctx context.Context, req interface
|
|||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *UnarySignService) verifyAndProc(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
func (s *UnarySignService) verifyAndProc(ctx context.Context, req interface{}, handler UnaryHandler) (interface{}, error) {
|
||||
// verify request signatures
|
||||
if err := signature.VerifyServiceMessage(req); err != nil {
|
||||
return nil, errors.Wrap(err, "could not verify request")
|
||||
}
|
||||
|
||||
// process request
|
||||
resp, err := s.unaryHandler(ctx, req)
|
||||
resp, err := handler(ctx, req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not handle request")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue