Search in sources :

Example 66 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class ServerCdiExtension method addApplication.

private void addApplication(JaxRsCdiExtension jaxRs, JaxRsApplication applicationMeta, InjectionManager injectionManager) {
    LOGGER.info("Registering JAX-RS Application: " + applicationMeta.appName());
    Optional<String> contextRoot = jaxRs.findContextRoot(config, applicationMeta);
    Optional<String> namedRouting = jaxRs.findNamedRouting(config, applicationMeta);
    boolean routingNameRequired = jaxRs.isNamedRoutingRequired(config, applicationMeta);
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.finest("Application " + applicationMeta.appName() + ", class: " + applicationMeta.appClassName() + ", contextRoot: " + contextRoot + ", namedRouting: " + namedRouting + ", routingNameRequired: " + routingNameRequired);
    }
    Routing.Builder routing = routingBuilder(namedRouting, routingNameRequired, applicationMeta.appName());
    JerseySupport jerseySupport = jaxRs.toJerseySupport(jaxRsExecutorService, applicationMeta, injectionManager);
    if (contextRoot.isPresent()) {
        String contextRootString = contextRoot.get();
        LOGGER.fine(() -> "JAX-RS application " + applicationMeta.appName() + " registered on '" + contextRootString + "'");
        routing.register(contextRootString, jerseySupport);
    } else {
        LOGGER.fine(() -> "JAX-RS application " + applicationMeta.appName() + " registered on '/'");
        routing.register(jerseySupport);
    }
    jerseySupports.add(jerseySupport);
}
Also used : Routing(io.helidon.webserver.Routing) JerseySupport(io.helidon.webserver.jersey.JerseySupport)

Example 67 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class ReactiveVaultMain method main.

/**
 * Main method.
 * @param args ignored
 */
public static void main(String[] args) {
    LogConfig.configureRuntime();
    // as I cannot share my secret configuration, let's combine the configuration
    // from my home directory with the one compiled into the jar
    // when running this example, you can either update the application.yaml in resources directory
    // or use the same approach
    Config config = buildConfig();
    // we have three configurations available
    // 1. Token based authentication
    Vault tokenVault = Vault.builder().config(config.get("vault.token")).updateWebClient(it -> it.connectTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS)).build();
    // 2. App role based authentication - must be created after we obtain the role id an token
    // 3. Kubernetes (k8s) based authentication (requires to run on k8s) - must be created after we create
    // the authentication method
    // the tokenVault is using the root token and can be used to enable engines and
    // other authentication mechanisms
    CompletionAwaitable<Void> k8sFuture = new K8sExample(tokenVault, config.get("vault.k8s")).run().forSingle(System.out::println);
    CompletionAwaitable<Void> appRoleFuture = new AppRoleExample(tokenVault, config.get("vault.approle")).run().forSingle(System.out::println);
    /*
        We do not need to block here for our examples, as the server started below will keep the process running
         */
    SysRx sys = tokenVault.sys(SysRx.API);
    // we use await for webserver, as we do not care if we block the main thread - it is not used
    // for anything
    WebServer webServer = WebServer.builder().config(config.get("server")).routing(Routing.builder().register("/cubbyhole", new CubbyholeService(sys, tokenVault.secrets(CubbyholeSecretsRx.ENGINE))).register("/kv1", new Kv1Service(sys, tokenVault.secrets(Kv1SecretsRx.ENGINE))).register("/kv2", new Kv2Service(sys, tokenVault.secrets(Kv2SecretsRx.ENGINE))).register("/transit", new TransitService(sys, tokenVault.secrets(TransitSecretsRx.ENGINE)))).build().start().await();
    try {
        appRoleFuture.await();
    } catch (Exception e) {
        System.err.println("AppRole example failed");
        e.printStackTrace();
    }
    try {
        k8sFuture.await();
    } catch (Exception e) {
        System.err.println("Kubernetes example failed");
        e.printStackTrace();
    }
    String baseAddress = "http://localhost:" + webServer.port() + "/";
    System.out.println("Server started on " + baseAddress);
    System.out.println();
    System.out.println("Key/Value Version 1 Secrets Engine");
    System.out.println("\t" + baseAddress + "kv1/enable");
    System.out.println("\t" + baseAddress + "kv1/create");
    System.out.println("\t" + baseAddress + "kv1/secrets/first/secret");
    System.out.println("\tcurl -i -X DELETE " + baseAddress + "kv1/secrets/first/secret");
    System.out.println("\t" + baseAddress + "kv1/disable");
    System.out.println();
    System.out.println("Key/Value Version 2 Secrets Engine");
    System.out.println("\t" + baseAddress + "kv2/create");
    System.out.println("\t" + baseAddress + "kv2/secrets/first/secret");
    System.out.println("\tcurl -i -X DELETE " + baseAddress + "kv2/secrets/first/secret");
    System.out.println();
    System.out.println("Transit Secrets Engine");
    System.out.println("\t" + baseAddress + "transit/enable");
    System.out.println("\t" + baseAddress + "transit/keys");
    System.out.println("\t" + baseAddress + "transit/encrypt/secret_text");
    System.out.println("\t" + baseAddress + "transit/decrypt/cipher_text");
    System.out.println("\t" + baseAddress + "transit/sign");
    System.out.println("\t" + baseAddress + "transit/verify/sign/signature_text");
    System.out.println("\t" + baseAddress + "transit/hmac");
    System.out.println("\t" + baseAddress + "transit/verify/hmac/hmac_text");
    System.out.println("\tcurl -i -X DELETE " + baseAddress + "transit/keys");
    System.out.println("\t" + baseAddress + "transit/disable");
}
Also used : TransitSecretsRx(io.helidon.integrations.vault.secrets.transit.TransitSecretsRx) Kv2SecretsRx(io.helidon.integrations.vault.secrets.kv2.Kv2SecretsRx) Config(io.helidon.config.Config) SysRx(io.helidon.integrations.vault.sys.SysRx) CompletionAwaitable(io.helidon.common.reactive.CompletionAwaitable) CubbyholeSecretsRx(io.helidon.integrations.vault.secrets.cubbyhole.CubbyholeSecretsRx) TimeUnit(java.util.concurrent.TimeUnit) Vault(io.helidon.integrations.vault.Vault) ConfigSources.file(io.helidon.config.ConfigSources.file) WebServer(io.helidon.webserver.WebServer) Kv1SecretsRx(io.helidon.integrations.vault.secrets.kv1.Kv1SecretsRx) ConfigSources.classpath(io.helidon.config.ConfigSources.classpath) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) WebServer(io.helidon.webserver.WebServer) SysRx(io.helidon.integrations.vault.sys.SysRx) Vault(io.helidon.integrations.vault.Vault)

Example 68 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class Main method startServer.

/**
 * Start the server.
 * @return the created {@link WebServer} instance
 */
static Single<WebServer> startServer() {
    // load logging configuration
    LogConfig.configureRuntime();
    // By default this will pick up application.yaml from the classpath
    Config config = Config.create();
    WebServer server = WebServer.builder().tracer(TracerBuilder.create(config.get("tracing"))).routing(createRouting(config)).config(config.get("server")).addMediaSupport(JsonpSupport.create()).build();
    Single<WebServer> 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! http://localhost:" + ws.port() + "/greet");
        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);
    });
    return webserver;
}
Also used : TracerBuilder(io.helidon.tracing.TracerBuilder) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) Single(io.helidon.common.reactive.Single) MetricsSupport(io.helidon.metrics.MetricsSupport) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) WebServer(io.helidon.webserver.WebServer) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig)

Example 69 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class Se1Main method startServer.

/**
 * Start the server.
 * @return the created {@link io.helidon.webserver.WebServer} instance
 */
static WebServer startServer() {
    // load logging configuration
    LogConfig.configureRuntime();
    // By default this will pick up application.yaml from the classpath
    Config config = buildConfig();
    // Get webserver config from the "server" section of application.yaml
    WebServer server = WebServer.builder().routing(createRouting(config)).config(config.get("server")).tracer(TracerBuilder.create(config.get("tracing")).build()).addMediaSupport(JsonpSupport.create()).addMediaSupport(JsonbSupport.create()).printFeatureDetails(true).build();
    // Try to start the server. If successful, print some info and arrange to
    // print a message at shutdown. If unsuccessful, print the exception.
    server.start().thenAccept(ws -> {
        System.out.println("WEB server is up! http://localhost:" + ws.port() + "/greet");
        ws.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    return server;
}
Also used : ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) JsonbSupport(io.helidon.media.jsonb.JsonbSupport) Config(io.helidon.config.Config) Set(java.util.Set) HealthSupport(io.helidon.health.HealthSupport) TracerBuilder(io.helidon.tracing.TracerBuilder) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) HealthCheckResponse(org.eclipse.microprofile.health.HealthCheckResponse) ConfigSources.file(io.helidon.config.ConfigSources.file) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport) Paths(java.nio.file.Paths) FileSystemWatcher(io.helidon.config.FileSystemWatcher) WebServer(io.helidon.webserver.WebServer) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) MetricsSupport(io.helidon.metrics.MetricsSupport) TyrusSupport(io.helidon.webserver.tyrus.TyrusSupport) ConfigSources.classpath(io.helidon.config.ConfigSources.classpath) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) HealthChecks(io.helidon.health.checks.HealthChecks) WebServer(io.helidon.webserver.WebServer) ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig)

Example 70 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class Server method main.

/**
 * The main program entry point.
 *
 * @param args  the program arguments
 *
 * @throws Exception  if an error occurs
 */
public static void main(String[] args) throws Exception {
    // By default this will pick up application.yaml from the classpath
    Config config = Config.create();
    // load logging configuration
    LogConfig.configureRuntime();
    // Get gRPC server config from the "grpc" section of application.yaml
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder(config.get("grpc")).build();
    GrpcRouting grpcRouting = GrpcRouting.builder().intercept(// global metrics - all service methods counted
    GrpcMetrics.counted()).register(// GreetService uses global metrics so all methods are counted
    new GreetService(config)).register(new StringService(), rules -> {
        // service level metrics - StringService overrides global so that its methods are timed
        rules.intercept(GrpcMetrics.timed()).intercept("Upper", GrpcMetrics.histogram());
    }).build();
    GrpcServer grpcServer = GrpcServer.create(serverConfig, grpcRouting);
    // Try to start the server. If successful, print some info and arrange to
    // print a message at shutdown. If unsuccessful, print the exception.
    grpcServer.start().thenAccept(s -> {
        System.out.println("gRPC server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    // start web server with the metrics endpoints
    Routing routing = Routing.builder().register(MetricsSupport.create()).build();
    WebServer.create(routing, config.get("webserver")).start().thenAccept(s -> {
        System.out.println("HTTP server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("HTTP server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
}
Also used : StringService(io.helidon.grpc.examples.common.StringService) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) GrpcServer(io.helidon.grpc.server.GrpcServer) GreetService(io.helidon.grpc.examples.common.GreetService) MetricsSupport(io.helidon.metrics.MetricsSupport) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) GrpcMetrics(io.helidon.grpc.metrics.GrpcMetrics) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) GreetService(io.helidon.grpc.examples.common.GreetService) Routing(io.helidon.webserver.Routing) GrpcRouting(io.helidon.grpc.server.GrpcRouting) StringService(io.helidon.grpc.examples.common.StringService) GrpcServer(io.helidon.grpc.server.GrpcServer) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Aggregations

Routing (io.helidon.webserver.Routing)86 WebServer (io.helidon.webserver.WebServer)38 Config (io.helidon.config.Config)33 Test (org.junit.jupiter.api.Test)32 Http (io.helidon.common.http.Http)23 TimeUnit (java.util.concurrent.TimeUnit)21 LogConfig (io.helidon.common.LogConfig)19 MediaType (io.helidon.common.http.MediaType)19 CoreMatchers.is (org.hamcrest.CoreMatchers.is)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)17 SecurityContext (io.helidon.security.SecurityContext)16 HttpException (io.helidon.webserver.HttpException)15 Optional (java.util.Optional)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)13 SERVICE_UNAVAILABLE_503 (io.helidon.common.http.Http.Status.SERVICE_UNAVAILABLE_503)11 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)11 Security (io.helidon.security.Security)11 StaticContentSupport (io.helidon.webserver.staticcontent.StaticContentSupport)10 TestResponse (io.helidon.webserver.testsupport.TestResponse)10