/examples/dbclient/pokemons/src/main/java/io/helidon/examples/dbclient/pokemons/PokemonMain.java

https://github.com/oracle/helidon · Java · 133 lines · 63 code · 22 blank · 48 comment · 7 complexity · 55e9facc2ba0b47148f0bf1c3a79cc5a MD5 · raw file

  1. /*
  2. * Copyright (c) 2019, 2020 Oracle and/or its affiliates.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.helidon.examples.dbclient.pokemons;
  17. import java.io.IOException;
  18. import java.util.logging.LogManager;
  19. import io.helidon.config.Config;
  20. import io.helidon.config.ConfigSources;
  21. import io.helidon.dbclient.DbClient;
  22. import io.helidon.dbclient.health.DbClientHealthCheck;
  23. import io.helidon.health.HealthSupport;
  24. import io.helidon.media.jsonb.JsonbSupport;
  25. import io.helidon.media.jsonp.JsonpSupport;
  26. import io.helidon.metrics.MetricsSupport;
  27. import io.helidon.tracing.TracerBuilder;
  28. import io.helidon.webserver.Routing;
  29. import io.helidon.webserver.WebServer;
  30. /**
  31. * Simple Hello World rest application.
  32. */
  33. public final class PokemonMain {
  34. /** MongoDB configuration. Default configuration file {@code appliaction.yaml} contains JDBC configuration. */
  35. private static final String MONGO_CFG = "mongo.yaml";
  36. /** Whether MongoDB support is selected. */
  37. private static boolean mongo;
  38. static boolean isMongo() {
  39. return mongo;
  40. }
  41. /**
  42. * Cannot be instantiated.
  43. */
  44. private PokemonMain() {
  45. }
  46. /**
  47. * Application main entry point.
  48. *
  49. * @param args Command line arguments. Run with MongoDB support when 1st argument is mongo, run with JDBC support otherwise.
  50. * @throws java.io.IOException if there are problems reading logging properties
  51. */
  52. public static void main(final String[] args) throws IOException {
  53. if (args != null && args.length > 0 && args[0] != null && "mongo".equals(args[0].toLowerCase())) {
  54. System.out.println("MongoDB database selected");
  55. mongo = true;
  56. } else {
  57. System.out.println("JDBC database selected");
  58. mongo = false;
  59. }
  60. startServer();
  61. }
  62. /**
  63. * Start the server.
  64. *
  65. * @return the created {@link io.helidon.webserver.WebServer} instance
  66. * @throws java.io.IOException if there are problems reading logging properties
  67. */
  68. static WebServer startServer() throws IOException {
  69. // load logging configuration
  70. LogManager.getLogManager().readConfiguration(PokemonMain.class.getResourceAsStream("/logging.properties"));
  71. // By default this will pick up application.yaml from the classpath
  72. Config config = isMongo() ? Config.create(ConfigSources.classpath(MONGO_CFG)) : Config.create();
  73. // Prepare routing for the server
  74. Routing routing = createRouting(config);
  75. WebServer server = WebServer.builder(routing)
  76. .addMediaSupport(JsonpSupport.create())
  77. .addMediaSupport(JsonbSupport.create())
  78. .config(config.get("server"))
  79. .tracer(TracerBuilder.create(config.get("tracing")).build())
  80. .build();
  81. // Start the server and print some info.
  82. server.start()
  83. .thenAccept(ws -> System.out.println("WEB server is up! http://localhost:" + ws.port() + "/"));
  84. // Server threads are not daemon. NO need to block. Just react.
  85. server.whenShutdown()
  86. .thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
  87. return server;
  88. }
  89. /**
  90. * Creates new {@link io.helidon.webserver.Routing}.
  91. *
  92. * @param config configuration of this server
  93. * @return routing configured with JSON support, a health check, and a service
  94. */
  95. private static Routing createRouting(Config config) {
  96. Config dbConfig = config.get("db");
  97. // Client services are added through a service loader - see mongoDB example for explicit services
  98. DbClient dbClient = DbClient.builder(dbConfig)
  99. .build();
  100. HealthSupport health = HealthSupport.builder()
  101. .addLiveness(DbClientHealthCheck.create(dbClient))
  102. .build();
  103. // Initialize database schema
  104. InitializeDb.init(dbClient);
  105. return Routing.builder()
  106. .register(health) // Health at "/health"
  107. .register(MetricsSupport.create()) // Metrics at "/metrics"
  108. .register("/db", new PokemonService(dbClient))
  109. .build();
  110. }
  111. }