PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/crossover/atrs/service/AtrsServer.java

https://gitlab.com/devsathish/atrs
Java | 91 lines | 57 code | 20 blank | 14 comment | 3 complexity | 288dc5823cbd049b4dec5d2eb43b9b3e MD5 | raw file
  1. package com.crossover.atrs.service;
  2. import com.crossover.atrs.service.config.AtrsConfig;
  3. import com.crossover.atrs.service.module.AtrsModule;
  4. import com.cyngn.vertx.web.JsonUtil;
  5. import com.cyngn.vertx.web.RestApi;
  6. import com.cyngn.vertx.web.RouterTools;
  7. import com.google.inject.Guice;
  8. import com.google.inject.Inject;
  9. import io.vertx.core.AbstractVerticle;
  10. import io.vertx.core.Future;
  11. import io.vertx.core.http.HttpServer;
  12. import io.vertx.core.shareddata.LocalMap;
  13. import io.vertx.ext.web.Router;
  14. import io.vertx.ext.web.handler.BodyHandler;
  15. import io.vertx.ext.web.handler.CorsHandler;
  16. import io.vertx.ext.web.handler.StaticHandler;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import java.util.List;
  20. /**
  21. * Created by spalanisamy on 07/08/16.
  22. */
  23. public class AtrsServer extends AbstractVerticle {
  24. // Logging setup. Vertx startup related log goes here.
  25. private static final Logger logger = LoggerFactory.getLogger(AtrsServer.class);
  26. private HttpServer server;
  27. private AtrsModule module;
  28. private AtrsConfig config;
  29. @Inject
  30. private List<RestApi> apis;
  31. // local shared map, shared across all vert.x instances
  32. private LocalMap<String, Long> initializationMap;
  33. @Override
  34. public void start(Future<Void> future) {
  35. Router router = Router.router(vertx);
  36. config = JsonUtil.parseJsonToObject(vertx.getOrCreateContext().config().toString(), AtrsConfig.class);
  37. logger.error("Server Config "+config.getPort()+config.getUploadDirectory());
  38. BodyHandler bodyHandler = BodyHandler.create(config.getUploadDirectory());
  39. module = new AtrsModule(vertx, config, onReady -> {
  40. // Create Guice injector and initialize this Verticle instance.
  41. Guice.createInjector(module).injectMembers(this);
  42. RouterTools.registerRootHandlers(router, bodyHandler);
  43. for (RestApi api : apis) {
  44. api.init(router);
  45. api.outputApi(logger);
  46. }
  47. // Handler for static resources (css/js/html etc)
  48. router.route("/s/*").handler(StaticHandler.create("webroot"));
  49. router.route("/").handler(ctx -> { ctx.response().sendFile("webroot/index.html").end(); });
  50. server = createHttpServer(future, config.getPort(), router);
  51. });
  52. }
  53. /**
  54. * Local method to create vertx server.
  55. * @param future
  56. * @param port
  57. * @param router
  58. * @return
  59. */
  60. private HttpServer createHttpServer(Future<Void> future, int port, Router router) {
  61. return vertx.createHttpServer()
  62. .requestHandler(router::accept)
  63. .listen(port, result -> {
  64. if (result.succeeded()) {
  65. future.complete();
  66. } else {
  67. future.fail(result.cause());
  68. }
  69. });
  70. }
  71. }