-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add an endpoint to serve swagger-ui for manual testing #30
Labels
Comments
This is actually straightforward. I am doing this on a couple of projects. Three things need to be done,
The HTML file <html>
<head>
<title>OpenAPI Spec</title>
<!-- https://cdnjs.com/libraries/swagger-ui -->
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.17.6/swagger-ui.css">
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.17.6/swagger-ui-bundle.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.17.6/swagger-ui-standalone-preset.js"></script>
<script>
window.onload = function () {
window.ui = SwaggerUIBundle({
url: "/spec.json",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
]
});
}
</script>
</body>
</html> Handler package com.foobar.service.handler;
import com.networknt.config.Config;
import com.networknt.handler.HandlerProvider;
import com.networknt.health.HealthGetHandler;
import com.networknt.info.ServerInfoGetHandler;
import io.undertow.Handlers;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.HttpString;
import io.undertow.util.Methods;
public class ApiHandlerProvider implements HandlerProvider {
@Override
public HttpHandler getHandler() {
return Handlers.routing()
// Metadata
.add(Methods.GET, "/health", new HealthGetHandler())
.add(Methods.GET, "/server/info", new ServerInfoGetHandler())
// Spec
.add(Methods.GET, "/spec.html", new StaticFileHandler("swaggerui.html", "text/html"))
.add(Methods.GET, "/spec.yaml", new StaticFileHandler("openapi.yaml", "text/yaml"))
.add(Methods.GET, "/spec.json", new StaticFileHandler("openapi.json", "application/json"))
;
}
private static class StaticFileHandler implements HttpHandler {
private final String filename;
private final String contentType;
private StaticFileHandler(final String filename, final String contentType) {
this.filename = filename;
this.contentType = contentType;
}
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
final String payload = Config.getInstance().getStringFromFile(filename);
exchange.getResponseHeaders().add(new HttpString("Content-Type"), contentType);
exchange.getResponseSender().send(payload);
}
}
} |
This is so cool. We will add it to the light-codegen based on the config.json. Thanks. |
I will take a look this issue |
Will add a new handler for Swagger UI display. And enhance the light-codegen for these handlers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: