JavaVertXServer.MainApiVerticle.mustache Maven / Gradle / Ivy
package {{rootPackage}};
import java.nio.charset.Charset;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.github.phiz71.vertx.swagger.router.OperationIdServiceIdResolver;
import com.github.phiz71.vertx.swagger.router.SwaggerRouter;
import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.file.FileSystem;
import io.vertx.core.json.Json;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
public class MainApiVerticle extends AbstractVerticle {
static final Logger LOGGER = LoggerFactory.getLogger(MainApiVerticle.class);
private int serverPort = {{serverPort}};
protected Router router;
public int getServerPort() {
return serverPort;
}
public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}
@Override
public void init(Vertx vertx, Context context) {
super.init(vertx, context);
router = Router.router(vertx);
}
@Override
public void start(Future startFuture) throws Exception {
Json.mapper.registerModule(new JavaTimeModule());
FileSystem vertxFileSystem = vertx.fileSystem();
vertxFileSystem.readFile("openapi.json", readFile -> {
if (readFile.succeeded()) {
Swagger swagger = new SwaggerParser().parse(readFile.result().toString(Charset.forName("utf-8")));
Router swaggerRouter = SwaggerRouter.swaggerRouter(router, swagger, vertx.eventBus(), new OperationIdServiceIdResolver());
deployVerticals(startFuture);
vertx.createHttpServer()
.requestHandler(swaggerRouter::accept)
.listen(serverPort, h -> {
if (h.succeeded()) {
startFuture.complete();
} else {
startFuture.fail(h.cause());
}
});
} else {
startFuture.fail(readFile.cause());
}
});
}
public void deployVerticals(Future startFuture) {
{{#apiInfo}}{{#apis}}
vertx.deployVerticle("{{apiPackage}}.{{classname}}Verticle", res -> {
if (res.succeeded()) {
LOGGER.info("{{classname}}Verticle : Deployed");
} else {
startFuture.fail(res.cause());
LOGGER.error("{{classname}}Verticle : Deployment failed");
}
});
{{/apis}}{{/apiInfo}}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy