Skip to content
Snippets Groups Projects
grpc_server.go 3.79 KiB
Newer Older
  • Learn to ignore specific revisions
  • OZGCloud's avatar
    OZGCloud committed
    /*
     * Copyright (C) 2023-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
    
    OZGCloud's avatar
    OZGCloud committed
    
    import (
    	"context"
    	pb "fachstellen-proxy/gen/go"
    
    OZGCloud's avatar
    OZGCloud committed
    	log "github.com/sirupsen/logrus"
    
    OZGCloud's avatar
    OZGCloud committed
    	"fmt"
    	"google.golang.org/grpc"
    
    OZGCloud's avatar
    OZGCloud committed
    	"google.golang.org/grpc/codes"
    	"google.golang.org/grpc/status"
    
    OZGCloud's avatar
    OZGCloud committed
    	"net"
    
    OZGCloud's avatar
    OZGCloud committed
    )
    
    
    const (
    	GrpcMockPort    = 50052
    	defaultFilePath = "internal/mock/testdata/dummy.pdf"
    	testFilePath    = "testdata/dummy.pdf"
    )
    
    
    type registrationServer struct {
    
    OZGCloud's avatar
    OZGCloud committed
    	pb.UnimplementedFachstelleRegistrationServiceServer
    }
    
    
    func (s *registrationServer) Register(_ context.Context, in *pb.GrpcFachstelleRegistrationRequest) (*pb.GrpcFachstelleRegistrationResponse, error) {
    
    OZGCloud's avatar
    OZGCloud committed
    	if in.MukId == "" {
    		return nil, status.Error(codes.InvalidArgument, "mukId is missing")
    
    OZGCloud's avatar
    OZGCloud committed
    	}
    
    
    OZGCloud's avatar
    OZGCloud committed
    	if in.MukId == "conflictMukId" {
    		return nil, status.Error(codes.AlreadyExists, "conflict mukId")
    	} else if in.MukId == "unavailableMukId" {
    		return nil, status.Error(codes.Unavailable, "unavailable mukId")
    	} else if in.MukId == "erroneousMukId" {
    		return nil, status.Error(codes.Internal, "erroneous mukId")
    
    OZGCloud's avatar
    OZGCloud committed
    	}
    
    
    OZGCloud's avatar
    OZGCloud committed
    	return &pb.GrpcFachstelleRegistrationResponse{}, nil
    }
    
    
    type collaborationServer struct {
    	pb.UnimplementedCollaborationServiceServer
    }
    
    
    func (s *collaborationServer) FindVorgang(_ context.Context, in *pb.GrpcFindVorgangRequest) (*pb.GrpcFindVorgangResponse, error) {
    
    	if in.VorgangId == "" {
    
    		return nil, status.Error(codes.InvalidArgument, "vorgangId is missing")
    
    	}
    
    	if in.SamlToken == "" {
    
    		return nil, status.Error(codes.InvalidArgument, "samlToken is missing")
    
    OZGCloud's avatar
    OZGCloud committed
    	return &pb.GrpcFindVorgangResponse{Vorgang: &pb.GrpcVorgang{Id: "testVorgangId"}}, nil
    
    func (s *collaborationServer) GetFileContent(in *pb.GrpcGetFileContentRequest, stream pb.CollaborationService_GetFileContentServer) error {
    	if in.Id == "" {
    		return status.Error(codes.InvalidArgument, "fileId is missing")
    	}
    
    	if in.SamlToken == "" {
    		return status.Error(codes.InvalidArgument, "samlToken is missing")
    	}
    
    	fp := defaultFilePath
    	if testing.Testing() {
    		fp = testFilePath
    	}
    
    	file, err := os.Open(fp)
    	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
    		}
    
    		chunk := &pb.GrpcGetFileContentResponse{FileContent: buffer[:bytesRead]}
    		if err := stream.Send(chunk); err != nil {
    			return err
    		}
    	}
    
    	return nil
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    func StartGrpcServer() *grpc.Server {
    	s := grpc.NewServer()
    
    	pb.RegisterFachstelleRegistrationServiceServer(s, &registrationServer{})
    
    	pb.RegisterCollaborationServiceServer(s, &collaborationServer{})
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", GrpcMockPort))
    
    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 %d", GrpcMockPort))
    
    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
    }