Search in sources :

Example 21 with WebServer

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

the class BasicExampleConfigMain method startServer.

static WebServer startServer() {
    LogConfig.initClass();
    Config config = Config.create();
    Routing routing = Routing.builder().register(WebSecurity.create(config.get("security"))).register("/static", StaticContentSupport.create("/WEB")).get("/{*}", (req, res) -> {
        Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
        res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
        res.send("Hello, you are: \n" + securityContext.map(ctx -> ctx.user().orElse(SecurityContext.ANONYMOUS).toString()).orElse("Security context is null"));
    }).build();
    return WebServer.builder().config(config.get("server")).routing(routing).build().start().await(10, TimeUnit.SECONDS);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) SecurityContext(io.helidon.security.SecurityContext) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) MediaType(io.helidon.common.http.MediaType) Optional(java.util.Optional) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) SecurityContext(io.helidon.security.SecurityContext) Routing(io.helidon.webserver.Routing)

Example 22 with WebServer

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

the class BasicExampleUtil method startAndPrintEndpoints.

static void startAndPrintEndpoints(Supplier<WebServer> startMethod) {
    long t = System.nanoTime();
    WebServer webServer = startMethod.get();
    long time = System.nanoTime() - t;
    System.out.printf("Server started in %d ms ms%n", TimeUnit.MILLISECONDS.convert(time, TimeUnit.NANOSECONDS));
    System.out.printf("Started server on localhost:%d%n", webServer.port());
    System.out.println();
    System.out.println("Users:");
    System.out.println("Jack/password in roles: user, admin");
    System.out.println("Jill/password in roles: user");
    System.out.println("John/password in no roles");
    System.out.println();
    System.out.println("***********************");
    System.out.println("** Endpoints:        **");
    System.out.println("***********************");
    System.out.println("No authentication:");
    System.out.printf("  http://localhost:%1$d/public%n", webServer.port());
    System.out.println("No roles required, authenticated:");
    System.out.printf("  http://localhost:%1$d/noRoles%n", webServer.port());
    System.out.println("User role required:");
    System.out.printf("  http://localhost:%1$d/user%n", webServer.port());
    System.out.println("Admin role required:");
    System.out.printf("  http://localhost:%1$d/admin%n", webServer.port());
    System.out.println("Always forbidden (uses role nobody is in), audited:");
    System.out.printf("  http://localhost:%1$d/deny%n", webServer.port());
    System.out.println("Admin role required, authenticated, authentication optional, audited (always forbidden - challenge is not " + "returned as authentication is optional):");
    System.out.printf("  http://localhost:%1$d/noAuthn%n", webServer.port());
    System.out.println("Static content, requires user role:");
    System.out.printf("  http://localhost:%1$d/static/index.html%n", webServer.port());
    System.out.println();
}
Also used : WebServer(io.helidon.webserver.WebServer)

Example 23 with WebServer

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

the class GoogleUtil method startIt.

static WebServer startIt(int port, Supplier<? extends Routing> routing) {
    WebServer server = WebServer.builder(routing).port(port).build();
    long t = System.nanoTime();
    CountDownLatch cdl = new CountDownLatch(1);
    server.start().thenAccept(webServer -> {
        long time = System.nanoTime() - t;
        System.out.printf("Server started in %d ms ms%n", TimeUnit.MILLISECONDS.convert(time, TimeUnit.NANOSECONDS));
        System.out.printf("Started server on localhost:%d%n", webServer.port());
        System.out.printf("You can access this example at http://localhost:%d/index.html%n", webServer.port());
        System.out.println();
        System.out.println();
        System.out.println("Check application.yaml in case you are behind a proxy to configure it");
        cdl.countDown();
    });
    try {
        cdl.await(START_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        throw new RuntimeException("Failed to start server within defined timeout: " + START_TIMEOUT_SECONDS + " seconds");
    }
    return server;
}
Also used : WebServer(io.helidon.webserver.WebServer) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 24 with WebServer

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

the class Main method startServer.

/**
 * Start the server.
 *
 * @return the created {@link WebServer} instance
 */
static Single<WebServer> startServer(Config config) {
    // load logging configuration
    LogConfig.configureRuntime();
    // Build server using three ports:
    // default public port, admin port, private port
    WebServer server = WebServer.builder(createPublicRouting(config)).config(config.get("server")).addMediaSupport(JsonpSupport.create()).build();
    Single<WebServer> webServerSingle = 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.
    webServerSingle.thenAccept(ws -> {
        System.out.println("WEB server is up! http://localhost:" + ws.port());
        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 webServerSingle;
}
Also used : 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) HealthSupport(io.helidon.health.HealthSupport) HealthChecks(io.helidon.health.checks.HealthChecks) WebServer(io.helidon.webserver.WebServer)

Example 25 with WebServer

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

the class Main method startServer.

/**
 * Start the WebServer based on the provided configuration. When running from
 * a test, pass {@link null} to have a dynamically allocated port
 * the server listens on.
 *
 * @param port port to start server on
 * @return a completion stage indicating that the server has started and is ready to
 * accept http requests
 */
static CompletionStage<WebServer> startServer(int port) {
    WebServer webServer = WebServer.builder(Routing.builder().register("/jersey", JerseySupport.create(new ResourceConfig(HelloWorld.class))).build()).port(port).build();
    return webServer.start().whenComplete((server, t) -> {
        System.out.println("Jersey WebServer started.");
        System.out.println("To stop the application, hit CTRL+C");
        System.out.println("Try the hello world resource at: http://localhost:" + server.port() + "/jersey/hello");
    });
}
Also used : WebServer(io.helidon.webserver.WebServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Aggregations

WebServer (io.helidon.webserver.WebServer)88 Config (io.helidon.config.Config)49 Routing (io.helidon.webserver.Routing)48 LogConfig (io.helidon.common.LogConfig)34 Single (io.helidon.common.reactive.Single)17 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)16 MetricsSupport (io.helidon.metrics.MetricsSupport)16 HealthSupport (io.helidon.health.HealthSupport)15 HealthChecks (io.helidon.health.checks.HealthChecks)13 ConfigSources (io.helidon.config.ConfigSources)10 TimeUnit (java.util.concurrent.TimeUnit)10 WebClient (io.helidon.webclient.WebClient)9 WebClientResponse (io.helidon.webclient.WebClientResponse)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 Test (org.junit.jupiter.api.Test)9 Http (io.helidon.common.http.Http)8 MediaType (io.helidon.common.http.MediaType)8 IOException (java.io.IOException)8 SecurityContext (io.helidon.security.SecurityContext)7 StaticContentSupport (io.helidon.webserver.staticcontent.StaticContentSupport)7