2019-04-15 03:56:55 +00:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/log"
|
2019-08-01 03:16:20 +00:00
|
|
|
"github.com/go-kit/kit/transport"
|
2019-04-15 03:56:55 +00:00
|
|
|
"github.com/go-kit/kit/transport/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2019-07-12 01:02:46 +00:00
|
|
|
protobuf "github.com/mitchell/selfpass/protobuf/go"
|
2019-07-11 02:33:22 +00:00
|
|
|
"github.com/mitchell/selfpass/services/credentials/endpoints"
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/types"
|
2019-04-15 03:56:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewGRPCServer(svc types.Service, logger log.Logger) GRPCServer {
|
|
|
|
return GRPCServer{
|
|
|
|
getAllMetadata: grpc.NewServer(
|
|
|
|
endpoints.MakeGetAllMetadataEndpoint(svc),
|
2019-07-12 01:02:46 +00:00
|
|
|
decodeSourceHostRequest,
|
2019-04-15 03:56:55 +00:00
|
|
|
encodeMetadataStreamResponse,
|
2019-08-01 03:16:20 +00:00
|
|
|
grpc.ServerErrorHandler(transport.NewLogErrorHandler(logger)),
|
2019-04-15 03:56:55 +00:00
|
|
|
),
|
2019-05-06 00:56:27 +00:00
|
|
|
get: grpc.NewServer(
|
|
|
|
endpoints.MakeGetEndpoint(svc),
|
|
|
|
decodeIdRequest,
|
|
|
|
encodeCredentialResponse,
|
2019-08-01 03:16:20 +00:00
|
|
|
grpc.ServerErrorHandler(transport.NewLogErrorHandler(logger)),
|
2019-05-06 00:56:27 +00:00
|
|
|
),
|
2019-04-15 03:56:55 +00:00
|
|
|
create: grpc.NewServer(
|
|
|
|
endpoints.MakeCreateEndpoint(svc),
|
|
|
|
decodeCredentialRequest,
|
|
|
|
encodeCredentialResponse,
|
2019-08-01 03:16:20 +00:00
|
|
|
grpc.ServerErrorHandler(transport.NewLogErrorHandler(logger)),
|
2019-04-15 03:56:55 +00:00
|
|
|
),
|
|
|
|
update: grpc.NewServer(
|
|
|
|
endpoints.MakeUpdateEndpoint(svc),
|
|
|
|
decodeUpdateRequest,
|
|
|
|
encodeCredentialResponse,
|
2019-08-01 03:16:20 +00:00
|
|
|
grpc.ServerErrorHandler(transport.NewLogErrorHandler(logger)),
|
2019-04-15 03:56:55 +00:00
|
|
|
),
|
|
|
|
delete: grpc.NewServer(
|
|
|
|
endpoints.MakeDeleteEndpoint(svc),
|
|
|
|
decodeIdRequest,
|
2019-05-06 00:56:27 +00:00
|
|
|
noOp,
|
2019-08-01 03:16:20 +00:00
|
|
|
grpc.ServerErrorHandler(transport.NewLogErrorHandler(logger)),
|
2019-05-06 00:56:27 +00:00
|
|
|
),
|
2019-04-15 03:56:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type GRPCServer struct {
|
|
|
|
getAllMetadata *grpc.Server
|
2019-05-06 00:56:27 +00:00
|
|
|
get *grpc.Server
|
2019-04-15 03:56:55 +00:00
|
|
|
create *grpc.Server
|
|
|
|
update *grpc.Server
|
|
|
|
delete *grpc.Server
|
|
|
|
}
|
|
|
|
|
2019-07-12 01:02:46 +00:00
|
|
|
func (s GRPCServer) GetAllMetadata(r *protobuf.SourceHostRequest, srv protobuf.Credentials_GetAllMetadataServer) (err error) {
|
2019-04-15 03:56:55 +00:00
|
|
|
var i interface{}
|
|
|
|
ctx := srv.Context()
|
|
|
|
|
|
|
|
ctx, i, err = s.getAllMetadata.ServeGRPC(ctx, *r)
|
|
|
|
if err != nil {
|
2019-08-01 03:16:20 +00:00
|
|
|
err = handlerGRPCError(err)
|
2019-04-15 03:56:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
mds := i.(ProtobufMetadataStream)
|
2019-04-15 03:56:55 +00:00
|
|
|
|
|
|
|
receiveLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-08-01 03:16:20 +00:00
|
|
|
err = ctx.Err()
|
2019-04-15 03:56:55 +00:00
|
|
|
break receiveLoop
|
|
|
|
case err = <-mds.Errors:
|
2019-08-01 03:16:20 +00:00
|
|
|
if err != nil {
|
|
|
|
err = handlerGRPCError(err)
|
|
|
|
break receiveLoop
|
|
|
|
}
|
2019-04-15 03:56:55 +00:00
|
|
|
case md, ok := <-mds.Metadata:
|
|
|
|
if !ok {
|
|
|
|
break receiveLoop
|
|
|
|
}
|
|
|
|
if err = srv.Send(&md); err != nil {
|
2019-08-01 03:16:20 +00:00
|
|
|
err = handlerGRPCError(err)
|
2019-04-15 03:56:55 +00:00
|
|
|
break receiveLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-06 00:56:27 +00:00
|
|
|
func (s GRPCServer) Get(ctx context.Context, r *protobuf.IdRequest) (*protobuf.Credential, error) {
|
|
|
|
ctx, i, err := s.get.ServeGRPC(ctx, *r)
|
|
|
|
if err != nil {
|
|
|
|
err = handlerGRPCError(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &protobuf.Credential{}
|
|
|
|
*c = i.(protobuf.Credential)
|
|
|
|
return c, nil
|
2019-04-15 03:56:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 00:56:27 +00:00
|
|
|
func (s GRPCServer) Create(ctx context.Context, r *protobuf.CredentialRequest) (*protobuf.Credential, error) {
|
|
|
|
ctx, i, err := s.create.ServeGRPC(ctx, *r)
|
2019-04-15 03:56:55 +00:00
|
|
|
if err != nil {
|
|
|
|
err = handlerGRPCError(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &protobuf.Credential{}
|
|
|
|
*c = i.(protobuf.Credential)
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s GRPCServer) Update(ctx context.Context, r *protobuf.UpdateRequest) (*protobuf.Credential, error) {
|
|
|
|
ctx, i, err := s.update.ServeGRPC(ctx, *r)
|
|
|
|
if err != nil {
|
|
|
|
err = handlerGRPCError(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &protobuf.Credential{}
|
|
|
|
*c = i.(protobuf.Credential)
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 01:02:46 +00:00
|
|
|
func (s GRPCServer) Delete(ctx context.Context, r *protobuf.IdRequest) (*protobuf.SuccessResponse, error) {
|
2019-04-15 03:56:55 +00:00
|
|
|
ctx, _, err := s.delete.ServeGRPC(ctx, *r)
|
|
|
|
if err != nil {
|
2019-05-06 00:56:27 +00:00
|
|
|
err = handlerGRPCError(err)
|
2019-04-15 03:56:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-12 01:02:46 +00:00
|
|
|
return &protobuf.SuccessResponse{Success: true}, nil
|
2019-05-06 00:56:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 03:56:55 +00:00
|
|
|
func handlerGRPCError(err error) error {
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(err.Error(), types.InvalidArgument):
|
|
|
|
err = status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
case strings.HasPrefix(err.Error(), types.NotFound):
|
|
|
|
err = status.Error(codes.NotFound, err.Error())
|
|
|
|
default:
|
|
|
|
err = status.Error(codes.Internal, "an internal error has occurred")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|