public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            if (AppConfig.SwaggerOptions != null)
            {
                services.AddSwaggerGen(options =>
                {
                    options.SwaggerDoc(AppConfig.SwaggerOptions.Version, AppConfig.SwaggerOptions);
                    options.GenerateSwaggerDoc(_serviceEntryProvider.GetALLEntries());
                    options.DocInclusionPredicateV2((docName, apiDesc) =>
                    {
                        if (docName == AppConfig.SwaggerOptions.Version)
                        {
                            return(true);
                        }
                        var assembly = apiDesc.Type.Assembly;

                        var title = assembly
                                    .GetCustomAttributes(true)
                                    .OfType <AssemblyTitleAttribute>();

                        return(title.Any(v => v.Title == docName));
                    });
                    var xmlPaths = _serviceSchemaProvider.GetSchemaFilesPath();
                    foreach (var xmlPath in xmlPaths)
                    {
                        options.IncludeXmlComments(xmlPath);
                    }
                });
            }
        }
Example #2
0
        public async Task <string> InitAllActions()
        {
            var entries = _serviceEntryProvider.GetALLEntries();
            var actions = entries.Select(p => new InitActionActionInput
            {
                ServiceId           = p.Descriptor.Id,
                ServiceHost         = GetServiceHost(p.Type.FullName),
                Application         = GetApplication(p.Type.FullName),
                WebApi              = p.RoutePath,
                Method              = string.Join(",", p.Methods),
                Name                = p.Descriptor.GetMetadata <string>("GroupName"),
                DisableNetwork      = p.Descriptor.GetMetadata <bool>("DisableNetwork"),
                EnableAuthorization = p.Descriptor.GetMetadata <bool>("EnableAuthorization"),
                AllowPermission     = p.Descriptor.GetMetadata <bool>("AllowPermission"),
                Developer           = p.Descriptor.GetMetadata <string>("Director"),
                Date                = GetDevDate(p.Descriptor.GetMetadata <string>("Date"))
            }).ToList();

            try
            {
                await _actionDomainService.InitActions(actions);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
            }

            return($"根据主机服务条目更新服务功能列表成功,一共有{actions.Count}个服务条目");
        }
        public SwaggerDocument GetSwagger(
            string documentName,
            string host      = null,
            string basePath  = null,
            string[] schemes = null)
        {
            if (!_options.SwaggerDocs.TryGetValue(documentName, out Info info))
            {
                throw new UnknownSwaggerDocument(documentName);
            }

            var entry = _serviceEntryProvider.GetALLEntries();

            var schemaRegistry = _schemaRegistryFactory.Create();

            var swaggerDoc = new SwaggerDocument
            {
                Info                = info,
                Host                = host,
                BasePath            = basePath,
                Schemes             = schemes,
                Paths               = CreatePathItems(entry, schemaRegistry),
                Definitions         = schemaRegistry.Definitions,
                SecurityDefinitions = _options.SecurityDefinitions.Any() ? _options.SecurityDefinitions : null,
                Security            = _options.SecurityRequirements.Any() ? _options.SecurityRequirements : null
            };

            return(swaggerDoc);
        }
Example #4
0
        /// <summary>
        /// Initializes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void Configure(ApplicationInitializationContext context)
        {
            var app    = context.GetApplicationBuilder();
            var config = AppConfig.SwaggerConfig.Info ?? AppConfig.SwaggerOptions;

            if (config != null)
            {
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    var areaName = AppConfig.SwaggerConfig.Options?.IngressName;
                    c.SwaggerEndpoint($"../swagger/{config.Version}/swagger.json", config.Title, areaName);
                    c.SwaggerEndpoint(_serviceEntryProvider.GetALLEntries(), areaName);
                });
            }
        }
Example #5
0
        public override void Initialize(IApplicationBuilder builder)
        {
            var info = AppConfig.SwaggerConfig.Info == null
          ? AppConfig.SwaggerOptions : AppConfig.SwaggerConfig.Info;

            if (info != null)
            {
                builder.UseSwagger();
                builder.UseSwaggerUI(c =>
                {
                    var areaName = AppConfig.SwaggerConfig.Options?.IngressName;
                    c.SwaggerEndpoint($"/swagger/{info.Version}/swagger.json", info.Title, areaName);
                    c.SwaggerEndpoint(_serviceEntryProvider.GetALLEntries(), areaName);
                });
            }
        }
Example #6
0
        public IEnumerable <string> GetSchemaFilesPath()
        {
            var result         = new List <string>();
            var assemblieFiles = _serviceEntryProvider.GetALLEntries()
                                 .Select(p => p.Type.Assembly.Location).Distinct();

            foreach (var assemblieFile in assemblieFiles)
            {
                var fileSpan = assemblieFile.AsSpan();
                var path     = $"{fileSpan.Slice(0, fileSpan.LastIndexOf(".")).ToString()}.xml";
                if (File.Exists(path))
                {
                    result.Add(path);
                }
            }
            var annotationxmldir = AppConfig.SwaggerConfig.Options?.AnnotationXmlDir;

            if (!string.IsNullOrEmpty(annotationxmldir) && Directory.Exists(annotationxmldir))
            {
                var annotationxmls = Directory.GetFiles(annotationxmldir, "*.xml");
                if (annotationxmls.Any())
                {
                    foreach (var annotationxml in annotationxmls)
                    {
                        result.Add(annotationxml);
                    }
                }
            }
            return(result);
        }
Example #7
0
        public OpenApiDocument GetSwagger(string documentName, string host = null, string basePath = null)
        {
            if (!_options.SwaggerDocs.TryGetValue(documentName, out OpenApiInfo info))
            {
                throw new UnknownSwaggerDocument(documentName, _options.SwaggerDocs.Select(d => d.Key));
            }

            var applicableApiDescriptions = _apiDescriptionsProvider.ApiDescriptionGroups.Items
                                            .SelectMany(group => group.Items)
                                            .Where(apiDesc => !(_options.IgnoreObsoleteActions && apiDesc.CustomAttributes().OfType <ObsoleteAttribute>().Any()))
                                            .Where(apiDesc => _options.DocInclusionPredicate(documentName, apiDesc));

            var schemaRepository = new SchemaRepository(documentName);
            var entries          = _serviceEntryProvider.GetALLEntries();
            var mapRoutePaths    = AppConfig.SwaggerConfig.Options?.MapRoutePaths;

            if (mapRoutePaths != null)
            {
                foreach (var path in mapRoutePaths)
                {
                    var entry = entries.Where(p => p.RoutePath == path.SourceRoutePath).FirstOrDefault();
                    if (entry != null)
                    {
                        entry.RoutePath            = path.TargetRoutePath;
                        entry.Descriptor.RoutePath = path.TargetRoutePath;
                    }
                }
            }
            entries = entries
                      .Where(apiDesc => _options.DocInclusionPredicateV2(documentName, apiDesc));
            try
            {
                var swaggerDoc = new OpenApiDocument
                {
                    Info       = info,
                    Servers    = GenerateServers(host, basePath),
                    Paths      = GeneratePaths(entries, schemaRepository),
                    Components = new OpenApiComponents
                    {
                        Schemas         = schemaRepository.Schemas,
                        SecuritySchemes = new Dictionary <string, OpenApiSecurityScheme>(_options.SecuritySchemes)
                    },
                    SecurityRequirements = new List <OpenApiSecurityRequirement>(_options.SecurityRequirements)
                };

                var filterContext = new DocumentFilterContext(applicableApiDescriptions, _schemaGenerator, schemaRepository);
                foreach (var filter in _options.DocumentFilters)
                {
                    filter.Apply(swaggerDoc, filterContext);
                }

                swaggerDoc.Components.Schemas = new SortedDictionary <string, OpenApiSchema>(swaggerDoc.Components.Schemas, _options.SchemaComparer);

                return(swaggerDoc);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #8
0
        public override void Initialize(ApplicationInitializationContext context)
        {
            var info = AppConfig.SwaggerConfig.Info == null
          ? AppConfig.SwaggerOptions : AppConfig.SwaggerConfig.Info;

            if (info != null)
            {
                context.Builder.UseSwagger();
                context.Builder.UseSwaggerUI(c =>
                {
                    c.ShowExtensions();
                    var areaName = AppConfig.SwaggerConfig.Options?.IngressName;
                    c.SwaggerEndpoint($"../swagger/{info.Version}/swagger.json", info.Title, areaName);
                    c.SwaggerEndpoint(_serviceEntryProvider.GetALLEntries(), areaName);
                });
            }
        }
Example #9
0
        public SwaggerDocument GetSwagger(
            string documentName,
            string host      = null,
            string basePath  = null,
            string[] schemes = null)
        {
            if (!_options.SwaggerDocs.TryGetValue(documentName, out Info info))
            {
                throw new UnknownSwaggerDocument(documentName);
            }


            var mapRoutePaths = Swagger.AppConfig.SwaggerConfig.Options?.MapRoutePaths;
            var isOnlyGenerateLocalHostDocs = Swagger.AppConfig.SwaggerConfig.Options?.IsOnlyGenerateLocalHostDocs;
            IEnumerable <ServiceEntry> entries;

            if (isOnlyGenerateLocalHostDocs != null && isOnlyGenerateLocalHostDocs.Value)
            {
                entries = _serviceEntryProvider.GetEntries().Where(p => !p.Descriptor.DisableNetwork());
            }
            else
            {
                entries = _serviceEntryProvider.GetALLEntries().Where(p => !p.Descriptor.DisableNetwork());
            }

            if (mapRoutePaths != null)
            {
                foreach (var path in mapRoutePaths)
                {
                    var entry = entries.Where(p => p.RoutePath == path.SourceRoutePath).FirstOrDefault();
                    if (entry != null)
                    {
                        entry.RoutePath            = path.TargetRoutePath;
                        entry.Descriptor.RoutePath = path.TargetRoutePath;
                    }
                }
            }
            entries = entries.Where(apiDesc => _options.DocInclusionPredicateV2(documentName, apiDesc));
            var schemaRegistry = _schemaRegistryFactory.Create();

            var swaggerDoc = new SwaggerDocument
            {
                Info                = info,
                Host                = host,
                BasePath            = basePath,
                Schemes             = schemes,
                Paths               = CreatePathItems(entries, schemaRegistry),
                Definitions         = schemaRegistry.Definitions,
                SecurityDefinitions = _options.SecurityDefinitions.Any() ? _options.SecurityDefinitions : null,
                Security            = _options.SecurityRequirements.Any() ? _options.SecurityRequirements : null
            };

            return(swaggerDoc);
        }
Example #10
0
        public override void Initialize(ApplicationInitializationContext context)
        {
            var info = AppConfig.SwaggerConfig.Info == null
          ? AppConfig.SwaggerOptions : AppConfig.SwaggerConfig.Info;

            if (info != null)
            {
                context.Builder.UseSwagger();
                context.Builder.UseSwaggerUI(c =>
                {
                    var areaName = AppConfig.SwaggerConfig.Options?.IngressName;
                    c.SwaggerEndpoint($"../swagger/{info.Version}/swagger.json", info.Title, areaName);
                    var isOnlyGenerateLocalHostDocs = AppConfig.SwaggerConfig.Options?.IsOnlyGenerateLocalHostDocs;
                    if (isOnlyGenerateLocalHostDocs != null && isOnlyGenerateLocalHostDocs.Value)
                    {
                        c.SwaggerEndpoint(_serviceEntryProvider.GetEntries(), areaName);
                    }
                    else
                    {
                        c.SwaggerEndpoint(_serviceEntryProvider.GetALLEntries(), areaName);
                    }
                });
            }
        }
Example #11
0
        public IEnumerable <string> GetSchemaFilesPath()
        {
            var result         = new List <string>();
            var assemblieFiles = _serviceEntryProvider.GetALLEntries()
                                 .Select(p => p.Type.Assembly.Location).Distinct();

            foreach (var assemblieFile in assemblieFiles)
            {
                var fileSpan = assemblieFile.AsSpan();
                var path     = $"{fileSpan.Slice(0, fileSpan.LastIndexOf(".")).ToString()}.xml";
                if (File.Exists(path))
                {
                    result.Add(path);
                }
            }
            return(result);
        }
Example #12
0
        public SwaggerDocument GetSwagger(
            string documentName,
            string host      = null,
            string basePath  = null,
            string[] schemes = null)
        {
            if (!_options.SwaggerDocs.TryGetValue(documentName, out Info info))
            {
                throw new UnknownSwaggerDocument(documentName);
            }

            var entry = _serviceEntryProvider.GetALLEntries();



            var schemaRegistry = _schemaRegistryFactory.Create();

            var swaggerDoc = new SwaggerDocument
            {
                Info                = info,
                Host                = host,
                BasePath            = basePath,
                Schemes             = schemes,
                Paths               = CreatePathItems(entry, schemaRegistry),
                Definitions         = schemaRegistry.Definitions,
                SecurityDefinitions = _options.SecurityDefinitions.Any() ? _options.SecurityDefinitions : null,
                Security            = _options.SecurityRequirements.Any() ? _options.SecurityRequirements : null
            };

            //var filterContext = new DocumentFilterContext(
            //    _apiDescriptionsProvider.ApiDescriptionGroups,
            //    applicableApiDescriptions,
            //    schemaRegistry);

            //foreach (var filter in _options.DocumentFilters)
            //{
            //    filter.Apply(swaggerDoc, filterContext);
            //}

            return(swaggerDoc);
        }
        public IEnumerable <string> GetSchemaFilesPath(string annotationXmlDir)
        {
            var result         = new List <string>();
            var assemblieFiles = _serviceEntryProvider.GetALLEntries()
                                 .Select(p => p.Type.Assembly.Location).Distinct();

            foreach (var assemblieFile in assemblieFiles)
            {
                var fileSpan = assemblieFile.AsSpan();
                var path     = $"{fileSpan.Slice(0, fileSpan.LastIndexOf(".")).ToString()}.xml";
                if (!string.IsNullOrEmpty(annotationXmlDir))
                {
                    path = Path.Combine(annotationXmlDir, assemblieFile.Split("/").Last().Replace("dll", "xml"));
                }

                if (File.Exists(path))
                {
                    result.Add(path);
                }
            }
            return(result);
        }