JavaVertXWebServer.supportFiles.HttpServerVerticle.mustache Maven / Gradle / Ivy
package {{invokerPackage}};
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.openapi.RouterBuilder;
import io.vertx.ext.web.openapi.RouterBuilderOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
{{#apiInfo}}{{#apis}}
import {{apiPackage}}.{{classname}}Handler;
import {{apiPackage}}.{{classname}}Impl;{{/apis}}{{/apiInfo}}
public class HttpServerVerticle extends AbstractVerticle {
private static final Logger logger = LoggerFactory.getLogger(HttpServerVerticle.class);
private static final String specFile = "src/main/resources/openapi.yaml";
{{#apiInfo}}{{#apis}}
private final {{classname}}Handler {{classVarName}}Handler = new {{classname}}Handler(new {{classname}}Impl());{{/apis}}{{/apiInfo}}
@Override
public void start(Promise startPromise) {
RouterBuilder.create(vertx, specFile)
.map(builder -> {
builder.setOptions(new RouterBuilderOptions()
// For production use case, you need to enable this flag and provide the proper security handler
.setRequireSecurityHandlers(false)
);
{{#apiInfo}}{{#apis}}
{{classVarName}}Handler.mount(builder);{{/apis}}{{/apiInfo}}
Router router = builder.createRouter();
router.errorHandler(400, this::validationFailureHandler);
return router;
})
.compose(router ->
vertx.createHttpServer()
.requestHandler(router)
.listen(8080)
)
.onSuccess(server -> logger.info("Http verticle deploy successful"))
.onFailure(t -> logger.error("Http verticle failed to deploy", t))
// Complete the start promise
.mapEmpty().onComplete(startPromise);
}
private void validationFailureHandler(RoutingContext rc) {
rc.response().setStatusCode(400)
.end("Bad Request : " + rc.failure().getMessage());
}
}