Skip to content
Snippets Groups Projects
grpc_server.go 4.56 KiB
Newer Older
  • Learn to ignore specific revisions
  • OZGCloud's avatar
    OZGCloud committed
    //
    // Copyright (C) 2024 Das Land Schleswig-Holstein vertreten durch den
    // Ministerpräsidenten des Landes Schleswig-Holstein
    // Staatskanzlei
    // Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
    //
    // Lizenziert unter der EUPL, Version 1.2 oder - sobald
    // diese von der Europäischen Kommission genehmigt wurden -
    // Folgeversionen der EUPL ("Lizenz");
    // Sie dürfen dieses Werk ausschließlich gemäß
    // dieser Lizenz nutzen.
    // Eine Kopie der Lizenz finden Sie hier:
    //
    // https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
    //
    // Sofern nicht durch anwendbare Rechtsvorschriften
    // gefordert oder in schriftlicher Form vereinbart, wird
    // die unter der Lizenz verbreitete Software "so wie sie
    // ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
    // ausdrücklich oder stillschweigend - verbreitet.
    // Die sprachspezifischen Genehmigungen und Beschränkungen
    // unter der Lizenz sind dem Lizenztext zu entnehmen.
    //
    
    
    OZGCloud's avatar
    OZGCloud committed
    package mock
    
    import (
    
    OZGCloud's avatar
    OZGCloud committed
        log "github.com/sirupsen/logrus"
    
    OZGCloud's avatar
    OZGCloud committed
    	pb "antragsraum-proxy/gen/go"
    
    OZGCloud's avatar
    OZGCloud committed
    	"fmt"
    	"google.golang.org/grpc"
    	"google.golang.org/grpc/codes"
    	"google.golang.org/grpc/status"
    
    OZGCloud's avatar
    OZGCloud committed
    	"io"
    
    OZGCloud's avatar
    OZGCloud committed
    	"net"
    
    OZGCloud's avatar
    OZGCloud committed
    	"os"
    
    OZGCloud's avatar
    OZGCloud committed
    	"testing"
    
    OZGCloud's avatar
    OZGCloud committed
    )
    
    
    OZGCloud's avatar
    OZGCloud committed
    const (
    	defaultFilePath = "internal/mock/testdata/dummy.pdf"
    	testFilePath    = "testdata/dummy.pdf"
    )
    
    OZGCloud's avatar
    OZGCloud committed
    
    type rueckfrageServer struct {
    
    	pb.UnimplementedAntragraumServiceServer
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    func (s *rueckfrageServer) FindRueckfragen(_ context.Context, in *pb.GrpcFindRueckfragenRequest) (*pb.GrpcFindRueckfragenResponse, error) {
    
    	if in.PostfachId == "" {
    		return nil, status.Error(codes.InvalidArgument, "Id is missing")
    
    OZGCloud's avatar
    OZGCloud committed
    	}
    
    
    	if in.PostfachId == "conflictId" {
    		return nil, status.Error(codes.AlreadyExists, "Conflict id")
    	} else if in.PostfachId == "unavailableId" {
    		return nil, status.Error(codes.Unavailable, "Unavailable Id")
    	} else if in.PostfachId == "erroneousId" {
    		return nil, status.Error(codes.Internal, "Erroneous Id")
    
    OZGCloud's avatar
    OZGCloud committed
    	}
    
    
    	return &pb.GrpcFindRueckfragenResponse{}, nil
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    func (s *rueckfrageServer) GetRueckfrage(_ context.Context, in *pb.GrpcGetRueckfrageRequest) (*pb.GrpcGetRueckfrageResponse, error) {
    	if in.Id == "" {
    		return nil, status.Error(codes.InvalidArgument, "Id is missing")
    	}
    
    	return &pb.GrpcGetRueckfrageResponse{}, nil
    }
    
    func (s *rueckfrageServer) SendRueckfrageAnswer(_ context.Context, _ *pb.GrpcSendRueckfrageAnswerRequest) (*pb.GrpcSendRueckfrageAnswerResponse, error) {
    	return &pb.GrpcSendRueckfrageAnswerResponse{}, nil
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    func (s *rueckfrageServer) GetAttachmentContent(_ *pb.GrpcGetAttachmentContentRequest, stream pb.AntragraumService_GetAttachmentContentServer) error {
    
    OZGCloud's avatar
    OZGCloud committed
    	fp := defaultFilePath
    	if testing.Testing() {
    		fp = testFilePath
    	}
    
    	file, err := os.Open(fp)
    
    OZGCloud's avatar
    OZGCloud committed
    	if err != nil {
    		return err
    	}
    	defer file.Close()
    
    	buffer := make([]byte, 1024*1024)
    	for {
    		bytesRead, err := file.Read(buffer)
    		if err == io.EOF {
    			break
    		}
    		if err != nil {
    			return err
    		}
    
    
    OZGCloud's avatar
    OZGCloud committed
    		chunk := &pb.GrpcGetAttachmentContentResponse{FileContent: buffer[:bytesRead]}
    
    OZGCloud's avatar
    OZGCloud committed
    		if err := stream.Send(chunk); err != nil {
    			return err
    		}
    	}
    
    	return nil
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    func (s *rueckfrageServer) GetAttachmentMetadata(_ context.Context, in *pb.GrpcGetAttachmentMetadataRequest) (*pb.GrpcGetAttachmentMetadataResponse, error) {
    	if in.FileId == "" {
    		return nil, status.Error(codes.InvalidArgument, "FileId is missing")
    	}
    
    	return &pb.GrpcGetAttachmentMetadataResponse{}, nil
    }
    
    type commandServer struct {
    	pb.UnimplementedCommandServiceServer
    }
    
    func (s *commandServer) GetCommand(_ context.Context, in *pb.GrpcGetCommandRequest) (*pb.GrpcCommand, error) {
    	if in.Id == "" {
    		return nil, status.Error(codes.InvalidArgument, "Id is missing")
    	}
    
    	return &pb.GrpcCommand{}, nil
    }
    
    type binaryFileServer struct {
    	pb.UnimplementedBinaryFileServiceServer
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    func (s *binaryFileServer) UploadBinaryFileAsStream(stream pb.BinaryFileService_UploadBinaryFileAsStreamServer) error {
    	for {
    		_, err := stream.Recv()
    		if err == io.EOF {
    			return stream.SendAndClose(&pb.GrpcUploadBinaryFileResponse{FileId: "testFileId"})
    		}
    
    		if err != nil {
    			return err
    		}
    	}
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    func StartGrpcServer() *grpc.Server {
    	s := grpc.NewServer()
    
    OZGCloud's avatar
    OZGCloud committed
    	pb.RegisterAntragraumServiceServer(s, &rueckfrageServer{})
    	pb.RegisterCommandServiceServer(s, &commandServer{})
    	pb.RegisterBinaryFileServiceServer(s, &binaryFileServer{})
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", conf.Grpc.Server.Port))
    
    OZGCloud's avatar
    OZGCloud committed
    	if err != nil {
    
    OZGCloud's avatar
    OZGCloud committed
    		log.Fatal(fmt.Sprintf("gRPC server failed to listen: %v", err))
    
    OZGCloud's avatar
    OZGCloud committed
    	log.Info(fmt.Sprintf("gRPC server listening on port %v", conf.Grpc.Server.Port))
    
    OZGCloud's avatar
    OZGCloud committed
    	if err := s.Serve(lis); err != nil {
    
    OZGCloud's avatar
    OZGCloud committed
    		log.Fatal(fmt.Sprintf("gRPC server failed to serve: %v", err))
    
    OZGCloud's avatar
    OZGCloud committed
    	}
    
    	return s
    }