Skip to content
Snippets Groups Projects
grpc_server.go 2.89 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"
    	"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"
    )
    
    
    const GrpcMockPort = 50052
    
    type registrationServer struct {
    
    OZGCloud's avatar
    OZGCloud committed
    	pb.UnimplementedFachstelleRegistrationServiceServer
    }
    
    
    func (s *registrationServer) Register(ctx context.Context, in *pb.GrpcFachstelleRegistrationRequest) (*pb.GrpcFachstelleRegistrationResponse, error) {
    
    OZGCloud's avatar
    OZGCloud committed
    	if in.Fachstelle == nil {
    		return nil, status.Error(codes.InvalidArgument, "Fachstelle is missing")
    	}
    
    	if in.Fachstelle.MukId == "conflictMukId" {
    
    		return nil, status.Error(codes.AlreadyExists, "Conflict MukId")
    
    OZGCloud's avatar
    OZGCloud committed
    	} else if in.Fachstelle.MukId == "unavailableMukId" {
    
    		return nil, status.Error(codes.Unavailable, "Unavailable MukId")
    
    OZGCloud's avatar
    OZGCloud committed
    	} else if in.Fachstelle.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(ctx 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
    
    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
    		logger.Fatal("gRPC server failed to listen: %v", err)
    
    OZGCloud's avatar
    OZGCloud committed
    	}
    
    
    	logger.Info("gRPC server listening on port %d", GrpcMockPort)
    
    OZGCloud's avatar
    OZGCloud committed
    	if err := s.Serve(lis); err != nil {
    
    OZGCloud's avatar
    OZGCloud committed
    		logger.Fatal("gRPC server failed to serve: %v", err)
    
    OZGCloud's avatar
    OZGCloud committed
    	}
    
    	return s
    }