Skip to content

Commit

Permalink
Manual swagger documentation generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernard Labno committed Aug 6, 2019
1 parent 3798e63 commit f5157a2
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 1 deletion.
41 changes: 40 additions & 1 deletion api/src/main/java/bisq/api/http/service/HttpApiServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import bisq.core.app.BisqEnvironment;

import javax.servlet.DispatcherType;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.berndpruenster.netlayer.tor.HsContainer;
import org.berndpruenster.netlayer.tor.Tor;
Expand All @@ -33,7 +36,9 @@
import javax.inject.Inject;

import java.net.InetSocketAddress;
import java.net.URL;

import java.io.BufferedInputStream;
import java.io.IOException;

import java.util.EnumSet;
Expand All @@ -42,11 +47,15 @@



import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Slf4jRequestLog;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
Expand All @@ -72,7 +81,7 @@ public HttpApiServer(ApiPasswordManager apiPasswordManager, BisqEnvironment bisq
public void startServer() {
try {
ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
contextHandlerCollection.setHandlers(new Handler[]{buildAPIHandler()});
contextHandlerCollection.setHandlers(new Handler[]{buildSwaggerUIHandler(), buildOpenAPIJsonHandler(), buildAPIHandler()});
// Start server
InetSocketAddress socketAddress = new InetSocketAddress(bisqEnvironment.getHttpApiHost(), bisqEnvironment.getHttpApiPort());
Server server = new Server(socketAddress);
Expand All @@ -90,13 +99,43 @@ private ContextHandler buildAPIHandler() {
ResourceConfig resourceConfig = new ResourceConfig();
ExceptionMappers.register(resourceConfig);
resourceConfig.register(httpApiInterfaceV1);
resourceConfig.packages("io.swagger.v3.jaxrs2.integration.resources");
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS | ServletContextHandler.NO_SECURITY);
servletContextHandler.setContextPath("/");
servletContextHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");
setupAuth(servletContextHandler);
return servletContextHandler;
}

private ContextHandler buildSwaggerUIHandler() throws Exception {
ResourceHandler swaggerUIResourceHandler = new ResourceHandler();
swaggerUIResourceHandler.setResourceBase(getClass().getClassLoader().getResource("META-INF/swagger/ui").toURI().toString());
ContextHandler swaggerUIContext = new ContextHandler();
swaggerUIContext.setContextPath("/docs");
swaggerUIContext.setHandler(swaggerUIResourceHandler);
return swaggerUIContext;
}

private ContextHandler buildOpenAPIJsonHandler() throws Exception {
URL openAPIJSONResource = getClass().getClassLoader().getResource("META-INF/swagger/openapi.json");
assert openAPIJSONResource != null;
String openAPIJSONContent = new String(((BufferedInputStream) openAPIJSONResource.getContent()).readAllBytes());
ContextHandler contextHandler = new ContextHandler();
contextHandler.setContextPath("/");
contextHandler.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (!"/openapi.json".equals(target)) {
return;
}
response.setContentType(MimeTypes.Type.APPLICATION_JSON.asString());
response.getWriter().write(openAPIJSONContent);
baseRequest.setHandled(true);
}
});
return contextHandler;
}

private void setupAuth(ServletContextHandler appContextHandler) {
AuthFilter authFilter = new AuthFilter(apiPasswordManager, tokenRegistry);
appContextHandler.addFilter(new FilterHolder(authFilter), "/*", EnumSet.allOf(DispatcherType.class));
Expand Down
163 changes: 163 additions & 0 deletions api/src/main/resources/META-INF/swagger/openapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"openapi": "3.0.1",
"info": {
"title": "Bisq HTTP API",
"version": "0.0.1"
},
"security": [
{
"authorization": []
}
],
"tags": [
{
"name": "user"
},
{
"name": "version"
}
],
"paths": {
"/api/v1/user/authenticate": {
"post": {
"tags": [
"user"
],
"summary": "Exchange password for access token",
"operationId": "authenticate",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AuthForm"
}
}
}
},
"responses": {
"default": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AuthResult"
}
}
}
}
}
}
},
"/api/v1/user/password": {
"post": {
"tags": [
"user"
],
"summary": "Change password",
"operationId": "changePassword",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ChangePassword"
}
}
}
},
"responses": {
"default": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AuthResult"
}
}
}
}
}
}
},
"/api/v1/version": {
"get": {
"tags": [
"version"
],
"summary": "Get version details",
"operationId": "getVersionDetails",
"responses": {
"default": {
"description": "default response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/VersionDetails"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"AuthResult": {
"type": "object",
"properties": {
"token": {
"type": "string"
}
}
},
"AuthForm": {
"type": "object",
"properties": {
"password": {
"type": "string"
}
}
},
"ChangePassword": {
"type": "object",
"properties": {
"newPassword": {
"type": "string"
},
"oldPassword": {
"type": "string"
}
}
},
"VersionDetails": {
"type": "object",
"properties": {
"application": {
"type": "string"
},
"network": {
"type": "integer",
"format": "int32"
},
"p2PMessage": {
"type": "integer",
"format": "int32"
},
"localDB": {
"type": "integer",
"format": "int32"
},
"tradeProtocol": {
"type": "integer",
"format": "int32"
}
}
}
},
"securitySchemes": {
"authorization": {
"type": "apiKey",
"name": "authorization",
"in": "header"
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions api/src/main/resources/META-INF/swagger/ui/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" >
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}

*,
*:before,
*:after
{
box-sizing: inherit;
}

body
{
margin:0;
background: #fafafa;
}
</style>
</head>

<body>
<div id="swagger-ui"></div>

<script src="./swagger-ui-bundle.js"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "/openapi.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "BaseLayout"
});
// End Swagger UI call region

window.ui = ui
}
</script>
</body>
</html>
98 changes: 98 additions & 0 deletions api/src/main/resources/META-INF/swagger/ui/swagger-ui-bundle.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions api/src/main/resources/META-INF/swagger/ui/swagger-ui.css

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions api/src/test/java/bisq/api/http/service/auth/SwaggerGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.api.http.service.auth;

import bisq.api.http.service.HttpApiInterfaceV1;



import io.swagger.v3.core.util.Json;
import io.swagger.v3.jaxrs2.Reader;
import io.swagger.v3.oas.integration.SwaggerConfiguration;
import io.swagger.v3.oas.models.OpenAPI;

public class SwaggerGenerator {

public static void main(String[] args) {
Reader reader = new Reader(new SwaggerConfiguration());
OpenAPI openAPI = reader.read(HttpApiInterfaceV1.class);
System.out.println(Json.pretty(openAPI));
}
}
Loading

0 comments on commit f5157a2

Please sign in to comment.