Exemplo n.º 1
0
        public async Task <SwaggerDocument> Generate(OcelotSwaggerSettings settings, Configuration ocelotConfiguration)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (ocelotConfiguration == null)
            {
                throw new ArgumentNullException(nameof(ocelotConfiguration));
            }

            var swaggerDocument = new SwaggerDocument
            {
                Info = settings.Info
            };

            var documents = await GetDownstreamSwaggerDocuments(settings, ocelotConfiguration);

            var definitionKeys = new Dictionary <string, int>();

            foreach (var definition in documents.Values.SelectMany(x => x.Definitions))
            {
                if (swaggerDocument.Definitions.ContainsKey(definition.Key))
                {
                    var keyCount = definitionKeys[definition.Key];
                    swaggerDocument.Definitions.Add($"{definition.Key}_{keyCount}", definition.Value);
                    definitionKeys[definition.Key] = keyCount + 1;
                }
                else
                {
                    swaggerDocument.Definitions.Add(definition.Key, definition.Value);
                    definitionKeys.Add(definition.Key, 1);
                }
            }

            foreach (var reRoute in ocelotConfiguration.ReRoutes)
            {
                AddPath(swaggerDocument, reRoute, documents);
            }

            if (settings.SecurityDefinitions != null)
            {
                foreach (var item in settings.SecurityDefinitions)
                {
                    swaggerDocument.SecurityDefinitions.Add(item);
                }
            }

            return(swaggerDocument);
        }
Exemplo n.º 2
0
        private async Task <Dictionary <string, SwaggerDocument> > GetDownstreamSwaggerDocuments(
            OcelotSwaggerSettings settings, Configuration ocelotConfiguration)
        {
            var dic = new Dictionary <string, SwaggerDocument>();

            var downstreamHosts = ocelotConfiguration.ReRoutes
                                  .SelectMany(x => x.DownstreamHostAndPorts);

            foreach (var host in downstreamHosts)
            {
                if (dic.ContainsKey(host.ToUrl()))
                {
                    continue;
                }

                // var swaggerDocumentUrl = $"{host.ToUrl()}/swagger/v1/swagger.json";
                var swaggerDocumentUrl = string.Format(settings.Settings.SwaggerUrl, host.ToUrl());

                var downstreamSwaggerDocument = settings.DownstreamSwaggers
                                                .FirstOrDefault(x => string.Compare(x.Host, host.ToUrl(), StringComparison.OrdinalIgnoreCase) == 0);
                if (downstreamSwaggerDocument != null)
                {
                    swaggerDocumentUrl = downstreamSwaggerDocument.SwaggerUrl;
                }

                try
                {
                    var document = await SwaggerDocument.FromUrlAsync(swaggerDocumentUrl);

                    dic.Add(host.ToUrl(), document);
                }
                catch (Exception ex)
                {
                    Log(ex);
                }
            }

            return(dic);
        }
Exemplo n.º 3
0
            private void OnExecute(CommandLineApplication app, IConsole console)
            {
                #region 验证

                var deleteFile = false;
                if (File.Exists(SavePath))
                {
                    deleteFile = Prompt.GetYesNo($"{SavePath}{Environment.NewLine}文件已经存在,您确认覆盖当前文件吗?", false, ConsoleColor.Yellow);
                    if (!deleteFile)
                    {
                        return;
                    }
                }

                if (!File.Exists(ConfigPath))
                {
                    console.ForegroundColor = ConsoleColor.Yellow;
                    console.WriteLine("生成 Ocelot Swagger 文档的配置文件不存在。");
                    console.WriteLine(ConfigPath);
                    console.ResetColor();
                    return;
                }

                if (!File.Exists(OcelotPath))
                {
                    console.ForegroundColor = ConsoleColor.Yellow;
                    console.WriteLine("Ocelot 配置文件不存在。");
                    console.WriteLine(OcelotPath);
                    console.ResetColor();
                    return;
                }

                #endregion

                var generator = new OcelotSwaggerGenerator()
                {
                    ThrowException = false
                };

                var settings      = OcelotSwaggerSettings.FromFile(ConfigPath);
                var configuration = Configuration.FromFile(OcelotPath);

                var doc = generator.Generate(settings, configuration).GetAwaiter().GetResult();

                try
                {
                    var json = doc.ToJson();

                    if (deleteFile)
                    {
                        File.Delete(SavePath);
                    }

                    using (var sw = new StreamWriter(SavePath))
                    {
                        sw.Write(json);
                    }

                    console.ForegroundColor = ConsoleColor.Green;
                    console.WriteLine("已完成 Swagger 文档生成,请查看:");
                    console.WriteLine(SavePath);
                    console.ResetColor();
                }
                catch (Exception ex)
                {
                    console.ForegroundColor = ConsoleColor.Red;
                    console.WriteLine(ex.Message);
                    console.WriteLine("生成失败,请查看日志。");
                    console.ResetColor();
                }
            }
        public async Task <SwaggerDocument> Generate(OcelotSwaggerSettings settings, Configuration ocelotConfiguration)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (ocelotConfiguration == null)
            {
                throw new ArgumentNullException(nameof(ocelotConfiguration));
            }

            var swaggerDocument = new SwaggerDocument
            {
                Info = settings.Info
            };

            var documents = await GetDownstreamSwaggerDocuments(ocelotConfiguration);

            var definitionKeys = new Dictionary <string, int>();

            foreach (var valuePair in documents.Values.SelectMany(m => m.Paths))
            {
                if (swaggerDocument.Paths.Keys.Contains(valuePair.Key.ToString()))
                {
                    continue;
                }
                else
                {
                    swaggerDocument.Paths.Add(valuePair.Key, valuePair.Value);
                }
            }

            foreach (var definition in documents.Values.SelectMany(x => x.Definitions))
            {
                if (swaggerDocument.Definitions.ContainsKey(definition.Key))
                {
                    continue;
                }
                else
                {
                    swaggerDocument.Definitions.Add(definition.Key, definition.Value);
                    // definitionKeys.Add(definition.Key, 1);
                }
            }

//            foreach (var reRoute in ocelotConfiguration.ReRoutes)
//            {
//                AddPath(swaggerDocument, reRoute, documents);
//            }

            if (settings.SecurityDefinitions != null)
            {
                foreach (var item in settings.SecurityDefinitions)
                {
                    swaggerDocument.SecurityDefinitions.Add(item);
                }
            }

            return(swaggerDocument);
        }