private static SwaggerFileConfiguration MergeFilesOfOcelotConfiguration(
            List <FileInfo> files,
            string fileOfSwaggerEndPoints,
            string environmentName)
        {
            SwaggerFileConfiguration fileConfigurationMerged = new SwaggerFileConfiguration();

            foreach (FileInfo itemFile in files)
            {
                string linesOfFile = File.ReadAllText(itemFile.FullName);
                SwaggerFileConfiguration config = JsonConvert.DeserializeObject <SwaggerFileConfiguration>(linesOfFile);

                if (CanContinue(files, itemFile))
                {
                    continue;
                }
                else if (IsGlobalConfigurationFile(environmentName, itemFile.Name, SwaggerForOcelotFileOptions.GlobalOcelotConfigFile))
                {
                    fileConfigurationMerged.GlobalConfiguration = config.GlobalConfiguration;
                }
                else if (IsGlobalConfigurationFile(environmentName, itemFile.Name, fileOfSwaggerEndPoints))
                {
                    fileConfigurationMerged.SwaggerEndPoints = config.SwaggerEndPoints;
                }

                fileConfigurationMerged.Aggregates.AddRange(config.Aggregates);
                fileConfigurationMerged.Routes.AddRange(config.Routes);
            }

            return(fileConfigurationMerged);
        }
Beispiel #2
0
        /// <summary>
        /// Extension for compatibility of functionality multifile of ocelot
        /// </summary>
        /// <param name="builder">builder of net core for call this extension</param>
        /// <param name="environment">environment of net core app</param>
        /// <param name="folder">folder of files of configuration of ocelot</param>
        /// <param name="fileOfSwaggerEndPoints">name of file of configuration SwaggerForOcelot without .json extension</param>
        /// <returns>a Object IConfigurationBuilder</returns>
        public static IConfigurationBuilder AddOcelotWithSwaggerSupport(
            this IConfigurationBuilder builder,
            IWebHostEnvironment environment = null,
            string folder = "/",
            string fileOfSwaggerEndPoints = SwaggerForOcelotFileOptions.SwaggerEndPointsConfigFile)
        {
            List <FileInfo>          files = GetListOfOcelotFiles(folder, environment?.EnvironmentName);
            SwaggerFileConfiguration fileConfigurationMerged =
                MergeFilesOfOcelotConfiguration(files, fileOfSwaggerEndPoints, environment?.EnvironmentName);
            string jsonFileConfiguration = JsonConvert.SerializeObject(fileConfigurationMerged);

            File.WriteAllText(SwaggerForOcelotFileOptions.PrimaryOcelotConfigFile, jsonFileConfiguration);

            builder.AddJsonFile(SwaggerForOcelotFileOptions.PrimaryOcelotConfigFile, optional: false, reloadOnChange: false);

            return(builder);
        }
        /// <summary>
        /// Extension for compatibility of functionality multifile of ocelot.
        /// </summary>
        /// <param name="builder">Builder of net core for call this extension.</param>
        /// <param name="action">Configuration action.</param>
        public static IConfigurationBuilder AddOcelotWithSwaggerSupport(
            this IConfigurationBuilder builder,
            Action <OcelotWithSwaggerOptions> action = null)
        {
            var options = new OcelotWithSwaggerOptions();

            action?.Invoke(options);

            List <FileInfo>          files = GetListOfOcelotFiles(options.Folder, options.HostEnvironment?.EnvironmentName);
            SwaggerFileConfiguration fileConfigurationMerged =
                MergeFilesOfOcelotConfiguration(files, options.FileOfSwaggerEndPoints, options.HostEnvironment?.EnvironmentName);
            string jsonFileConfiguration = JsonConvert.SerializeObject(fileConfigurationMerged);

            File.WriteAllText(options.PrimaryOcelotConfigFileName, jsonFileConfiguration);

            builder.AddJsonFile(options.PrimaryOcelotConfigFileName, optional: false, reloadOnChange: false);

            return(builder);
        }