Skip to content
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

Closed
stevehu opened this issue Feb 26, 2018 · 5 comments
Closed

add an endpoint to serve swagger-ui for manual testing #30

stevehu opened this issue Feb 26, 2018 · 5 comments

Comments

@stevehu
Copy link
Contributor

stevehu commented Feb 26, 2018

No description provided.

@bkuberek
Copy link
Member

bkuberek commented Sep 4, 2018

This is actually straightforward. I am doing this on a couple of projects.

Three things need to be done,

  1. Create an endpoint that serves the openapi.json file
  2. Create a resources/swaggerui.html
  3. Create an endpoint to serve the html file
src/main/resources
├── config
│   ├── openapi.json
│   └── openapi.yaml
└── swaggerui.html

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);
    }
  }
}

@stevehu
Copy link
Contributor Author

stevehu commented Sep 4, 2018

This is so cool. We will add it to the light-codegen based on the config.json. Thanks.

@GavinChenYan
Copy link
Contributor

I will take a look this issue

@DSchrupert
Copy link
Member

@GavinChenYan
Copy link
Contributor

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
Projects
None yet
Development

No branches or pull requests

5 participants