Exemplo n.º 1
0
        public static bool Initialize(
            out BServiceInitializer _Result,
            string[][] _RequiredExtraEnvVars = null)
        {
            var Instance = new BServiceInitializer();

            _Result = null;

            Instance.LoggingService = new BLoggingServiceBasic();

            var RequiredEnvVarKeys = new List <string[]>()
            {
                new string[] { "PORT" },
                new string[] { "PROGRAM_ID" }
            };

            if (_RequiredExtraEnvVars != null)
            {
                RequiredEnvVarKeys.AddRange(_RequiredExtraEnvVars);
            }

            /*
             * Getting environment variables
             */
            if (!BUtility.GetEnvironmentVariables(out Instance._RequiredEnvironmentVariables,
                                                  RequiredEnvVarKeys.ToArray(),
                                                  (string Message) =>
            {
                Instance.LoggingService.WriteLogs(BLoggingServiceMessageUtility.Single(EBLoggingServiceLogType.Critical, Message), Instance.ProgramID, "Initialization");
            }))
            {
                return(false);
            }

            Instance.ProgramID = Instance.RequiredEnvironmentVariables["PROGRAM_ID"];

            /*
             * Parsing http server port
             */
            if (!int.TryParse(Instance.RequiredEnvironmentVariables["PORT"], out int _ServPort))
            {
                Instance.LoggingService.WriteLogs(BLoggingServiceMessageUtility.Single(EBLoggingServiceLogType.Critical, "Given http server port is invalid."), Instance.ProgramID, "Initialization");
                return(false);
            }
            Instance.ServerPort = _ServPort;

            _Result = Instance;
            return(true);
        }
Exemplo n.º 2
0
        public static bool RunTests()
        {
            if (!BUtility.GetEnvironmentVariables(out Dictionary <string, string> RequiredEnvVars,
                                                  new string[][]
            {
                new string[] { "GC_PROJECT_ID" }
            },
                                                  Console.WriteLine))
            {
                return(false);
            }

            /*
             * Logging Services tests
             */
            var LoggingTests_GC = new BLoggingServiceTest(
                new BLoggingServiceGC(
                    RequiredEnvVars["GC_PROJECT_ID"],
                    Console.WriteLine),
                Console.WriteLine);

            if (!LoggingTests_GC.Start())
            {
                return(false);
            }

            var LoggingTests_AWS = new BLoggingServiceTest(
                new BLoggingServiceAWS(
                    RequiredEnvVars["AWS_ACCESS_KEY"],
                    RequiredEnvVars["AWS_SECRET_KEY"],
                    RequiredEnvVars["AWS_REGION"],
                    Console.WriteLine),
                Console.WriteLine);

            if (!LoggingTests_AWS.Start())
            {
                return(false);
            }

            var LoggingTests_Azure = new BLoggingServiceTest(
                new BLoggingServiceAzure(
                    RequiredEnvVars["APPINSIGHTS_INSTRUMENTATIONKEY"],
                    Console.WriteLine),
                Console.WriteLine);

            if (!LoggingTests_Azure.Start())
            {
                return(false);
            }

            /*
             * E-mail Services tests
             */
            var Comparator = new BRelativeTestsResultComparator(Console.WriteLine);

            var MailTests_SendGrid = new BEmailServicesTest(
                new BMailServiceSendGrid(
                    RequiredEnvVars["SENDGRID_API_KEY"],
                    "*****@*****.**",
                    "BTest",
                    Console.WriteLine),
                Comparator.AddLine);

            if (!MailTests_SendGrid.Start())
            {
                return(false);
            }

            /*
             * Database Services tests
             */
            Comparator = new BRelativeTestsResultComparator(Console.WriteLine);

            var DBTests_GC = new BDatabaseServicesTest(
                new BDatabaseServiceGC(
                    RequiredEnvVars["GC_PROJECT_ID"],
                    Console.WriteLine),
                "BTest",
                "TestKey",
                Comparator.AddLine);

            if (!DBTests_GC.Start())
            {
                return(false);
            }

            Comparator.Next();

            var DBTests_AWS = new BDatabaseServicesTest(
                new BDatabaseServiceAWS(
                    RequiredEnvVars["AWS_ACCESS_KEY"],
                    RequiredEnvVars["AWS_SECRET_KEY"],
                    RequiredEnvVars["AWS_REGION"],
                    Console.WriteLine),
                "BTest",
                "TestKey",
                Comparator.AddLine);

            if (!DBTests_AWS.Start())
            {
                return(false);
            }

            Comparator.Next();

            var DBTests_MongoDB = new BDatabaseServicesTest(
                new BDatabaseServiceMongoDB(
                    RequiredEnvVars["MONGO_DB_CONNECTION_STRING"],
                    RequiredEnvVars["MONGO_DB_DATABASE"],
                    Console.WriteLine),
                "BTest",
                "TestKey",
                Comparator.AddLine);

            if (!DBTests_MongoDB.Start())
            {
                return(false);
            }

            if (!Comparator.Compare())
            {
                return(false);
            }

            /*
             * Memory and Pub/Sub Services tests
             */
            Comparator = new BRelativeTestsResultComparator(Console.WriteLine);

            var MemTests_WithRedisPubSub = new BMemoryPubSubServiceTest(
                new BMemoryServiceRedis(
                    RequiredEnvVars["REDIS_ENDPOINT"],
                    int.Parse(RequiredEnvVars["REDIS_PORT"]),
                    RequiredEnvVars["REDIS_PASSWORD"],
                    bool.Parse(RequiredEnvVars["REDIS_SSL_ENABLED"]),
                    new BPubSubServiceRedis(
                        RequiredEnvVars["REDIS_ENDPOINT"],
                        int.Parse(RequiredEnvVars["REDIS_PORT"]),
                        RequiredEnvVars["REDIS_PASSWORD"],
                        bool.Parse(RequiredEnvVars["REDIS_SSL_ENABLED"]),
                        true,
                        Console.WriteLine),
                    true,
                    Console.WriteLine),
                Comparator.AddLine);

            if (!MemTests_WithRedisPubSub.Start())
            {
                return(false);
            }

            Comparator.Next();

            var MemTests_WithGCPubSub = new BMemoryPubSubServiceTest(
                new BMemoryServiceRedis(
                    RequiredEnvVars["REDIS_ENDPOINT"],
                    int.Parse(RequiredEnvVars["REDIS_PORT"]),
                    RequiredEnvVars["REDIS_PASSWORD"],
                    bool.Parse(RequiredEnvVars["REDIS_SSL_ENABLED"]),
                    new BPubSubServiceGC(
                        RequiredEnvVars["GC_PROJECT_ID"],
                        Console.WriteLine),
                    true,
                    Console.WriteLine),
                Comparator.AddLine);

            if (!MemTests_WithGCPubSub.Start())
            {
                return(false);
            }

            if (!Comparator.Compare())
            {
                return(false);
            }

            Comparator.Next();

            var MemTests_WithAWSPubSub = new BMemoryPubSubServiceTest(
                new BMemoryServiceRedis(
                    RequiredEnvVars["REDIS_ENDPOINT"],
                    int.Parse(RequiredEnvVars["REDIS_PORT"]),
                    RequiredEnvVars["REDIS_PASSWORD"],
                    bool.Parse(RequiredEnvVars["REDIS_SSL_ENABLED"]),
                    new BPubSubServiceAWS(
                        RequiredEnvVars["AWS_ACCESS_KEY"],
                        RequiredEnvVars["AWS_SECRET_KEY"],
                        RequiredEnvVars["AWS_REGION"],
                        Console.WriteLine),
                    true,
                    Console.WriteLine),
                Comparator.AddLine);

            if (!MemTests_WithAWSPubSub.Start())
            {
                return(false);
            }

            if (!Comparator.Compare())
            {
                return(false);
            }

            Comparator.Next();

            var MemTests_WithAzurePubSub = new BMemoryPubSubServiceTest(
                new BMemoryServiceRedis(
                    RequiredEnvVars["REDIS_ENDPOINT"],
                    int.Parse(RequiredEnvVars["REDIS_PORT"]),
                    RequiredEnvVars["REDIS_PASSWORD"],
                    bool.Parse(RequiredEnvVars["REDIS_SSL_ENABLED"]),
                    new BPubSubServiceAzure(
                        RequiredEnvVars["AZURE_CLIENT_ID"],
                        RequiredEnvVars["AZURE_CLIENT_SECRET"],
                        RequiredEnvVars["AZURE_TENANT_ID"],
                        RequiredEnvVars["AZURE_NAMESPACE_ID"],
                        RequiredEnvVars["AZURE_NAMESPACE_CONNSTR"],
                        Console.WriteLine),
                    true,
                    Console.WriteLine),
                Comparator.AddLine);

            if (!MemTests_WithAWSPubSub.Start())
            {
                return(false);
            }

            if (!Comparator.Compare())
            {
                return(false);
            }

            /*
             * File Services tests
             */
            Comparator = new BRelativeTestsResultComparator(Console.WriteLine);

            var FSTests_GC = new BFileServiceTest(
                new BFileServiceGC(
                    RequiredEnvVars["GC_PROJECT_ID"],
                    Console.WriteLine),
                RequiredEnvVars["FILESERVICE_BUCKET"],
                RequiredEnvVars["FILESERVICE_REMOTE_PATH"],
                RequiredEnvVars["FILESERVICE_TEST_FILE_LOCAL_PATH"],
                Comparator.AddLine);

            if (!FSTests_GC.Start())
            {
                return(false);
            }

            Comparator.Next();

            var FSTests_AWS = new BFileServiceTest(
                new BFileServiceAWS(
                    RequiredEnvVars["AWS_ACCESS_KEY"],
                    RequiredEnvVars["AWS_SECRET_KEY"],
                    RequiredEnvVars["AWS_REGION"],
                    Console.WriteLine),
                RequiredEnvVars["FILESERVICE_BUCKET"],
                RequiredEnvVars["FILESERVICE_REMOTE_PATH"],
                RequiredEnvVars["FILESERVICE_TEST_FILE_LOCAL_PATH"],
                Comparator.AddLine);

            if (!FSTests_AWS.Start())
            {
                return(false);
            }

            Comparator.Next();

            var FSTests_AZ = new BFileServiceTest(
                new BFileServiceAZ(
                    RequiredEnvVars["AZ_STORAGE_SERVICE"],
                    RequiredEnvVars["AZ_STORAGE_ACCOUNT"],
                    RequiredEnvVars["AZ_STORAGE_ACCOUNT_KEY"],
                    RequiredEnvVars["AZ_STORAGE_RESOURCE_GROUP"],
                    RequiredEnvVars["AZ_STORAGE_MANAGEMENT_APP_ID"],
                    RequiredEnvVars["AZ_STORAGE_MANAGEMENT_SECRET"],
                    RequiredEnvVars["AZ_SUBSCRIPTION_ID"],
                    RequiredEnvVars["AZ_TENANT_ID"],
                    RequiredEnvVars["AZ_STORAGE_LOCATION"],
                    Console.WriteLine),
                RequiredEnvVars["FILESERVICE_BUCKET"],
                RequiredEnvVars["FILESERVICE_REMOTE_PATH"],
                RequiredEnvVars["FILESERVICE_TEST_FILE_LOCAL_PATH"],
                Comparator.AddLine);

            if (!FSTests_AWS.Start())
            {
                return(false);
            }

            if (!Comparator.Compare())
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 3
0
        static int Main()
        {
            Console.WriteLine("Initializing the service...");

#if (Debug || DEBUG)
            if (!ServicesDebugOnlyUtilities.CalledFromMain())
            {
                return(1);
            }
#endif

            // In case of a cloud component dependency or environment variable is added/removed;
            // Relative terraform script and microservice-dependency-map.cs must be updated as well.

            if (!BUtility.GetEnvironmentVariables(out Dictionary <string, string> EnvironmentVariables,
                                                  new string[][]
            {
                new string[] { "PORT" },
                new string[] { "PROGRAM_ID" },

                new string[] { "API_GATEWAY_SERVICE_BASE_URL" },

                new string[] { "STATIC_STATE_BUCKET" },     //Currently set to ignore on local debugging sessions; since it is only used for maintenance check

                new string[] { "DEPLOYMENT_BRANCH_NAME" },
                new string[] { "DEPLOYMENT_BUILD_NUMBER" }
            },
                                                  Console.WriteLine))
            {
                return(1);
            }

            Resources_DeploymentManager.Get().SetDeploymentBranchNameAndBuildNumber(EnvironmentVariables["DEPLOYMENT_BRANCH_NAME"], EnvironmentVariables["DEPLOYMENT_BUILD_NUMBER"]);

            if (!int.TryParse(EnvironmentVariables["PORT"], out int ServerPort))
            {
                Console.WriteLine("Invalid PORT environment variable; it must be an integer.");
                return(1);
            }

            string ApiGatewayServiceEndpoint = EnvironmentVariables["API_GATEWAY_SERVICE_BASE_URL"];

            string MaintenanceModeCheckUrl = "https://storage.googleapis.com/" + EnvironmentVariables["STATIC_STATE_BUCKET"] + "/maintenance_mode_" + Resources_DeploymentManager.Get().GetDeploymentBranchName();

            var DeploymentLoweredBranchName = Resources_DeploymentManager.Get().GetDeploymentBranchNameEscapedLoweredWithDash();
            if (DeploymentLoweredBranchName == "master" || DeploymentLoweredBranchName == "development")
            {
                //If the branch is feature, bugfix, hotfix etc. do not check for maintenance.
                MaintenanceChecker.Get().Start(MaintenanceModeCheckUrl, Console.WriteLine);
            }

            /*
             * Web-http service initialization
             */
            var WebServiceEndpoints = new List <BWebPrefixStructure>()
            {
                new BWebPrefixStructure(new string[] { "*" }, () => new PassThroughRequest(ApiGatewayServiceEndpoint))
            };
            var BWebService = new BWebService(WebServiceEndpoints.ToArray(), ServerPort);
            BWebService.Run((string Message) =>
            {
                Console.WriteLine(Message);
            });

            /*
             * Make main thread sleep forever
             */
            Thread.Sleep(Timeout.Infinite);

            return(0);
        }