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); }
private void Initialize(IServiceEntryProvider provider) { var entries = provider.GetEntries(); if (entries != null && entries.Any()) { foreach (var entry in entries) { _entryCache.AddOrUpdate(entry.ServiceId, entry, (k, v) => entry); } } else { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug("没有发现可注册的服务"); } } }
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); } }); } }