Skip to content
Snippets Groups Projects
grpc_server.go 2.44 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
    
    import (
    
        log "github.com/sirupsen/logrus"
    
    OZGCloud's avatar
    OZGCloud committed
    	"context"
    	"fmt"
    	"google.golang.org/grpc"
    	"google.golang.org/grpc/codes"
    	"google.golang.org/grpc/status"
    
    OZGCloud's avatar
    OZGCloud committed
    	pb "info-manager-proxy/gen/go"
    
    OZGCloud's avatar
    OZGCloud committed
    	"net"
    )
    
    const GrpcMockPort = 50051
    
    type server struct {
    	pb.UnimplementedInformationServiceServer
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    func (s *server) GetInformation(_ context.Context, in *pb.GrpcInformationRequest) (*pb.GrpcInformationResponse, error) {
    	if in.PostfachId == "" {
    		return nil, status.Error(codes.InvalidArgument, "PostfachId is missing")
    
    OZGCloud's avatar
    OZGCloud committed
    	if in.PostfachId == "service_unavailable" {
    		return nil, status.Error(codes.Unavailable, "Unavailable")
    	} else if in.PostfachId == "internal_server_error" {
    		return nil, status.Error(codes.Internal, "Error")
    	}
    
    	return &pb.GrpcInformationResponse{}, nil
    }
    
    func (s *server) GetInformationById(_ context.Context, in *pb.GrpcInformationByIdRequest) (*pb.GrpcInformationByIdResponse, error) {
    
    OZGCloud's avatar
    OZGCloud committed
    	if in.Id == "" {
    		return nil, status.Error(codes.InvalidArgument, "Id is missing")
    	}
    
    	return &pb.GrpcInformationByIdResponse{}, nil
    }
    
    func StartGrpcServer() *grpc.Server {
    	s := grpc.NewServer()
    	pb.RegisterInformationServiceServer(s, &server{})
    
    	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", GrpcMockPort))
    	if err != nil {
    
    		log.Fatal(fmt.Sprintf("gRPC server failed to listen: %v", err))
    
    	log.Info(fmt.Sprintf("gRPC server listening on port %v", GrpcMockPort))
    
    OZGCloud's avatar
    OZGCloud committed
    	if err := s.Serve(lis); err != nil {
    
    		log.Fatal(fmt.Sprintf("gRPC server failed to serve: %v", err))