use of io.helidon.webserver.Routing in project helidon by oracle.
the class MetricsSupport method setUpFullFeaturedEndpoint.
private void setUpFullFeaturedEndpoint(Routing.Rules serviceEndpointRoutingRules, io.helidon.metrics.RegistryFactory rf) {
Registry base = rf.getARegistry(MetricRegistry.Type.BASE);
Registry vendor = rf.getARegistry(MetricRegistry.Type.VENDOR);
Registry app = rf.getARegistry(MetricRegistry.Type.APPLICATION);
// routing to root of metrics
serviceEndpointRoutingRules.get(context(), (req, res) -> getMultiple(req, res, base, app, vendor)).options(context(), (req, res) -> optionsMultiple(req, res, base, app, vendor));
// routing to each scope
Stream.of(app, base, vendor).forEach(registry -> {
String type = registry.type();
serviceEndpointRoutingRules.get(context() + "/" + type, (req, res) -> getAll(req, res, registry)).get(context() + "/" + type + "/{metric}", (req, res) -> getByName(req, res, registry)).options(context() + "/" + type, (req, res) -> optionsAll(req, res, registry)).options(context() + "/" + type + "/{metric}", (req, res) -> optionsOne(req, res, registry));
});
}
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
*/
public static void main(String[] args) {
// 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();
GrpcServer grpcServer = GrpcServer.create(serverConfig, createRouting(config));
// 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;
});
// add support for standard and gRPC health checks
HealthSupport health = HealthSupport.builder().addLiveness(HealthChecks.healthChecks()).addLiveness(grpcServer.healthChecks()).build();
// start web server with health endpoint
Routing routing = Routing.builder().register(health).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;
});
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class BasicExampleBuilderMain method startServer.
static WebServer startServer() {
LogConfig.initClass();
Routing routing = Routing.builder().register(buildWebSecurity().securityDefaults(WebSecurity.authenticate())).any("/static[/{*}]", WebSecurity.rolesAllowed("user")).register("/static", StaticContentSupport.create("/WEB")).get("/noRoles", WebSecurity.enforce()).get("/user[/{*}]", WebSecurity.rolesAllowed("user")).get("/admin", WebSecurity.rolesAllowed("admin")).get("/deny", WebSecurity.rolesAllowed("deny").audit()).any("/noAuthn", WebSecurity.rolesAllowed("admin").authenticationOptional().audit()).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().routing(routing).build().start().await(10, TimeUnit.SECONDS);
}
use of io.helidon.webserver.Routing 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.Routing in project helidon by oracle.
the class GoogleBuilderMain method start.
static int start(int port) {
Security security = Security.builder().addProvider(GoogleTokenProvider.builder().clientId("your-client-id.apps.googleusercontent.com")).build();
WebSecurity ws = WebSecurity.create(security);
Routing.Builder routing = Routing.builder().register(ws).get("/rest/profile", WebSecurity.authenticate(), (req, res) -> {
Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
res.send("Response from builder based service, you are: \n" + securityContext.flatMap(SecurityContext::user).map(Subject::toString).orElse("Security context is null"));
req.next();
}).register(StaticContentSupport.create("/WEB"));
theServer = GoogleUtil.startIt(port, routing);
return theServer.port();
}
Aggregations