[#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:
Leonard Lyubich 2020-08-26 12:20:26 +03:00 committed by Alex Vanin
parent b5671c1971
commit 6b4fb3a0aa
5 changed files with 97 additions and 125 deletions

View file

@ -10,21 +10,23 @@ import (
type signService struct { type signService struct {
unarySigService *util.UnarySignService unarySigService *util.UnarySignService
svc accounting.Service
} }
func NewSignService(key *ecdsa.PrivateKey, svc accounting.Service) accounting.Service { func NewSignService(key *ecdsa.PrivateKey, svc accounting.Service) accounting.Service {
return &signService{ return &signService{
unarySigService: util.NewUnarySignService( unarySigService: util.NewUnarySignService(key),
key, svc: svc,
func(ctx context.Context, req interface{}) (interface{}, error) {
return svc.Balance(ctx, req.(*accounting.BalanceRequest))
},
),
} }
} }
func (s *signService) Balance(ctx context.Context, req *accounting.BalanceRequest) (*accounting.BalanceResponse, error) { 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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -9,57 +9,24 @@ import (
) )
type signService struct { type signService struct {
putSigService *util.UnarySignService unarySigService *util.UnarySignService
getSigService *util.UnarySignService
delSigService *util.UnarySignService svc container.Service
listSigService *util.UnarySignService
setEACLSigService *util.UnarySignService
getEACLSigService *util.UnarySignService
} }
func NewSignService(key *ecdsa.PrivateKey, svc container.Service) container.Service { func NewSignService(key *ecdsa.PrivateKey, svc container.Service) container.Service {
return &signService{ return &signService{
putSigService: util.NewUnarySignService( unarySigService: util.NewUnarySignService(key),
key, svc: svc,
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))
},
),
} }
} }
func (s *signService) Put(ctx context.Context, req *container.PutRequest) (*container.PutResponse, error) { 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 { if err != nil {
return nil, err 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) { 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 { if err != nil {
return nil, err 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) { 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 { if err != nil {
return nil, err 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) { 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 { if err != nil {
return nil, err 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) { 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 { if err != nil {
return nil, err 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) { 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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -13,13 +13,9 @@ import (
type signService struct { type signService struct {
key *ecdsa.PrivateKey key *ecdsa.PrivateKey
searchSigService *util.UnarySignService unarySigService *util.UnarySignService
getSigService *util.UnarySignService
putService object.Service svc object.Service
rangeSigService *util.UnarySignService
headSigService *util.UnarySignService
delSigService *util.UnarySignService
rangeHashSigService *util.UnarySignService
} }
type searchStreamSigner struct { type searchStreamSigner struct {
@ -49,43 +45,8 @@ type getRangeStreamSigner struct {
func NewSignService(key *ecdsa.PrivateKey, svc object.Service) object.Service { func NewSignService(key *ecdsa.PrivateKey, svc object.Service) object.Service {
return &signService{ return &signService{
key: key, key: key,
searchSigService: util.NewUnarySignService( unarySigService: util.NewUnarySignService(key),
nil, // private key is not needed because service returns stream svc: svc,
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))
},
),
} }
} }
@ -103,7 +64,11 @@ func (s *getStreamSigner) Recv() (*object.GetResponse, error) {
} }
func (s *signService) Get(ctx context.Context, req *object.GetRequest) (object.GetObjectStreamer, 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 { if err != nil {
return nil, err return nil, err
} }
@ -136,7 +101,7 @@ func (s *putStreamSigner) CloseAndRecv() (*object.PutResponse, error) {
} }
func (s *signService) Put(ctx context.Context) (object.PutObjectStreamer, 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 { if err != nil {
return nil, errors.Wrap(err, "could not create Put object streamer") 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) { 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 { if err != nil {
return nil, err 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) { 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 { if err != nil {
return nil, err 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) { 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 { if err != nil {
return nil, err 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) { 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 { if err != nil {
return nil, err 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) { 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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -10,21 +10,22 @@ import (
type signService struct { type signService struct {
unarySigService *util.UnarySignService unarySigService *util.UnarySignService
svc session.Service
} }
func NewSignService(key *ecdsa.PrivateKey, svc session.Service) session.Service { func NewSignService(key *ecdsa.PrivateKey, svc session.Service) session.Service {
return &signService{ return &signService{
unarySigService: util.NewUnarySignService( unarySigService: util.NewUnarySignService(key),
key,
func(ctx context.Context, req interface{}) (interface{}, error) {
return svc.Create(ctx, req.(*session.CreateRequest))
},
),
} }
} }
func (s *signService) Create(ctx context.Context, req *session.CreateRequest) (*session.CreateResponse, error) { 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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -12,24 +12,21 @@ type UnaryHandler func(context.Context, interface{}) (interface{}, error)
type UnarySignService struct { type UnarySignService struct {
key *ecdsa.PrivateKey key *ecdsa.PrivateKey
unaryHandler UnaryHandler
} }
func NewUnarySignService(key *ecdsa.PrivateKey, handler UnaryHandler) *UnarySignService { func NewUnarySignService(key *ecdsa.PrivateKey) *UnarySignService {
return &UnarySignService{ return &UnarySignService{
key: key, key: key,
unaryHandler: handler,
} }
} }
func (s *UnarySignService) HandleServerStreamRequest(ctx context.Context, req interface{}) (interface{}, error) { func (s *UnarySignService) HandleServerStreamRequest(ctx context.Context, req interface{}, handler UnaryHandler) (interface{}, error) {
return s.verifyAndProc(ctx, req) 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 // verify and process request
resp, err := s.verifyAndProc(ctx, req) resp, err := s.verifyAndProc(ctx, req, handler)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -42,14 +39,14 @@ func (s *UnarySignService) HandleUnaryRequest(ctx context.Context, req interface
return resp, nil 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 // verify request signatures
if err := signature.VerifyServiceMessage(req); err != nil { if err := signature.VerifyServiceMessage(req); err != nil {
return nil, errors.Wrap(err, "could not verify request") return nil, errors.Wrap(err, "could not verify request")
} }
// process request // process request
resp, err := s.unaryHandler(ctx, req) resp, err := handler(ctx, req)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not handle request") return nil, errors.Wrap(err, "could not handle request")
} }