Skip to content

Commit

Permalink
Added redoc support for OpenAPI.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Dec 6, 2018
1 parent f0f42d0 commit fe05331
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 11 deletions.
51 changes: 45 additions & 6 deletions src/main/java/io/sinistral/proteus/services/OpenAPIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class OpenAPIService extends BaseService implements Supplier<RoutingHandl
protected OpenAPI openApi = null;
protected String spec = null;
protected String indexHTML = null;
protected String redocHTML = null;

@Inject
@Named("openapi.resourcePrefix")
protected String resourcePrefix;
Expand All @@ -101,9 +103,15 @@ public class OpenAPIService extends BaseService implements Supplier<RoutingHandl
@Inject
@Named("openapi.port")
protected Integer port;

@Inject
@Named("openapi.redocPath")
protected String redocPath;

@Inject
@Named("application.path")
protected String applicationPath;

@Inject
protected RoutingHandler router;
@Inject
Expand Down Expand Up @@ -141,6 +149,13 @@ public void generateHTML()
templateString = templateString.replaceAll("\\{\\{ title \\}\\}", applicationName + " Swagger UI");
this.indexHTML = templateString;
}

try (InputStream templateInputStream = getClass().getClassLoader().getResourceAsStream(resourcePrefix + "/redoc.html"))
{
byte[] templateBytes = IOUtils.toByteArray(templateInputStream);

this.redocHTML = new String(templateBytes, Charset.defaultCharset());
}

URL url = this.getClass().getClassLoader().getResource(resourcePrefix);

Expand Down Expand Up @@ -234,9 +249,7 @@ public void generateSpec() throws Exception

openApi.getComponents().setSecuritySchemes(securitySchemes);

List<Server> servers = mapper.convertValue(openAPIConfig.getValue("servers").unwrapped(), new TypeReference<List<Server>>()
{
});
List<Server> servers = mapper.convertValue(openAPIConfig.getValue("servers").unwrapped(), new TypeReference<List<Server>>(){});

openApi.setServers(servers);

Expand Down Expand Up @@ -317,11 +330,10 @@ public void handleRequest(HttpServerExchange exchange) throws Exception
.withMethod(Methods.GET)
.withProduces(io.sinistral.proteus.server.MediaType.TEXT_YAML.contentType())
.build());

pathTemplate = this.basePath;


router.add( HttpMethod.GET,
pathTemplate,
basePath,
new HttpHandler()
{
@Override
Expand All @@ -339,6 +351,33 @@ public void handleRequest(HttpServerExchange exchange) throws Exception
.withControllerName(this.getClass().getSimpleName())
.withMethod(Methods.GET)
.build());

final String specPath = pathTemplate;

router.add( HttpMethod.GET,
this.basePath + "/" + this.redocPath,
new HttpHandler()
{
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception
{
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MediaType.TEXT_HTML);

final String fullPath = String.format("%s://%s%s",exchange.getRequestScheme(), exchange.getHostAndPort(), specPath);

final String html = redocHTML.replaceAll("\\{\\{ specPath \\}\\}", fullPath);

exchange.getResponseSender().send(html);
}
});

this.registeredEndpoints.add(EndpointInfo.builder()
.withConsumes(MediaType.WILDCARD)
.withProduces(MediaType.TEXT_HTML)
.withPathTemplate(this.basePath + "/" + this.redocPath)
.withControllerName(this.getClass().getSimpleName())
.withMethod(Methods.GET)
.build());

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public void generateSwaggerHTML()

String templateString = new String(templateBytes, Charset.defaultCharset());

templateString = templateString.replaceAll("\\{\\{ swaggerSpecPath \\}\\}", this.basePath + ".json");
templateString = templateString.replaceAll("\\{\\{ specPath \\}\\}", this.basePath + ".json");
templateString = templateString.replaceAll("\\{\\{ applicationName \\}\\}", applicationName);

this.redocHTML = templateString;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>ReDoc</title>
<!-- needed for adaptive design -->
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">

<!--
ReDoc doesn't change outer page styles
-->
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-url='{{ specPath }}'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>{{ applicationName }} ReDoc</title>
<title>ReDoc</title>
<!-- needed for adaptive design -->
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">

<!--
ReDoc doesn't change outer page styles
Expand All @@ -15,9 +16,10 @@
padding: 0;
}
</style>
</head>
</head>
<body>
<redoc spec-url='{{ swaggerSpecPath }}'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc/bundles/redoc.standalone.js"> </script>
<redoc spec-url='{{ specPath }}'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script>

</body>
</html>
2 changes: 2 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ openapi {

basePath= ${application.path}"/openapi"

redocPath= "redoc"

port = ${application.ports.http}

specFilename="openapi.yaml"
Expand Down

0 comments on commit fe05331

Please sign in to comment.