示例#1
0
        public void Register(IAppHost appHost)
        {
            if (ResourceFilterPattern != null)
            {
                OpenApiService.resourceFilterRegex = new Regex(ResourceFilterPattern, RegexOptions.Compiled);
            }

            if (SecurityDefinitions == null && OperationSecurity == null)
            {
                var useBasicAuth = appHost.GetPlugin <AuthFeature>()?.AuthProviders
                                   ?.Any(x => x.Provider == AuthenticateService.BasicProvider) == true;
                if (!useBasicAuth)
                {
                    UseBearerSecurity = true;
                }
                else
                {
                    UseBasicSecurity = true;
                }
            }

            OpenApiService.UseCamelCaseSchemaPropertyNames           = UseCamelCaseSchemaPropertyNames;
            OpenApiService.UseLowercaseUnderscoreSchemaPropertyNames = UseLowercaseUnderscoreSchemaPropertyNames;
            OpenApiService.DisableAutoDtoInBodyParam = DisableAutoDtoInBodyParam;
            OpenApiService.ApiDeclarationFilter      = ApiDeclarationFilter;
            OpenApiService.OperationFilter           = OperationFilter;
            OpenApiService.SchemaFilter                  = SchemaFilter;
            OpenApiService.SchemaPropertyFilter          = SchemaPropertyFilter;
            OpenApiService.AnyRouteVerbs                 = AnyRouteVerbs.ToArray();
            OpenApiService.InlineSchemaTypesInNamespaces = InlineSchemaTypesInNamespaces.ToArray();
            OpenApiService.SecurityDefinitions           = SecurityDefinitions;
            OpenApiService.OperationSecurity             = OperationSecurity;

            appHost.RegisterService(typeof(OpenApiService), "/openapi");

            if (!DisableSwaggerUI)
            {
                var swaggerUrl = "swagger-ui/";

                appHost.GetPlugin <MetadataFeature>()
                .AddPluginLink(swaggerUrl, "Swagger UI");

                appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) =>
                {
                    IVirtualFile indexFile;
                    IVirtualFile patchFile        = null;
                    IVirtualFile patchPreLoadFile = null;
                    switch (pathInfo)
                    {
                    case "/swagger-ui/":
                    case "/swagger-ui/default.html":
                        indexFile        = appHost.VirtualFileSources.GetFile("/swagger-ui/index.html");
                        patchFile        = appHost.VirtualFileSources.GetFile("/swagger-ui/patch.js");
                        patchPreLoadFile = appHost.VirtualFileSources.GetFile("/swagger-ui/patch-preload.js");
                        break;

                    default:
                        indexFile = null;
                        break;
                    }
                    if (indexFile != null)
                    {
                        var html            = indexFile.ReadAllText();
                        var injectJs        = patchFile?.ReadAllText();
                        var injectPreloadJs = patchPreLoadFile?.ReadAllText();

                        return(new CustomResponseHandler((req, res) =>
                        {
                            res.ContentType = MimeTypes.HtmlUtf8; //use alt HTML ContentType so it's not overridden when Feature.Html is removed
                            var resourcesUrl = req.ResolveAbsoluteUrl("~/openapi");
                            html = html.Replace("http://petstore.swagger.io/v2/swagger.json", resourcesUrl)
                                   .Replace("ApiDocs", HostContext.ServiceName)
                                   .Replace("<span class=\"logo__title\">swagger</span>", $"<span class=\"logo__title\">{HostContext.ServiceName}</span>")
                                   .Replace("http://swagger.io", LogoHref ?? "./");

                            if (LogoUrl != null)
                            {
                                html = html.Replace("images/logo_small.png", LogoUrl);
                            }

                            if (injectPreloadJs != null)
                            {
                                html = html.Replace("window.swaggerUi.load();", injectPreloadJs + "\n\n      window.swaggerUi.load();");
                            }

                            if (injectJs != null)
                            {
                                html = html.Replace("</body>",
                                                    "<script type='text/javascript'>" + injectJs + "</script></body>");
                            }

                            return html;
                        }));
                    }
                    return(pathInfo.StartsWith("/swagger-ui/")
                        ? new StaticFileHandler()
                        : null);
                });
            }
        }
示例#2
0
        public void Register(IAppHost appHost)
        {
            if (ResourceFilterPattern != null)
            {
                OpenApiService.resourceFilterRegex = new Regex(ResourceFilterPattern, RegexOptions.Compiled);
            }

            OpenApiService.UseCamelCaseSchemaPropertyNames           = UseCamelCaseSchemaPropertyNames;
            OpenApiService.UseLowercaseUnderscoreSchemaPropertyNames = UseLowercaseUnderscoreSchemaPropertyNames;
            OpenApiService.DisableAutoDtoInBodyParam = DisableAutoDtoInBodyParam;
            OpenApiService.ApiDeclarationFilter      = ApiDeclarationFilter;
            OpenApiService.OperationFilter           = OperationFilter;
            OpenApiService.SchemaFilter         = SchemaFilter;
            OpenApiService.SchemaPropertyFilter = SchemaPropertyFilter;
            OpenApiService.AnyRouteVerbs        = AnyRouteVerbs.ToArray();

            appHost.RegisterService(typeof(OpenApiService), "/openapi");

            if (!DisableSwaggerUI)
            {
                var swaggerUrl = "swagger-ui/";

                appHost.GetPlugin <MetadataFeature>()
                .AddPluginLink(swaggerUrl, "Swagger UI");

                appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) =>
                {
                    IVirtualFile indexFile;
                    IVirtualFile patchFile = null;
                    switch (pathInfo)
                    {
                    case "/swagger-ui":
                    case "/swagger-ui/":
                    case "/swagger-ui/default.html":
                        indexFile = appHost.VirtualFileSources.GetFile("/swagger-ui/index.html");
                        patchFile = appHost.VirtualFileSources.GetFile("/swagger-ui/patch.js");
                        break;

                    default:
                        indexFile = null;
                        break;
                    }
                    if (indexFile != null)
                    {
                        var html     = indexFile.ReadAllText();
                        var injectJs = patchFile?.ReadAllText();

                        return(new CustomResponseHandler((req, res) =>
                        {
                            res.ContentType = MimeTypes.Html;
                            var resourcesUrl = req.ResolveAbsoluteUrl("~/openapi");
                            html = html.Replace("http://petstore.swagger.io/v2/swagger.json", resourcesUrl)
                                   .Replace("ApiDocs", HostContext.ServiceName)
                                   .Replace("{LogoUrl}", LogoUrl);

                            if (injectJs != null)
                            {
                                html = html.Replace("</body>",
                                                    "<script type='text/javascript'>" + injectJs + "</script></body>");
                            }

                            return html;
                        }));
                    }
                    return(pathInfo.StartsWith("/swagger-ui") ? new StaticFileHandler() : null);
                });
            }
        }