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);
}
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();
}
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;
}
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;
}
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");
});
}
Aggregations