All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java-helidon.server.libraries.se.main.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
package {{invokerPackage}};
{{#apiInfo}}{{#apis}}{{#operations}}
import {{package}}.{{classname}}Impl;{{/operations}}{{/apis}}{{/apiInfo}}

import io.helidon{{^x-helidon-v3}}.logging{{/x-helidon-v3}}.common.LogConfig;
{{#x-helidon-v3}}
import io.helidon.common.reactive.Single;
{{/x-helidon-v3}}
import io.helidon.config.Config;
{{#x-helidon-v3}}
import io.helidon.health.HealthSupport;
import io.helidon.health.checks.HealthChecks;
import io.helidon.media.jsonp.JsonpSupport;
{{/x-helidon-v3}}
{{#jsonb}}
{{#x-helidon-v3}}
import io.helidon.media.jsonb.JsonbSupport;
{{/x-helidon-v3}}
{{/jsonb}}
{{#jackson}}
{{#x-helidon-v3}}
import io.helidon.media.jackson.JacksonSupport;
import {{apiPackage}}.JsonProvider;
{{/x-helidon-v3}}
{{/jackson}}
{{#x-helidon-v3}}
import io.helidon.metrics.MetricsSupport;
import io.helidon.openapi.OpenAPISupport;
import io.helidon.webserver.Routing;
{{/x-helidon-v3}}
{{^x-helidon-v3}}
import io.helidon.webserver.http.HttpRouting;
{{/x-helidon-v3}}
import io.helidon.webserver.WebServer;

/**
* The application main class.
*/
public final class Main {

    /**
    * Cannot be instantiated.
    */
    private Main() {
    }

    /**
    * Application main entry point.
    * @param args command line arguments.
    */
    public static void main(final String[] args) {
        startServer();
    }

    /**
    * Start the server.
    * @return the created {@link WebServer} instance
    */
    static {{#x-helidon-v3}}Single<{{/x-helidon-v3}}WebServer{{#x-helidon-v3}}>{{/x-helidon-v3}} startServer() {

        // load logging configuration
        LogConfig.configureRuntime();

        // By default this will pick up application.yaml from the classpath
        Config config = Config.create();
{{^x-helidon-v3}}
        Config.global(config);
{{/x-helidon-v3}}

{{#x-helidon-v3}}
        WebServer server = WebServer.builder(createRouting(config))
                                    .config(config.get("server"))
                                    .addMediaSupport(JsonpSupport.create())
{{#jsonb}}
                                    .addMediaSupport(JsonbSupport.create())
{{/jsonb}}
{{#jackson}}
                                    .addMediaSupport(JacksonSupport.create(JsonProvider.objectMapper()))
{{/jackson}}
                                    .build();

        Single webserver = server.start();

        // Try to start the server. If successful, print some info and arrange to
        // print a message at shutdown. If unsuccessful, print the exception.
        webserver.thenAccept(ws -> {
                     System.out.println("WEB server is up! {{{basePath}}}");
                     ws.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
                 })
                 .exceptionallyAccept(t -> {
                     System.err.println("Startup failed: " + t.getMessage());
                     t.printStackTrace(System.err);
                 });
{{/x-helidon-v3}}
{{^x-helidon-v3}}
        WebServer webserver = WebServer.builder()
                .config(config.get("server"))
                .routing(Main::routing)
                .build()
                .start();

        System.out.println("WEB server is up! {{{basePath}}}");
{{/x-helidon-v3}}

        return webserver;
    }

{{#x-helidon-v3}}
    /**
    * Creates new {@link Routing}.
    *
    * @return routing configured with JSON support, a health check, and a service
    * @param config configuration of this server
    */
    private static Routing createRouting(Config config) {

        MetricsSupport metrics = MetricsSupport.create();
        HealthSupport health = HealthSupport.builder()
                                            .addLiveness(HealthChecks.healthChecks())   // Adds a convenient set of checks
                                            .build();

        return Routing.builder()
                      .register(OpenAPISupport.create(config.get(OpenAPISupport.Builder.CONFIG_KEY)))
                      .register(health)                   // Health at "/health"
                      .register(metrics)                  // Metrics at "/metrics"{{#apiInfo}}{{#apis}}{{#operations}}
                      .register("/", new {{classname}}Impl()){{/operations}}{{/apis}}{{/apiInfo}}
                      .build();
    }
{{/x-helidon-v3}}
{{^x-helidon-v3}}
    /**
     * Updates HTTP routing and implicitly registers observe providers.
     */
    static void routing(HttpRouting.Builder routing) {
        routing{{#apiInfo}}{{#apis}}{{#operations}}
            .register("{{#x-helidon-v3}}/{{/x-helidon-v3}}{{{pathPrefix}}}", new {{classname}}Impl()){{^x-helidon-v3}}{{#x-helidon-fixupPath}}/* {{.}} */{{/x-helidon-fixupPath}}{{/x-helidon-v3}}{{/operations}}{{/apis}}{{/apiInfo}};
    }
{{/x-helidon-v3}}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy