From fe05331edb0f180373721ecb73870f42bad61377 Mon Sep 17 00:00:00 2001 From: joshua bauer Date: Wed, 5 Dec 2018 21:45:15 -0800 Subject: [PATCH] Added redoc support for OpenAPI. --- .../proteus/services/OpenAPIService.java | 51 ++++++++++++++++--- .../proteus/services/SwaggerService.java | 2 +- .../proteus/server/tools/openapi/redoc.html | 25 +++++++++ .../proteus/server/tools/swagger/redoc.html | 10 ++-- src/main/resources/reference.conf | 2 + 5 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 src/main/resources/io/sinistral/proteus/server/tools/openapi/redoc.html diff --git a/src/main/java/io/sinistral/proteus/services/OpenAPIService.java b/src/main/java/io/sinistral/proteus/services/OpenAPIService.java index ad223a7..d0720e1 100644 --- a/src/main/java/io/sinistral/proteus/services/OpenAPIService.java +++ b/src/main/java/io/sinistral/proteus/services/OpenAPIService.java @@ -83,6 +83,8 @@ public class OpenAPIService extends BaseService implements Supplier servers = mapper.convertValue(openAPIConfig.getValue("servers").unwrapped(), new TypeReference>() - { - }); + List servers = mapper.convertValue(openAPIConfig.getValue("servers").unwrapped(), new TypeReference>(){}); openApi.setServers(servers); @@ -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 @@ -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 { diff --git a/src/main/java/io/sinistral/proteus/services/SwaggerService.java b/src/main/java/io/sinistral/proteus/services/SwaggerService.java index ae53a8d..7e79eee 100644 --- a/src/main/java/io/sinistral/proteus/services/SwaggerService.java +++ b/src/main/java/io/sinistral/proteus/services/SwaggerService.java @@ -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; diff --git a/src/main/resources/io/sinistral/proteus/server/tools/openapi/redoc.html b/src/main/resources/io/sinistral/proteus/server/tools/openapi/redoc.html new file mode 100644 index 0000000..b99afb5 --- /dev/null +++ b/src/main/resources/io/sinistral/proteus/server/tools/openapi/redoc.html @@ -0,0 +1,25 @@ + + + + ReDoc + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/io/sinistral/proteus/server/tools/swagger/redoc.html b/src/main/resources/io/sinistral/proteus/server/tools/swagger/redoc.html index c3eb485..b99afb5 100644 --- a/src/main/resources/io/sinistral/proteus/server/tools/swagger/redoc.html +++ b/src/main/resources/io/sinistral/proteus/server/tools/swagger/redoc.html @@ -1,10 +1,11 @@ - {{ applicationName }} ReDoc + ReDoc +