Other: Single instance

This commit is contained in:
James Houlahan
2022-10-12 23:30:43 +02:00
parent fd63611b41
commit 593d86f3a7
16 changed files with 470 additions and 73 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt"
"net"
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/proton-bridge/v2/internal/focus/proto"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
@ -24,11 +25,12 @@ type Service struct {
server *grpc.Server
listener net.Listener
raiseCh chan struct{}
version *semver.Version
}
// NewService creates a new focus service.
// It listens on the local host and port 1042 (by default).
func NewService() (*Service, error) {
func NewService(version *semver.Version) (*Service, error) {
listener, err := net.Listen("tcp", net.JoinHostPort(Host, fmt.Sprint(Port)))
if err != nil {
return nil, fmt.Errorf("failed to listen: %w", err)
@ -38,6 +40,7 @@ func NewService() (*Service, error) {
server: grpc.NewServer(),
listener: listener,
raiseCh: make(chan struct{}, 1),
version: version,
}
proto.RegisterFocusServer(service.server, service)
@ -57,6 +60,13 @@ func (service *Service) Raise(context.Context, *emptypb.Empty) (*emptypb.Empty,
return &emptypb.Empty{}, nil
}
// Version implements the gRPC FocusService interface; it returns the version of the service.
func (service *Service) Version(context.Context, *emptypb.Empty) (*proto.VersionResponse, error) {
return &proto.VersionResponse{
Version: service.version.Original(),
}, nil
}
// GetRaiseCh returns a channel on which events are sent when the application should be raised.
func (service *Service) GetRaiseCh() <-chan struct{} {
return service.raiseCh