Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.jadala.pne; import org.jadala.auth.jwt.JsonWebTokenHandler; import org.jadala.storage.ElasticClient; import io.vertx.core.AbstractVerticle; import io.vertx.core.AsyncResult; import io.vertx.core.Future; import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.http.HttpHeaders; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServer; import io.vertx.core.json.JsonObject; import io.vertx.ext.web.Router; import io.vertx.ext.web.handler.BodyHandler; import io.vertx.ext.web.handler.CorsHandler; import io.vertx.ext.web.handler.LoggerHandler; import org.jadala.domain.NodeHandler; import org.jadala.domain.persons.PersonHandler; /** * * @author Jakob Adala */ public class Bootstrap extends AbstractVerticle { // TODO change this in production environment public static final String ORIGIN_PATTERN = "(file://)|" // used in WebApp + "(http://localhost:8100)|" // used when ionic started with 'ionic serve --lab' (OSX) + "(http://localhost:3000)|" // used when ionic started with 'ionic serve --lab' (OSX) + "(http://localhost:4200)|" // used when app started with 'ng serve' + "(http://192\\.168\\.178\\.64:4200)|" // used when app started with 'ng serve' + "(http://192\\.168\\.178\\.35:8100)|" // used when ionic started with 'ionic serve --lab' (OSX) + "(http://192\\.168\\.178\\.40:8100)|" // used when WebApp is started with livereload on device with 'ionic run --device -l android' + "(chrome-extension://.*)"; // chrome plugin postman adds origin chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm public static void main(String[] args) { Vertx vertx = Vertx.vertx(); vertx.deployVerticle(Bootstrap.class.getName()); } @Override public void start(Future<Void> startFuture) throws Exception { final String originPattern = this.config().getString("origin_pattern", ORIGIN_PATTERN); final int httpPort = this.config().getInteger("http_port", 8080); String cp = this.config().getString("context", "/"); if (!cp.endsWith("/")) { cp = cp + "/"; } if (!cp.startsWith("/")) { cp = "/" + cp; } final String contextPath = cp; Router router = Router.router(vertx); router.route().handler(LoggerHandler.create(LoggerHandler.DEFAULT_FORMAT)); // setup CORS CorsHandler corsHandler = CorsHandler.create(originPattern).allowCredentials(true) .allowedHeader(HttpHeaders.ACCEPT.toString()).allowedHeader(HttpHeaders.ORIGIN.toString()) .allowedHeader(HttpHeaders.AUTHORIZATION.toString()) .allowedHeader(HttpHeaders.CONTENT_TYPE.toString()).allowedHeader(HttpHeaders.COOKIE.toString()) .exposedHeader(HttpHeaders.SET_COOKIE.toString()).allowedMethod(HttpMethod.POST) .allowedMethod(HttpMethod.PUT).allowedMethod(HttpMethod.GET).allowedMethod(HttpMethod.DELETE); router.route(contextPath + "api/*").handler(corsHandler); router.route(contextPath + "api/*").method(HttpMethod.POST).method(HttpMethod.PUT) .handler(BodyHandler.create()); // config jwt service JsonWebTokenHandler jwtService = new JsonWebTokenHandler(vertx, config().getJsonObject("keyStore")); router.post(contextPath + "api/jwt").handler(jwtService); // status page String version = config().getString("version", "unknown"); router.get(contextPath).handler(new StatusPageHandler(version)); router.post(contextPath + "api/node").handler(new NodeHandler(vertx)); router.get(contextPath + "api/node").handler(new NodeHandler(vertx)); // router.post(contextPath + "api/organisation").handler(new PersonHandler(vertx)); // router.get(contextPath + "api/organisation").handler(new PersonHandler(vertx)); vertx.deployVerticle(ElasticClient.class.getName(), deploymentHandler("ElasticClient")); vertx.createHttpServer().requestHandler(router::accept).listen(httpPort, (AsyncResult<HttpServer> asyncHttpServer) -> { if (asyncHttpServer.succeeded()) { System.out .println("npe server started. listen at http://0.0.0.0:" + httpPort + contextPath); startFuture.complete(); } else { startFuture.fail(asyncHttpServer.cause()); } }); } Handler<AsyncResult<String>> deploymentHandler(final String name) { return (AsyncResult<String> deployAsync) -> { if (deployAsync.failed()) { System.out.println(name + " deployment FAILED." + (deployAsync.cause() != null ? deployAsync.cause().getMessage() : "")); } else { System.out.println(name + " deployed"); } }; } }