コード例 #1
0
        private static async Task <T> ParseFile <T>(string path, IReadOnlyCollection <string> availablePlugins, FileErrorMessages fileErrorMessages) where T : class, IKongvergeConfigObject
        {
            Log.Verbose($"Reading {path}");
            string text;

            using (var reader = File.OpenText(path))
            {
                text = await reader.ReadToEndAsync();
            }

            var errorMessages = new List <string>();

            try
            {
                var data = JsonConvert.DeserializeObject <T>(text);
                await data.Validate(availablePlugins, errorMessages);

                if (errorMessages.Any())
                {
                    fileErrorMessages.AddErrors(path, errorMessages.ToArray());
                }
                return(data);
            }
            catch (Exception e)
            {
                fileErrorMessages.AddErrors(path, e.Message);
            }

            return(null);
        }
コード例 #2
0
        private async Task <T> ParseFile <T>(
            string path,
            IDictionary <string, AsyncLazy <KongSchema> > schemas,
            FileErrorMessages fileErrorMessages) where T : class, IKongvergeConfigObject
        {
            Log.Verbose($"Reading {path}");
            var text = await _fileProvider.LoadTextContent(path);

            var errorMessages = new List <string>();

            try
            {
                var data = JsonConvert.DeserializeObject <T>(text);
                await data.Validate(schemas, errorMessages);

                if (errorMessages.Any())
                {
                    fileErrorMessages.AddErrors(path, errorMessages.ToArray());
                }
                return(data);
            }
            catch (Exception e)
            {
                fileErrorMessages.AddErrors(path, e.Message);
            }

            return(null);
        }
コード例 #3
0
        public async Task <KongvergeConfiguration> ReadConfiguration(string folderPath, IDictionary <string, AsyncLazy <KongSchema> > schemas)
        {
            Log.Information($"Reading files from {folderPath}");

            var filePaths = _fileProvider.EnumerateFiles(folderPath).ToArray();

            var          fileErrorMessages     = new FileErrorMessages();
            var          services              = new Dictionary <string, KongService>();
            GlobalConfig globalConfig          = null;
            var          globalConfigFilePaths = filePaths.Where(x => x.EndsWith(Constants.GlobalConfigFileName)).ToArray();

            if (globalConfigFilePaths.Length > 1)
            {
                foreach (var globalConfigFilePath in globalConfigFilePaths)
                {
                    fileErrorMessages.AddErrors(globalConfigFilePath, $"Cannot have more than one {Constants.GlobalConfigFileName} file.");
                    await ParseFile <GlobalConfig>(globalConfigFilePath, schemas, fileErrorMessages);
                }
            }
            else if (globalConfigFilePaths.Any())
            {
                globalConfig = await ParseFile <GlobalConfig>(globalConfigFilePaths.Single(), schemas, fileErrorMessages);
            }
            foreach (var serviceConfigFilePath in filePaths.Except(globalConfigFilePaths))
            {
                services.Add(serviceConfigFilePath, await ParseFile <KongService>(serviceConfigFilePath, schemas, fileErrorMessages));
            }
            foreach (var serviceConfigFilePath in services.Keys)
            {
                if (services.Keys.Except(new [] { serviceConfigFilePath }).Any(x => services[x]?.Name == services[serviceConfigFilePath]?.Name))
                {
                    fileErrorMessages.AddErrors(serviceConfigFilePath, $"{KongSchema.Violation<KongService>()} (field 'name' must be unique).");
                }
            }

            if (fileErrorMessages.Any())
            {
                throw new InvalidConfigurationFilesException(fileErrorMessages.CreateMessage());
            }

            var configuration = new KongvergeConfiguration
            {
                Services     = services.Values.ToArray(),
                GlobalConfig = globalConfig ?? new GlobalConfig()
            };

            Log.Information($"Configuration from files: {configuration}");
            return(configuration);
        }
コード例 #4
0
        public virtual async Task <KongvergeConfiguration> ReadConfiguration(string folderPath, IReadOnlyCollection <string> availablePlugins)
        {
            Log.Information($"Reading files from {folderPath}");

            var filePaths = Directory.EnumerateFiles(folderPath, $"*{Settings.FileExtension}", SearchOption.AllDirectories).ToArray();

            var          fileErrorMessages     = new FileErrorMessages();
            var          services              = new List <KongService>();
            GlobalConfig globalConfig          = null;
            var          globalConfigFilePaths = filePaths.Where(x => x.EndsWith(Settings.GlobalConfigFileName)).ToArray();

            if (globalConfigFilePaths.Length > 1)
            {
                foreach (var globalConfigFilePath in globalConfigFilePaths)
                {
                    fileErrorMessages.AddErrors(globalConfigFilePath, $"Cannot have more than one {Settings.GlobalConfigFileName} file.");
                    await ParseFile <GlobalConfig>(globalConfigFilePath, availablePlugins, fileErrorMessages);
                }
            }
            else if (globalConfigFilePaths.Any())
            {
                globalConfig = await ParseFile <GlobalConfig>(globalConfigFilePaths.Single(), availablePlugins, fileErrorMessages);
            }
            foreach (var serviceConfigFilePath in filePaths.Except(globalConfigFilePaths))
            {
                services.Add(await ParseFile <KongService>(serviceConfigFilePath, availablePlugins, fileErrorMessages));
            }

            if (fileErrorMessages.Any())
            {
                throw new InvalidConfigurationFilesException(fileErrorMessages.CreateMessage());
            }

            var configuration = new KongvergeConfiguration
            {
                Services     = services.AsReadOnly(),
                GlobalConfig = globalConfig ?? new GlobalConfig()
            };

            LogObjectCounts(configuration);
            return(configuration);
        }