Files
archie/internal/infrastructure/messagingrpc/messagingrpc.go
T

94 lines
3.3 KiB
Go

// Package messagingrpc is the transport for messaging.v1.MessagingService:
// the server the Messaging Service registers and the client other services
// dial to deliver outbound chat messages.
package messagingrpc
import (
"context"
"errors"
"fmt"
"strings"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/messaging/v1"
"github.com/samcharles93/archie-core/internal/domain/presence"
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
"github.com/samcharles93/archie-core/internal/logging"
)
// ErrUnavailable marks a delivery the channel could not make now: the channel
// is stopped or cannot send. The caller may retry later.
var ErrUnavailable = errors.New("messaging: channel unavailable")
// ErrNoChannel marks a delivery to a channel this service does not have.
// Retrying cannot succeed.
var ErrNoChannel = errors.New("messaging: no such channel")
// Deliverer sends text to one chat on a named channel.
type Deliverer interface {
Deliver(ctx context.Context, channel, chatID, text string) error
}
type server struct {
pb.UnimplementedMessagingServiceServer
deliverer Deliverer
feed *logging.Feed
}
// RegisterServer registers MessagingService over d.
func RegisterServer(registrar grpc.ServiceRegistrar, d Deliverer, feed *logging.Feed) {
pb.RegisterMessagingServiceServer(registrar, &server{deliverer: d, feed: feed})
}
func (s *server) Deliver(ctx context.Context, req *pb.DeliverRequest) (*pb.DeliverResponse, error) {
if req.GetChannel() == "" || req.GetChatId() == "" || req.GetText() == "" {
return nil, status.Error(codes.InvalidArgument, "channel, chat_id and text are required")
}
err := s.deliverer.Deliver(ctx, req.GetChannel(), req.GetChatId(), req.GetText())
switch {
case err == nil:
return &pb.DeliverResponse{}, nil
case errors.Is(err, ErrNoChannel):
return nil, status.Error(codes.NotFound, err.Error())
case errors.Is(err, ErrUnavailable):
return nil, status.Error(codes.FailedPrecondition, err.Error())
default:
return nil, status.Error(codes.Unavailable, err.Error())
}
}
// ServerOptions serves listen with id's credentials.
func ServerOptions(listen string, id *rpctls.Identity) ([]grpc.ServerOption, error) {
creds, err := id.ServerOption(listen, false)
if err != nil {
return nil, fmt.Errorf("messaging: %w", err)
}
return []grpc.ServerOption{creds}, nil
}
// Client delivers through a dialled MessagingService.
type Client struct{ rpc pb.MessagingServiceClient }
// Dial returns a client for target, authenticated by id.
func Dial(target string, id *rpctls.Identity) (*Client, func(), error) {
target = strings.TrimSpace(target)
creds, err := id.DialOption(target, presence.Messaging)
if err != nil {
return nil, nil, fmt.Errorf("messaging: %w", err)
}
conn, err := grpc.NewClient(target, creds)
if err != nil {
return nil, nil, fmt.Errorf("create messaging client: %w", err)
}
return &Client{rpc: pb.NewMessagingServiceClient(conn)}, func() { _ = conn.Close() }, nil
}
// Deliver sends text to chatID on channel.
func (c *Client) Deliver(ctx context.Context, channel, chatID, text string) error {
_, err := c.rpc.Deliver(ctx, &pb.DeliverRequest{Channel: channel, ChatId: chatID, Text: text})
return err
}