Esempio n. 1
0
        // load secrets
        private static void LoadSecrets(string secretsVolume)
        {
            if (InMemory)
            {
                Secrets = new Secrets
                {
                    UseInMemoryDb    = true,
                    CosmosCollection = "movies",
                    CosmosDatabase   = "imdb",
                    CosmosKey        = "in-memory",
                    CosmosServer     = "in-memory",
                };
            }
            else
            {
                Secrets = Secrets.GetSecretsFromVolume(secretsVolume);

                // set the Cosmos server name for logging
                CosmosName = Secrets.CosmosServer.Replace("https://", string.Empty, StringComparison.OrdinalIgnoreCase).Replace("http://", string.Empty, StringComparison.OrdinalIgnoreCase);

                int ndx = CosmosName.IndexOf('.', StringComparison.OrdinalIgnoreCase);

                if (ndx > 0)
                {
                    CosmosName = CosmosName.Remove(ndx);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Run the app
        /// </summary>
        /// <param name="secretsVolume">k8s Secrets Volume Path</param>
        /// <param name="logLevel">Log Level</param>
        /// <param name="dryRun">Dry Run flag</param>
        /// <param name="inMemory">Use in-memory DB</param>
        /// <returns>status</returns>
        public static async Task <int> RunApp(string secretsVolume, LogLevel logLevel, bool dryRun, bool inMemory)
        {
            try
            {
                Region  = Environment.GetEnvironmentVariable("Region");
                Zone    = Environment.GetEnvironmentVariable("Zone");
                PodType = Environment.GetEnvironmentVariable("PodType");

                if (string.IsNullOrEmpty(PodType))
                {
                    PodType = inMemory ? "ngsa-memory" : "ngsa-cosmos";
                }

                if (inMemory)
                {
                    Secrets = new Secrets
                    {
                        UseInMemoryDb    = true,
                        AppInsightsKey   = string.Empty,
                        CosmosCollection = "movies",
                        CosmosDatabase   = "imdb",
                        CosmosKey        = "in-memory",
                        CosmosServer     = "in-memory",
                    };
                }
                else
                {
                    Secrets = Secrets.GetSecretsFromVolume(secretsVolume);

                    // set the Cosmos server name for logging
                    CosmosName = Secrets.CosmosServer.Replace("https://", string.Empty, StringComparison.OrdinalIgnoreCase).Replace("http://", string.Empty, StringComparison.OrdinalIgnoreCase);

                    // todo - get this from cosmos query
                    CosmosQueryId = "notImplemented";

                    int ndx = CosmosName.IndexOf('.', StringComparison.OrdinalIgnoreCase);

                    if (ndx > 0)
                    {
                        CosmosName = CosmosName.Remove(ndx);
                    }
                }

                // setup ctl c handler
                ctCancel = SetupCtlCHandler();

                AppLogLevel = logLevel;

                // load the cache
                CacheDal = new DataAccessLayer.InMemoryDal();

                // create the cosomos data access layer
                if (App.Secrets.UseInMemoryDb)
                {
                    CosmosDal = CacheDal;
                }
                else
                {
                    CosmosDal = new DataAccessLayer.CosmosDal(new Uri(Secrets.CosmosServer), Secrets.CosmosKey, Secrets.CosmosDatabase, Secrets.CosmosCollection);
                }

                // build the host
                host = BuildHost();

                if (host == null)
                {
                    return(-1);
                }

                // don't start the web server
                if (dryRun)
                {
                    return(DoDryRun());
                }

                // log startup messages
                LogStartup();

                // start the webserver
                Task w = host.RunAsync();

                // start request count timer
                Middleware.Logger.StartCounterTime(10000, 5000);

                // this doesn't return except on ctl-c
                await w.ConfigureAwait(false);

                // if not cancelled, app exit -1
                return(ctCancel.IsCancellationRequested ? 0 : -1);
            }
            catch (Exception ex)
            {
                // end app on error
                if (logger != null)
                {
                    logger.LogError($"Exception: {ex}");
                }
                else
                {
                    Console.WriteLine($"Error in Main() {ex.Message}");
                }

                return(-1);
            }
        }