public void startHttpServer() throws Exception { final Http http = Http.get(system); final ActorMaterializer materializer = ActorMaterializer.create(system); ActorRef userRegistryActor = system.actorOf(ServiceRegistryActor.props(), "serviceRegistryActor"); ServiceRoutes serviceRoutes = new ServiceRoutes(system, userRegistryActor); final Flow<HttpRequest, HttpResponse, NotUsed> routeFlow = serviceRoutes.routes().flow(system, materializer); final CompletionStage<ServerBinding> binding = http.bindAndHandle(routeFlow, ConnectHttp.toHost("localhost", 8096), materializer); System.out.println("Server online at http://localhost:8096/"); System.in.read(); // let it run until user presses return }
private Route buildApiSubRoutes(final RequestContext ctx, final DittoHeaders dittoHeaders, final AuthorizationContext authorizationContext) { final Route customApiSubRoutes = customApiRoutesProvider.authorized(dittoHeaders); return Directives.route( // /api/{apiVersion}/policies policiesRoute.buildPoliciesRoute(ctx, dittoHeaders), // /api/{apiVersion}/things SSE support sseThingsRoute.buildThingsSseRoute(ctx, () -> overwriteDittoHeaders(ctx, dittoHeaders, CustomHeadersHandler.RequestType.SSE, authorizationContext)), // /api/{apiVersion}/things thingsRoute.buildThingsRoute(ctx, dittoHeaders), // /api/{apiVersion}/search/things thingSearchRoute.buildSearchRoute(ctx, dittoHeaders) ).orElse(customApiSubRoutes); }
.bindAndHandle(routes().flow(theSystem, materializer), ConnectHttp.toHost(host, port), settings,
/** * Builds the {@code /status} route. * * @return the {@code /status} route. */ public Route buildStatusRoute() { return pathPrefix(PATH_STATUS, () -> // /status/* get(() -> // GET route( pathEndOrSingleSlash(() -> // /status complete( HttpResponse.create().withStatus(StatusCodes.OK) .withEntity(ContentTypes.APPLICATION_JSON, Status.provideStaticStatus().toString()) ) ), path(PATH_HEALTH, healthRouteSupplier), // /status/health path(PATH_CLUSTER, () -> complete( // /status/cluster HttpResponse.create().withStatus(StatusCodes.OK) .withEntity(ContentTypes.APPLICATION_JSON, clusterStateSupplier.get().toJson().toString())) ) ) ).orElse(complete(StatusCodes.METHOD_NOT_ALLOWED))); }
.bindAndHandle(routes().flow(theSystem, materializer), ConnectHttp.toHost(host, port), settings,
/** * Builds the {@code /status} route. * * @return the {@code /status} route. */ public Route buildStatusRoute() { return pathPrefix(PATH_STATUS, () -> // /status/* get(() -> // GET route( pathEndOrSingleSlash(() -> // /status complete( HttpResponse.create().withStatus(StatusCodes.OK) .withEntity(ContentTypes.APPLICATION_JSON, Status.provideStaticStatus().toString()) ) ), path(PATH_HEALTH, healthRouteSupplier), // /status/health path(PATH_CLUSTER, () -> complete( // /status/cluster HttpResponse.create().withStatus(StatusCodes.OK) .withEntity(ContentTypes.APPLICATION_JSON, clusterStateSupplier.get().toJson().toString())) ) ) ).orElse(complete(StatusCodes.METHOD_NOT_ALLOWED))); }
.bindAndHandle(routes().flow(theSystem, materializer), ConnectHttp.toHost(host, port), settings,
private Route api(final RequestContext ctx, final String correlationId) { return rawPathPrefix(mergeDoubleSlashes().concat(HTTP_PATH_API_PREFIX), () -> // /api customApiRoutesProvider.unauthorized(apiVersion, correlationId).orElse( apiAuthentication(correlationId, authContextWithPrefixedSubjects ->
/** * Starts Prometheus HTTP endpoint on which Prometheus may scrape the data. */ private void startKamonPrometheusHttpEndpoint(final ActorSystem actorSystem) { if (configReader.metrics().isPrometheusEnabled() && prometheusReporter != null) { final ActorMaterializer materializer = createActorMaterializer(actorSystem); final Route prometheusReporterRoute = PrometheusReporterRoute .buildPrometheusReporterRoute(prometheusReporter); final CompletionStage<ServerBinding> binding = Http.get(actorSystem) .bindAndHandle(prometheusReporterRoute.flow(actorSystem, materializer), ConnectHttp.toHost(configReader.metrics().getPrometheusHostname(), configReader.metrics().getPrometheusPort()), materializer); binding.thenAccept(theBinding -> CoordinatedShutdown.get(actorSystem).addTask( CoordinatedShutdown.PhaseServiceUnbind(), "shutdown_prometheus_http_endpoint", () -> { logger.info("Gracefully shutting down Prometheus HTTP endpoint.."); // prometheus requests don't get the luxury of being processed a long time after shutdown: return theBinding.terminate(Duration.ofSeconds(1)) .handle((httpTerminated, e) -> Done.getInstance()); }) ).exceptionally(failure -> { logger.error("Kamon Prometheus HTTP endpoint could not be started: {}", failure.getMessage(), failure); logger.error("Terminating actorSystem!"); actorSystem.terminate(); return null; }); } }
).orElse( // GET /things/<thingId>/attributes/<attributePointerStr> extractUnmatchedPath(attributePointerStr -> handlePerRequest(ctx, RetrieveAttribute attributesJson), dittoHeaders)) ).orElse( // PUT /things/<thingId>/attributes/<attributePointerStr> extractUnmatchedPath(attributePointerStr -> handlePerRequest(ctx, dittoHeaders, payloadSource, ).orElse( // DELETE /things/<thingId>/attributes/<attributePointerStr> extractUnmatchedPath(attributePointerStr -> handlePerRequest(ctx, DeleteAttribute
/** * Starts Prometheus HTTP endpoint on which Prometheus may scrape the data. */ private void startKamonPrometheusHttpEndpoint(final ActorSystem actorSystem) { if (configReader.metrics().isPrometheusEnabled() && prometheusReporter != null) { final ActorMaterializer materializer = createActorMaterializer(actorSystem); final Route prometheusReporterRoute = PrometheusReporterRoute .buildPrometheusReporterRoute(prometheusReporter); final CompletionStage<ServerBinding> binding = Http.get(actorSystem) .bindAndHandle(prometheusReporterRoute.flow(actorSystem, materializer), ConnectHttp.toHost(configReader.metrics().getPrometheusHostname(), configReader.metrics().getPrometheusPort()), materializer); binding.thenAccept(theBinding -> CoordinatedShutdown.get(actorSystem).addTask( CoordinatedShutdown.PhaseServiceUnbind(), "shutdown_prometheus_http_endpoint", () -> { logger.info("Gracefully shutting down Prometheus HTTP endpoint.."); // prometheus requests don't get the luxury of being processed a long time after shutdown: return theBinding.terminate(Duration.ofSeconds(1)) .handle((httpTerminated, e) -> Done.getInstance()); }) ).exceptionally(failure -> { logger.error("Kamon Prometheus HTTP endpoint could not be started: {}", failure.getMessage(), failure); logger.error("Terminating actorSystem!"); actorSystem.terminate(); return null; }); } }
private void bindHttpStatusRoute(final ActorRef healthCheckingActor, final HttpConfigReader httpConfig, final ActorMaterializer materializer) { String hostname = httpConfig.getHostname(); if (hostname.isEmpty()) { hostname = ConfigUtil.getLocalHostAddress(); log.info("No explicit hostname configured, using HTTP hostname: {}", hostname); } final CompletionStage<ServerBinding> binding = Http.get(getContext().system()) .bindAndHandle(createRoute(getContext().system(), healthCheckingActor).flow(getContext().system(), materializer), ConnectHttp.toHost(hostname, httpConfig.getPort()), materializer); binding.thenAccept(theBinding -> CoordinatedShutdown.get(getContext().getSystem()).addTask( CoordinatedShutdown.PhaseServiceUnbind(), "shutdown_health_http_endpoint", () -> { log.info("Gracefully shutting down status/health HTTP endpoint.."); return theBinding.terminate(Duration.ofSeconds(1)) .handle((httpTerminated, e) -> Done.getInstance()); }) ).exceptionally(failure -> { log.error(failure, "Something very bad happened: {}", failure.getMessage()); getContext().system().terminate(); return null; }); }
private void createHealthCheckingActorHttpBinding(final HttpConfigReader httpConfig, final ActorRef healthCheckingActor, final ActorMaterializer materializer) { String hostname = httpConfig.getHostname(); if (hostname.isEmpty()) { hostname = ConfigUtil.getLocalHostAddress(); log.info("No explicit hostname configured, using HTTP hostname: {}", hostname); } final CompletionStage<ServerBinding> binding = Http.get(getContext().system()) // .bindAndHandle( createRoute(getContext().system(), healthCheckingActor).flow(getContext().system(), materializer), ConnectHttp.toHost(hostname, httpConfig.getPort()), materializer); binding.thenAccept(theBinding -> CoordinatedShutdown.get(getContext().getSystem()).addTask( CoordinatedShutdown.PhaseServiceUnbind(), "shutdown_health_http_endpoint", () -> { log.info("Gracefully shutting down status/health HTTP endpoint.."); return theBinding.terminate(Duration.ofSeconds(1)) .handle((httpTerminated, e) -> Done.getInstance()); }) ).exceptionally(failure -> { log.error(failure, "Something very bad happened: {}", failure.getMessage()); getContext().system().terminate(); return null; }); }
createRoute(getContext().system(), healthCheckingActor).flow(getContext().system(), materializer), ConnectHttp.toHost(hostname, config.getInt(ConfigKeys.Http.PORT)), materializer);
.flow(actorSystem, materializer), ConnectHttp.toHost(hostname, httpConfig.getPort()), materializer);
bindingCompletionStage = Http.get(context().system()) .bindAndHandle( route.flow(context().system(), materializer), ConnectHttp.toHost(host, port), materializer);
createRoute(getContext().system(), healthCheckingActor).flow(getContext().system(), materializer), ConnectHttp.toHost(hostname, httpConfig.getPort()), materializer);
.bindAndHandle(createRoute(getContext().system(), healthCheckingActor).flow(getContext().system(), materializer), ConnectHttp.toHost(hostname, httpConfig.getPort()), materializer);