Skip to content

Commit

Permalink
Add DisableSwaggerUI option to Swagger UI
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jun 12, 2017
1 parent 34acc42 commit 3f092a5
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions src/ServiceStack.Api.OpenApi/OpenApiFeature.cs
Expand Up @@ -37,6 +37,8 @@ public class OpenApiFeature : IPlugin, IPreInitPlugin

public List<string> AnyRouteVerbs { get; set; }

public bool DisableSwaggerUI { get; set; }

public OpenApiFeature()
{
LogoUrl = "//raw.githubusercontent.com/ServiceStack/Assets/master/img/artwork/logo-24.png";
Expand Down Expand Up @@ -65,51 +67,54 @@ public void Register(IAppHost appHost)

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

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

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

appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) =>
{
IVirtualFile indexFile;
IVirtualFile patchFile = null;
switch (pathInfo)
appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) =>
{
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) =>
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)
{
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);
var html = indexFile.ReadAllText();
var injectJs = patchFile?.ReadAllText();
if (injectJs != null)
return new CustomResponseHandler((req, res) =>
{
html = html.Replace("</body>",
"<script type='text/javascript'>" + injectJs + "</script></body>");
}
return html;
});
}
return pathInfo.StartsWith("/swagger-ui") ? new StaticFileHandler() : null;
});
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;
});
}
}

public static bool IsEnabled => HostContext.HasPlugin<OpenApiFeature>();
Expand Down

0 comments on commit 3f092a5

Please sign in to comment.