Exemple #1
0
        public string GetHtml <T>(T response, IVirtualFile vf)
        {
            var json = JsonDataContractSerializer.Instance.SerializeToString(response) ?? "null";

            json = json.Replace("<", "&lt;").Replace(">", "&gt;");
            return(vf.ReadAllText().Replace("${Dto}", json));
        }
Exemple #2
0
        private IVirtualFile ResolveRazorPageAsset(IHttpRequest httpReq, IHttpResponse httpRes, object model)
        {
            IVirtualFile razerPageAsset = this.FindRazorPageAsset(httpReq, model);

            if (razerPageAsset == null)
            {
                httpRes.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            using (var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom))
            {
                writer.Write(razerPageAsset.ReadAllText());
            }


            return(razerPageAsset);
        }
        /// <summary>
        ///     Registers the <see cref="IPlugin" />
        /// </summary>
        public void Register(IAppHost appHost)
        {
            plugIn.Register(appHost);

            // Replace previous plugin link
            appHost.GetPlugin <MetadataFeature>().PluginLinks.Remove("swagger-ui/");
            string newPluginPath = "{0}/".FormatWith(directory);

            appHost.GetPlugin <MetadataFeature>().AddPluginLink(newPluginPath, "Swagger UI");
            appHost.CatchAllHandlers.Add(delegate(string httpMethod, string pathInfo, string filePath)
            {
                var supportedPaths = new[]
                {
                    "/{0}".FormatWith(directory),
                    "/{0}/".FormatWith(directory),
                    "/{0}/default.html".FormatWith(directory)
                };
                string newPath = "/{0}/index.html".FormatWith(directory);

                if (supportedPaths.Contains(pathInfo, StringComparer.OrdinalIgnoreCase))
                {
                    IVirtualFile file = appHost.VirtualPathProvider.GetFile(newPath);
                    if (file != null)
                    {
                        string html = file.ReadAllText();
                        return(new CustomResponseHandler(delegate(IRequest req, IResponse res)
                        {
                            res.ContentType = MimeTypes.Html;
                            string newValue = req.ResolveAbsoluteUrl(@"~/resources");
                            html = html.Replace("http://petstore.swagger.wordnik.com/api/api-docs", newValue);
                            return html;
                        }, null));
                    }
                }
                return(null);
            });
        }
Exemple #4
0
        public void Register(IAppHost appHost)
        {
            if (ResourceFilterPattern != null)
            {
                SwaggerResourcesService.resourceFilterRegex = new Regex(ResourceFilterPattern, RegexOptions.Compiled);
            }

            SwaggerResourcesService.ResourcesResponseFilter = ResourcesResponseFilter;

            SwaggerApiService.UseCamelCaseModelPropertyNames           = UseCamelCaseModelPropertyNames;
            SwaggerApiService.UseLowercaseUnderscoreModelPropertyNames = UseLowercaseUnderscoreModelPropertyNames;
            SwaggerApiService.DisableAutoDtoInBodyParam = DisableAutoDtoInBodyParam;
            SwaggerApiService.ApiDeclarationFilter      = ApiDeclarationFilter;
            SwaggerApiService.OperationFilter           = OperationFilter;
            SwaggerApiService.ModelFilter         = ModelFilter;
            SwaggerApiService.ModelPropertyFilter = ModelPropertyFilter;

            appHost.RegisterService(typeof(SwaggerResourcesService), new[] { "/resources" });
            appHost.RegisterService(typeof(SwaggerApiService), new[] { SwaggerResourcesService.RESOURCE_PATH + "/{Name*}" });

            var swaggerUrl = UseBootstrapTheme
                ? "swagger-ui-bootstrap/"
                : "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;

                case "/swagger-ui-bootstrap":
                case "/swagger-ui-bootstrap/":
                case "/swagger-ui-bootstrap/index.html":
                    indexFile = appHost.VirtualFileSources.GetFile("/swagger-ui-bootstrap/index.html");
                    break;

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

                    return(new CustomResponseHandler((req, res) =>
                    {
                        res.ContentType = MimeTypes.Html;
                        var resourcesUrl = req.ResolveAbsoluteUrl("~/resources");
                        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);
            });
        }
 private string GetPageContents(IVirtualFile page)
 {
     return ReplaceContentWithRewriteTokens(page.ReadAllText());
 }
Exemple #6
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);
                });
            }
        }
Exemple #7
0
 private string GetPageContents(IVirtualFile page)
 {
     return(ReplaceContentWithRewriteTokens(page.ReadAllText()));
 }
        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/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("<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 (injectJs != null)
                            {
                                html = html.Replace("</body>",
                                                    "<script type='text/javascript'>" + injectJs + "</script></body>");
                            }

                            return html;
                        }));
                    }
                    return(pathInfo.StartsWith("/swagger-ui/")
                        ? new StaticFileHandler()
                        : null);
                });
            }
        }
 public string textContents(IVirtualFile file) => file?.ReadAllText();
 public object fileContents(IVirtualFile file) => file is null ? null
     : MimeTypes.IsBinary(MimeTypes.GetMimeType(file.Extension))
         ? file.ReadAllBytes()
         : (object)file.ReadAllText();