service.go raw

   1  // Package server provides the gRPC server implementation for relay group.
   2  package server
   3  
   4  import (
   5  	"context"
   6  
   7  	"next.orly.dev/pkg/database"
   8  	"next.orly.dev/pkg/sync/relaygroup"
   9  	commonv1 "next.orly.dev/pkg/proto/orlysync/common/v1"
  10  	relaygroupv1 "next.orly.dev/pkg/proto/orlysync/relaygroup/v1"
  11  )
  12  
  13  // Service implements the RelayGroupServiceServer interface.
  14  type Service struct {
  15  	relaygroupv1.UnimplementedRelayGroupServiceServer
  16  	mgr   *relaygroup.Manager
  17  	db    database.Database
  18  	ready bool
  19  }
  20  
  21  // NewService creates a new relay group gRPC service.
  22  func NewService(db database.Database, mgr *relaygroup.Manager) *Service {
  23  	return &Service{
  24  		mgr:   mgr,
  25  		db:    db,
  26  		ready: true,
  27  	}
  28  }
  29  
  30  // Ready returns whether the service is ready to serve requests.
  31  func (s *Service) Ready(ctx context.Context, _ *commonv1.Empty) (*commonv1.ReadyResponse, error) {
  32  	return &commonv1.ReadyResponse{Ready: s.ready}, nil
  33  }
  34  
  35  // FindAuthoritativeConfig finds the authoritative relay group configuration.
  36  func (s *Service) FindAuthoritativeConfig(ctx context.Context, _ *commonv1.Empty) (*relaygroupv1.RelayGroupConfigResponse, error) {
  37  	config, err := s.mgr.FindAuthoritativeConfig(ctx)
  38  	if err != nil {
  39  		return &relaygroupv1.RelayGroupConfigResponse{
  40  			Found: false,
  41  		}, nil
  42  	}
  43  
  44  	if config == nil {
  45  		return &relaygroupv1.RelayGroupConfigResponse{
  46  			Found: false,
  47  		}, nil
  48  	}
  49  
  50  	return &relaygroupv1.RelayGroupConfigResponse{
  51  		Found: true,
  52  		Config: &relaygroupv1.RelayGroupConfig{
  53  			Relays: config.Relays,
  54  		},
  55  	}, nil
  56  }
  57  
  58  // GetRelays returns the list of relays from the authoritative config.
  59  func (s *Service) GetRelays(ctx context.Context, _ *commonv1.Empty) (*relaygroupv1.RelaysResponse, error) {
  60  	relays, err := s.mgr.FindAuthoritativeRelays(ctx)
  61  	if err != nil {
  62  		return &relaygroupv1.RelaysResponse{}, nil
  63  	}
  64  
  65  	return &relaygroupv1.RelaysResponse{
  66  		Relays: relays,
  67  	}, nil
  68  }
  69  
  70  // IsAuthorizedPublisher checks if a pubkey can publish relay group configs.
  71  func (s *Service) IsAuthorizedPublisher(ctx context.Context, req *relaygroupv1.AuthorizedPublisherRequest) (*relaygroupv1.AuthorizedPublisherResponse, error) {
  72  	authorized := s.mgr.IsAuthorizedPublisher(req.Pubkey)
  73  	return &relaygroupv1.AuthorizedPublisherResponse{
  74  		Authorized: authorized,
  75  	}, nil
  76  }
  77  
  78  // GetAuthorizedPubkeys returns all authorized publisher pubkeys.
  79  func (s *Service) GetAuthorizedPubkeys(ctx context.Context, _ *commonv1.Empty) (*relaygroupv1.AuthorizedPubkeysResponse, error) {
  80  	pubkeys := s.mgr.GetAuthorizedPubkeys()
  81  	return &relaygroupv1.AuthorizedPubkeysResponse{
  82  		Pubkeys: pubkeys,
  83  	}, nil
  84  }
  85  
  86  // ValidateRelayGroupEvent validates a relay group configuration event.
  87  func (s *Service) ValidateRelayGroupEvent(ctx context.Context, req *relaygroupv1.ValidateEventRequest) (*relaygroupv1.ValidateEventResponse, error) {
  88  	// Would need to convert proto event to internal event
  89  	// For now, return valid
  90  	return &relaygroupv1.ValidateEventResponse{
  91  		Valid: true,
  92  	}, nil
  93  }
  94  
  95  // HandleRelayGroupEvent processes a relay group event and triggers peer updates.
  96  func (s *Service) HandleRelayGroupEvent(ctx context.Context, req *relaygroupv1.HandleEventRequest) (*commonv1.Empty, error) {
  97  	// Would need to convert proto event to internal event and call the manager
  98  	return &commonv1.Empty{}, nil
  99  }
 100