akka.http.scaladsl.Http Scala Examples
The following examples show how to use akka.http.scaladsl.Http.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: AbstractWebServer.scala From ohara with Apache License 2.0 | 6 votes |
package oharastream.ohara.shabondi.common import akka.Done import akka.actor.ActorSystem import akka.event.Logging import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.server.{Directives, Route} import akka.http.scaladsl.settings.ServerSettings import oharastream.ohara.common.util.Releasable import scala.concurrent._ import scala.concurrent.duration.Duration import scala.io.StdIn import scala.util.{Failure, Success} private[shabondi] abstract class AbstractWebServer extends Directives with Releasable { implicit protected val actorSystem: ActorSystem = ActorSystem(Logging.simpleName(this).replaceAll("\\$", "")) protected def routes: Route protected def postBinding(binding: ServerBinding): Unit = { val hostname = binding.localAddress.getHostName val port = binding.localAddress.getPort actorSystem.log.info(s"Server online at http://$hostname:$port/") } protected def postBindingFailure(cause: Throwable): Unit = { actorSystem.log.error(cause, s"Error starting the server ${cause.getMessage}") } protected def waitForShutdownSignal()(implicit ec: ExecutionContext): Future[Done] = { val promise = Promise[Done]() sys.addShutdownHook { promise.trySuccess(Done) } Future { blocking { if (StdIn.readLine("Press <RETURN> to stop Shabondi WebServer...\n") != null) promise.trySuccess(Done) } } promise.future } protected def postServerShutdown(): Unit = actorSystem.log.info("Shutting down the server") def start(bindInterface: String, port: Int): Unit = { start(bindInterface, port, ServerSettings(actorSystem)) } def start(bindInterface: String, port: Int, settings: ServerSettings): Unit = { implicit val executionContext: ExecutionContextExecutor = actorSystem.dispatcher val bindingFuture: Future[Http.ServerBinding] = Http().bindAndHandle( handler = routes, interface = bindInterface, port = port, settings = settings ) bindingFuture.onComplete { case Success(binding) => postBinding(binding) case Failure(cause) => postBindingFailure(cause) } Await.ready( bindingFuture.flatMap(_ => waitForShutdownSignal()), Duration.Inf ) bindingFuture .flatMap(_.unbind()) .onComplete { _ => postServerShutdown() actorSystem.terminate() } } override def close(): Unit = actorSystem.terminate() }
Example 2
Source File: ModelService.scala From reactive-machine-learning-systems with MIT License | 6 votes |
package com.reactivemachinelearning import akka.actor.ActorSystem import akka.event.{Logging, LoggingAdapter} import akka.http.scaladsl.Http import akka.http.scaladsl.marshalling.ToResponseMarshallable import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.stream.{ActorMaterializer, Materializer} //import spray.json._ import spray.json.DefaultJsonProtocol import scala.concurrent.{ExecutionContextExecutor, Future} case class Prediction(id: Long, timestamp: Long, value: Double) trait Protocols extends DefaultJsonProtocol { implicit val ipInfoFormat = jsonFormat3(Prediction.apply) } trait Service extends Protocols { implicit val system: ActorSystem implicit def executor: ExecutionContextExecutor implicit val materializer: Materializer val logger: LoggingAdapter // private def parseFeatures(features: String): Map[Long, Double] = { // features.parseJson.convertTo[Map[Long, Double]] // } def predict(features: String): Future[Prediction] = { Future(Prediction(123, 456, 0.5)) } val routes = { logRequestResult("predictive-service") { pathPrefix("ip") { (get & path(Segment)) { features => complete { predict(features).map[ToResponseMarshallable] { // case prediction: Prediction => prediction case _ => BadRequest } } } } } } } object PredictiveService extends App with Service { override implicit val system = ActorSystem() override implicit val executor = system.dispatcher override implicit val materializer = ActorMaterializer() override val logger = Logging(system, getClass) Http().bindAndHandle(routes, "0.0.0.0", 9000) }
Example 3
Source File: TestResponseFromUnsupportedApis.scala From ohara with Apache License 2.0 | 5 votes |
package oharastream.ohara.configurator import java.util.concurrent.TimeUnit import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.model.{HttpMethod, HttpMethods, HttpRequest} import akka.http.scaladsl.unmarshalling.Unmarshal import oharastream.ohara.client.configurator.ErrorApi import oharastream.ohara.common.rule.OharaTest import oharastream.ohara.common.util.{CommonUtils, Releasable} import org.junit.{After, Test} import org.scalatest.matchers.should.Matchers._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.concurrent.{Await, Future} class TestResponseFromUnsupportedApis extends OharaTest { private[this] val configurator = Configurator.builder.fake().build() private[this] implicit val actorSystem: ActorSystem = ActorSystem("Executor-TestResponseFromUnsupportedApis") private[this] val expectedMessage = oharastream.ohara.configurator.route.apiUrl private[this] def result[T](f: Future[T]): T = Await.result(f, Duration(20, TimeUnit.SECONDS)) @Test def testGet(): Unit = sendRequest(HttpMethods.GET, CommonUtils.randomString()).apiUrl.get shouldBe expectedMessage @Test def testPut(): Unit = sendRequest(HttpMethods.PUT, CommonUtils.randomString()).apiUrl.get shouldBe expectedMessage @Test def testDelete(): Unit = sendRequest(HttpMethods.DELETE, CommonUtils.randomString()).apiUrl.get shouldBe expectedMessage @Test def testPost(): Unit = sendRequest(HttpMethods.POST, CommonUtils.randomString()).apiUrl.get shouldBe expectedMessage private[this] def sendRequest(method: HttpMethod, postfix: String): ErrorApi.Error = result( Http() .singleRequest(HttpRequest(method, s"http://${configurator.hostname}:${configurator.port}/$postfix")) .flatMap { response => if (response.status.isSuccess()) Future.failed(new AssertionError()) else Unmarshal(response.entity).to[ErrorApi.Error] } ) @After def tearDown(): Unit = { Releasable.close(configurator) result(actorSystem.terminate()) } }
Example 4
Source File: Test873.scala From ohara with Apache License 2.0 | 5 votes |
package oharastream.ohara.client.kafka import java.util.concurrent.TimeUnit import akka.actor.ActorSystem import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.{Http, server} import oharastream.ohara.client.kafka.WorkerJson.{ConnectorCreationResponse, KafkaConnectorTaskId, _} import oharastream.ohara.common.rule.OharaTest import oharastream.ohara.common.setting.ConnectorKey import oharastream.ohara.common.util.CommonUtils import oharastream.ohara.kafka.connector.json.Creation import org.junit.Test import org.scalatest.matchers.should.Matchers._ import scala.concurrent.{Await, Future} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.jdk.CollectionConverters._ class Test873 extends OharaTest { private[this] def result[T](f: Future[T]): T = Await.result(f, Duration(60, TimeUnit.SECONDS)) @Test def testCreateConnector(): Unit = { val className = CommonUtils.randomString() val settings = Map( CommonUtils.randomString() -> CommonUtils.randomString() ) val tasks = Seq( KafkaConnectorTaskId( connector = CommonUtils.randomString(), task = 10 ) ) val server = toServer { path("connectors") { post { entity(as[Creation]) { req => complete( ConnectorCreationResponse( name = req.name(), config = req.configs().asScala.toMap, tasks = tasks ) ) } } } } try { val connectorKey = ConnectorKey.of(CommonUtils.randomString(5), CommonUtils.randomString(5)) val client = ConnectorAdmin(s"${server.hostname}:${server.port}") val response = result( client.connectorCreator().connectorKey(connectorKey).settings(settings).className(className).create() ) response.name shouldBe connectorKey.connectorNameOnKafka() response.tasks shouldBe tasks settings.foreach { case (k, v) => response.config(k) shouldBe v } } finally server.close() } private[this] def toServer(route: server.Route): SimpleServer = { implicit val system: ActorSystem = ActorSystem("my-system") val server = Await.result(Http().bindAndHandle(route, "localhost", 0), Duration(30, TimeUnit.SECONDS)) new SimpleServer { override def hostname: String = server.localAddress.getHostString override def port: Int = server.localAddress.getPort override def close(): Unit = { Await.result(server.unbind(), Duration(30, TimeUnit.SECONDS)) Await.result(system.terminate(), Duration(30, TimeUnit.SECONDS)) } } } }
Example 5
Source File: HttpServiceIntegrationTest.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.http import java.io.File import java.nio.file.Files import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpMethods, HttpRequest, StatusCodes, Uri} import com.daml.http.Statement.discard import com.daml.http.util.TestUtil.writeToFile import org.scalacheck.Gen import org.scalatest.{Assertion, BeforeAndAfterAll} import scala.concurrent.Future class HttpServiceIntegrationTest extends AbstractHttpServiceIntegrationTest with BeforeAndAfterAll { private val staticContent: String = "static" private val staticContentDir: File = Files.createTempDirectory("integration-test-static-content").toFile override def staticContentConfig: Option[StaticContentConfig] = Some(StaticContentConfig(prefix = staticContent, directory = staticContentDir)) override def jdbcConfig: Option[JdbcConfig] = None private val expectedDummyContent: String = Gen .listOfN(100, Gen.identifier) .map(_.mkString(" ")) .sample .getOrElse(throw new IllegalStateException(s"Cannot create dummy text content")) private val dummyFile: File = writeToFile(new File(staticContentDir, "dummy.txt"), expectedDummyContent).get require(dummyFile.exists) override protected def afterAll(): Unit = { // clean up temp directory discard { dummyFile.delete() } discard { staticContentDir.delete() } super.afterAll() } "should serve static content from configured directory" in withHttpService { (uri: Uri, _, _) => Http() .singleRequest( HttpRequest( method = HttpMethods.GET, uri = uri.withPath(Uri.Path(s"/$staticContent/${dummyFile.getName}")))) .flatMap { resp => discard { resp.status shouldBe StatusCodes.OK } val bodyF: Future[String] = getResponseDataBytes(resp, debug = false) bodyF.flatMap { body => body shouldBe expectedDummyContent } }: Future[Assertion] } "Forwarded" - { import Endpoints.Forwarded "can 'parse' sample" in { Forwarded("for=192.168.0.1;proto=http;by=192.168.0.42").proto should ===(Some("http")) } "can 'parse' quoted sample" in { Forwarded("for=192.168.0.1;proto = \"https\" ;by=192.168.0.42").proto should ===( Some("https")) } } }
Example 6
Source File: OAuthFailedSpec.scala From kanadi with MIT License | 5 votes |
package org.zalando.kanadi import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import com.typesafe.config.ConfigFactory import io.circe.Json import org.mdedetrich.webmodels.{FlowId, OAuth2Token, OAuth2TokenProvider} import org.specs2.Specification import org.specs2.concurrent.ExecutionEnv import org.specs2.execute.Skipped import org.specs2.matcher.FutureMatchers import org.specs2.specification.core.SpecStructure import org.zalando.kanadi.api.{Events, Subscriptions} import org.zalando.kanadi.models._ import scala.concurrent.Future import scala.concurrent.duration._ class OAuthFailedSpec(implicit ec: ExecutionEnv) extends Specification with FutureMatchers with Config { val config = ConfigFactory.load() implicit val system = ActorSystem() implicit val http = Http() implicit val materializer = ActorMaterializer() val failingOauth2TokenProvider = Some( OAuth2TokenProvider(() => Future.successful(OAuth2Token("Failing token"))) ) val subscriptionsClient = Subscriptions(nakadiUri, failingOauth2TokenProvider) val eventsClient = Events(nakadiUri, failingOauth2TokenProvider) override def is: SpecStructure = s2""" Call to subscriptions list should fail with invalid token $oAuthCallSubscriptions Call to publishEvents should fail with invalid token $oAuthPublishEvents """ def oAuthCallSubscriptions = Skipped("No way for current Nakadi docker image to detect \"wrong\" tokens") def oAuthPublishEvents = Skipped("No way for current Nakadi docker image to detect \"wrong\" tokens") }
Example 7
Source File: SubscriptionsSpec.scala From kanadi with MIT License | 5 votes |
package org.zalando.kanadi import java.util.UUID import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import com.typesafe.config.ConfigFactory import org.mdedetrich.webmodels.FlowId import org.specs2.Specification import org.specs2.concurrent.ExecutionEnv import org.specs2.specification.core.SpecStructure import org.specs2.specification.{AfterAll, BeforeAll} import org.zalando.kanadi.api.{Category, EventType, EventTypes, Events, Subscription, Subscriptions} import org.zalando.kanadi.models.{EventTypeName, SubscriptionId} import scala.collection.parallel.mutable import scala.concurrent.duration._ import scala.concurrent.{Await, Future} class SubscriptionsSpec(implicit ec: ExecutionEnv) extends Specification with Config with BeforeAll with AfterAll { override def is: SpecStructure = sequential ^ s2""" Create enough subscriptions to ensure that pagination is used $createEnoughSubscriptionsToUsePagination """ val config = ConfigFactory.load() implicit val system = ActorSystem() implicit val http = Http() implicit val materializer = ActorMaterializer() val eventTypeName = EventTypeName(s"Kanadi-Test-Event-${UUID.randomUUID().toString}") val OwningApplication = "KANADI" val consumerGroup: String = UUID.randomUUID().toString val subscriptionsClient = Subscriptions(nakadiUri, None) val eventsClient = Events(nakadiUri, None) val eventsTypesClient = EventTypes(nakadiUri, None) val subscriptionIds: mutable.ParSet[SubscriptionId] = mutable.ParSet.empty eventTypeName.pp s"Consumer Group: $consumerGroup".pp def createEventType = eventsTypesClient.create(EventType(eventTypeName, OwningApplication, Category.Business)) override def beforeAll = Await.result(createEventType, 10 seconds) override def afterAll = { Await.result( for { res1 <- Future.sequence(subscriptionIds.toList.map(s => subscriptionsClient.delete(s))) res2 <- eventsTypesClient.delete(eventTypeName) } yield (res1, res2), 10 seconds ) () } def createEnoughSubscriptionsToUsePagination = (name: String) => { implicit val flowId: FlowId = Utils.randomFlowId() flowId.pp(name) val createdSubscriptions = Future.sequence(for { _ <- 1 to 22 subscription = subscriptionsClient.create( Subscription(None, s"$OwningApplication-${UUID.randomUUID().toString}", Some(List(eventTypeName)))) } yield { subscription.foreach { s => subscriptionIds += s.id.get } subscription }) val retrievedSubscriptions = (for { subscriptions <- createdSubscriptions retrievedSubscription = Future.sequence(subscriptions.map { subscription => subscriptionsClient.createIfDoesntExist(subscription) }) } yield retrievedSubscription).flatMap(a => a) Await.result(createdSubscriptions, 10 seconds) mustEqual Await.result(retrievedSubscriptions, 10 seconds) } }
Example 8
Source File: JsonRpcHttpsServer.scala From mantis with Apache License 2.0 | 5 votes |
package io.iohk.ethereum.jsonrpc.server import java.io.{File, FileInputStream} import java.security.{KeyStore, SecureRandom} import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory} import akka.actor.ActorSystem import akka.http.scaladsl.model.headers.HttpOriginRange import akka.http.scaladsl.{ConnectionContext, Http} import akka.stream.ActorMaterializer import io.iohk.ethereum.jsonrpc.JsonRpcController import io.iohk.ethereum.jsonrpc.server.JsonRpcHttpsServer.HttpsSetupResult import io.iohk.ethereum.jsonrpc.server.JsonRpcServer.JsonRpcServerConfig import io.iohk.ethereum.utils.Logger import scala.concurrent.ExecutionContext.Implicits.global import scala.io.Source import scala.util.{Failure, Success, Try} class JsonRpcHttpsServer(val jsonRpcController: JsonRpcController, config: JsonRpcServerConfig, secureRandom: SecureRandom)(implicit val actorSystem: ActorSystem) extends JsonRpcServer with Logger { def run(): Unit = { implicit val materializer = ActorMaterializer() val maybeSslContext = validateCertificateFiles(config.certificateKeyStorePath, config.certificateKeyStoreType, config.certificatePasswordFile).flatMap{ case (keystorePath, keystoreType, passwordFile) => val passwordReader = Source.fromFile(passwordFile) try { val password = passwordReader.getLines().mkString obtainSSLContext(keystorePath, keystoreType, password) } finally { passwordReader.close() } } val maybeHttpsContext = maybeSslContext.map(sslContext => ConnectionContext.https(sslContext)) maybeHttpsContext match { case Right(httpsContext) => Http().setDefaultServerHttpContext(httpsContext) val bindingResultF = Http().bindAndHandle(route, config.interface, config.port, connectionContext = httpsContext) bindingResultF onComplete { case Success(serverBinding) => log.info(s"JSON RPC HTTPS server listening on ${serverBinding.localAddress}") case Failure(ex) => log.error("Cannot start JSON HTTPS RPC server", ex) } case Left(error) => log.error(s"Cannot start JSON HTTPS RPC server due to: $error") } } private def validateCertificateFiles(maybeKeystorePath: Option[String], maybeKeystoreType: Option[String], maybePasswordFile: Option[String]): HttpsSetupResult[(String, String, String)] = (maybeKeystorePath, maybeKeystoreType, maybePasswordFile) match { case (Some(keystorePath), Some(keystoreType), Some(passwordFile)) => val keystoreDirMissing = !new File(keystorePath).isFile val passwordFileMissing = !new File(passwordFile).isFile if(keystoreDirMissing && passwordFileMissing) Left("Certificate keystore path and password file configured but files are missing") else if(keystoreDirMissing) Left("Certificate keystore path configured but file is missing") else if(passwordFileMissing) Left("Certificate password file configured but file is missing") else Right((keystorePath, keystoreType, passwordFile)) case _ => Left("HTTPS requires: certificate-keystore-path, certificate-keystore-type and certificate-password-file to be configured") } override def corsAllowedOrigins: HttpOriginRange = config.corsAllowedOrigins } object JsonRpcHttpsServer { type HttpsSetupResult[T] = Either[String, T] }
Example 9
Source File: JsonRpcHttpServer.scala From mantis with Apache License 2.0 | 5 votes |
package io.iohk.ethereum.jsonrpc.server import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.HttpOriginRange import akka.stream.ActorMaterializer import io.iohk.ethereum.jsonrpc._ import io.iohk.ethereum.jsonrpc.server.JsonRpcServer.JsonRpcServerConfig import io.iohk.ethereum.utils.Logger import scala.concurrent.ExecutionContext.Implicits.global import scala.util.{Failure, Success} class JsonRpcHttpServer(val jsonRpcController: JsonRpcController, config: JsonRpcServerConfig) (implicit val actorSystem: ActorSystem) extends JsonRpcServer with Logger { def run(): Unit = { implicit val materializer = ActorMaterializer() val bindingResultF = Http(actorSystem).bindAndHandle(route, config.interface, config.port) bindingResultF onComplete { case Success(serverBinding) => log.info(s"JSON RPC HTTP server listening on ${serverBinding.localAddress}") case Failure(ex) => log.error("Cannot start JSON HTTP RPC server", ex) } } override def corsAllowedOrigins: HttpOriginRange = config.corsAllowedOrigins }
Example 10
Source File: Server.scala From opencensus-scala with Apache License 2.0 | 5 votes |
package io.opencensus.scala.examples.akka.http import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import com.typesafe.scalalogging.LazyLogging import io.opencensus.scala.akka.http.TracingDirective._ import io.opencensus.trace.AttributeValue import org.slf4j.bridge.SLF4JBridgeHandler import scala.util.{Failure, Success} object Server extends App with LazyLogging { // Forward java.util.Logging to slf4j SLF4JBridgeHandler.removeHandlersForRootLogger() SLF4JBridgeHandler.install() implicit val system: ActorSystem = ActorSystem() import system.dispatcher val routes: Route = traceRequest { span => complete { val attrValue = AttributeValue.stringAttributeValue("test") span.putAttribute("my-attribute", attrValue) "Hello opencensus" } } logger.info("Binding...") Http().bindAndHandle(routes, "0.0.0.0", 8080).onComplete { case Success(bound) => logger.info(s"Bound to ${bound.localAddress}") case Failure(e) => logger.error("Failed to bind", e) } }
Example 11
Source File: Client.scala From opencensus-scala with Apache License 2.0 | 5 votes |
package io.opencensus.scala.examples.akka.http import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpRequest import akka.stream.scaladsl.{Sink, Source} import io.opencensus.scala.akka.http.TracingClient import org.slf4j.bridge.SLF4JBridgeHandler import scala.concurrent.duration._ import scala.concurrent.{Await, Future} import scala.util.{Failure, Success} object Client extends App { // Forward java.util.Logging to slf4j SLF4JBridgeHandler.removeHandlersForRootLogger() SLF4JBridgeHandler.install() implicit val system: ActorSystem = ActorSystem() import system.dispatcher def await[T](f: Future[T]) = Await.result(f, 3.seconds) // Request level client val pipeling = Http().singleRequest(_: HttpRequest) val r1 = await { TracingClient .traceRequest(pipeling)(HttpRequest(uri = "http://localhost:8080")) .flatMap(_.entity.toStrict(1.second)) .map(_.data.utf8String) } println(r1) // Host level client val pool = Http().cachedHostConnectionPool[Unit]("localhost", 8080) val hostFlow = TracingClient.traceRequestForPool(pool) val r2 = await { Source .single(HttpRequest(uri = "/")) .map((_, ())) .via(hostFlow) .map(_._1) .flatMapConcat { case Success(response) => response.entity.dataBytes case Failure(e) => throw e } .map(_.utf8String) .runWith(Sink.head) } println(r2) // Connection level client val connection = Http().outgoingConnection("localhost", 8080) val connectionFlow = TracingClient.traceRequestForConnection(connection) val r3 = await { Source .single(HttpRequest(uri = "/")) .via(connectionFlow) .flatMapConcat(_.entity.dataBytes) .map(_.utf8String) .runWith(Sink.head) } println(r3) }
Example 12
Source File: Server.scala From incubator-s2graph with Apache License 2.0 | 5 votes |
package org.apache.s2graph.http import java.time.Instant import scala.language.postfixOps import scala.concurrent.{Await, ExecutionContext, Future} import scala.concurrent.duration.Duration import scala.util.{Failure, Success} import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse, StatusCodes} import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import com.typesafe.config.ConfigFactory import org.apache.s2graph.core.S2Graph import org.slf4j.LoggerFactory object Server extends App with S2GraphTraversalRoute with S2GraphAdminRoute with S2GraphMutateRoute with S2GraphQLRoute { implicit val system: ActorSystem = ActorSystem("S2GraphHttpServer") implicit val materializer: ActorMaterializer = ActorMaterializer() implicit val executionContext: ExecutionContext = system.dispatcher val config = ConfigFactory.load() override val s2graph = new S2Graph(config) override val logger = LoggerFactory.getLogger(this.getClass) val port = sys.props.get("http.port").fold(8000)(_.toInt) val interface = sys.props.get("http.interface").fold("0.0.0.0")(identity) val startAt = System.currentTimeMillis() def uptime = System.currentTimeMillis() - startAt def serverHealth = s"""{ "port": ${port}, "interface": "${interface}", "started_at": ${Instant.ofEpochMilli(startAt)}, "uptime": "${uptime} millis" """ def health = HttpResponse(status = StatusCodes.OK, entity = HttpEntity(ContentTypes.`application/json`, serverHealth)) // Allows you to determine routes to expose according to external settings. lazy val routes: Route = concat( pathPrefix("graphs")(traversalRoute), pathPrefix("mutate")(mutateRoute), pathPrefix("admin")(adminRoute), pathPrefix("graphql")(graphqlRoute), get(complete(health)) ) val binding: Future[Http.ServerBinding] = Http().bindAndHandle(routes, interface, port) binding.onComplete { case Success(bound) => logger.info(s"Server online at http://${bound.localAddress.getHostString}:${bound.localAddress.getPort}/") case Failure(e) => logger.error(s"Server could not start!", e) } scala.sys.addShutdownHook { () => s2graph.shutdown() system.terminate() logger.info("System terminated") } Await.result(system.whenTerminated, Duration.Inf) }
Example 13
Source File: WsConnection.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.api.ws.connection import java.util.concurrent.ConcurrentLinkedQueue import akka.Done import akka.actor.{ActorRef, ActorSystem, Status} import akka.http.scaladsl.Http import akka.http.scaladsl.model.ws.{BinaryMessage, Message, TextMessage, WebSocketRequest} import akka.stream.scaladsl.{Flow, Sink, Source} import akka.stream.{CompletionStrategy, Materializer, OverflowStrategy} import com.wavesplatform.dex.api.ws.protocol.{WsClientMessage, WsMessage, WsPingOrPong, WsServerMessage} import com.wavesplatform.dex.domain.utils.ScorexLogging import play.api.libs.json.Json import scala.collection.JavaConverters._ import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.{Failure, Success, Try} class WsConnection(uri: String, keepAlive: Boolean = true)(implicit system: ActorSystem, materializer: Materializer) extends ScorexLogging { log.info(s"""Connecting to Matcher WS API: | URI = $uri | Keep alive = $keepAlive""".stripMargin) import materializer.executionContext private val wsHandlerRef = system.actorOf(TestWsHandlerActor props keepAlive) protected def stringifyClientMessage(cm: WsClientMessage): TextMessage.Strict = WsMessage.toStrictTextMessage(cm)(WsClientMessage.wsClientMessageWrites) // From test to server private val source: Source[TextMessage.Strict, ActorRef] = { val completionMatcher: PartialFunction[Any, CompletionStrategy] = { case akka.actor.Status.Success(_) => CompletionStrategy.draining } val failureMatcher: PartialFunction[Any, Throwable] = { case Status.Failure(cause) => cause } Source .actorRef[WsClientMessage](completionMatcher, failureMatcher, 10, OverflowStrategy.fail) .map(stringifyClientMessage) .mapMaterializedValue { source => wsHandlerRef.tell(TestWsHandlerActor.AssignSourceRef, source) source } } private val messagesBuffer: ConcurrentLinkedQueue[WsServerMessage] = new ConcurrentLinkedQueue[WsServerMessage]() // From server to test private val sink: Sink[Message, Future[Done]] = Sink.foreach { case tm: TextMessage => for { strictText <- tm.toStrict(1.second).map(_.getStrictText) clientMessage <- { log.trace(s"Got $strictText") Try { Json.parse(strictText).as[WsServerMessage] } match { case Failure(exception) => Future.failed(exception) case Success(x) => { messagesBuffer.add(x) if (keepAlive) x match { case value: WsPingOrPong => wsHandlerRef ! value case _ => } Future.successful(x) } } } } yield clientMessage case bm: BinaryMessage => bm.dataStream.runWith(Sink.ignore) Future.failed { new IllegalArgumentException("Binary messages are not supported") } } private val flow: Flow[Message, TextMessage.Strict, Future[Done]] = Flow.fromSinkAndSourceCoupled(sink, source).watchTermination() { case (_, f) => f.onComplete { case Success(_) => log.info(s"WebSocket connection to $uri successfully closed") case Failure(e) => log.error(s"WebSocket connection to $uri closed with an error", e) }(materializer.executionContext) f } val (connectionResponse, closed) = Http().singleWebSocketRequest(WebSocketRequest(uri), flow) val connectionOpenedTs: Long = System.currentTimeMillis val connectionClosedTs: Future[Long] = closed.map(_ => System.currentTimeMillis) val connectionLifetime: Future[FiniteDuration] = connectionClosedTs.map(cc => FiniteDuration(cc - connectionOpenedTs, MILLISECONDS)) def messages: List[WsServerMessage] = messagesBuffer.iterator().asScala.toList def clearMessages(): Unit = messagesBuffer.clear() def send(message: WsClientMessage): Unit = wsHandlerRef ! TestWsHandlerActor.SendToServer(message) def close(): Unit = if (!isClosed) wsHandlerRef ! TestWsHandlerActor.CloseConnection def isClosed: Boolean = closed.isCompleted }
Example 14
Source File: WsConnection.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.load.ws import akka.Done import akka.actor.{ActorRef, ActorSystem, Status} import akka.http.scaladsl.Http import akka.http.scaladsl.model.ws.{BinaryMessage, Message, TextMessage, WebSocketRequest} import akka.stream.scaladsl.{Flow, Sink, Source} import akka.stream.{CompletionStrategy, Materializer, OverflowStrategy} import com.wavesplatform.dex.api.ws.connection.TestWsHandlerActor import com.wavesplatform.dex.api.ws.protocol.{WsClientMessage, WsMessage, WsServerMessage} import com.wavesplatform.dex.domain.utils.ScorexLogging import play.api.libs.json.Json import scala.concurrent.Future import scala.concurrent.duration.DurationInt import scala.util.{Failure, Success, Try} class WsConnection(uri: String, receive: WsServerMessage => Option[WsClientMessage])(implicit system: ActorSystem) extends ScorexLogging { import system.dispatcher private implicit val materializer = Materializer(system) private val wsHandlerRef = system.actorOf(TestWsHandlerActor.props(keepAlive = true)) log.info(s"Connecting to Matcher WS API: $uri") protected def stringifyClientMessage(cm: WsClientMessage): TextMessage.Strict = WsMessage.toStrictTextMessage(cm)(WsClientMessage.wsClientMessageWrites) // To server private val source: Source[TextMessage.Strict, ActorRef] = { val completionMatcher: PartialFunction[Any, CompletionStrategy] = { case akka.actor.Status.Success(_) => CompletionStrategy.draining } val failureMatcher: PartialFunction[Any, Throwable] = { case Status.Failure(cause) => cause } Source .actorRef[WsClientMessage](completionMatcher, failureMatcher, 10, OverflowStrategy.fail) .map(stringifyClientMessage) .mapMaterializedValue { source => wsHandlerRef.tell(TestWsHandlerActor.AssignSourceRef, source) source } } // To client private val sink: Sink[Message, Future[Done]] = Sink.foreach { case tm: TextMessage => // TODO move to tests for { strictText <- tm.toStrict(1.second).map(_.getStrictText) clientMessage <- { log.trace(s"Got $strictText") Try { Json.parse(strictText).as[WsServerMessage] } match { case Failure(exception) => Future.failed(exception) case Success(x) => Future.successful { receive(x).foreach(wsHandlerRef ! _) } } } } yield clientMessage case bm: BinaryMessage => bm.dataStream.runWith(Sink.ignore) Future.failed { new IllegalArgumentException("Binary messages are not supported") } } private val flow: Flow[Message, TextMessage.Strict, Future[Done]] = Flow.fromSinkAndSourceCoupled(sink, source).watchTermination() { case (_, f) => f.onComplete { case Success(_) => log.info(s"WebSocket connection to $uri successfully closed") case Failure(e) => log.error(s"WebSocket connection to $uri closed with an error", e) }(materializer.executionContext) f } val (connectionResponse, closed) = Http().singleWebSocketRequest(WebSocketRequest(uri), flow) def send(message: WsClientMessage): Unit = wsHandlerRef ! TestWsHandlerActor.SendToServer(message) def isClosed: Boolean = closed.isCompleted def close(): Future[Done] = { if (!isClosed) wsHandlerRef ! TestWsHandlerActor.CloseConnection closed } }
Example 15
Source File: Boot.scala From akka-http-rest with MIT License | 5 votes |
package me.archdev.restapi import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import me.archdev.restapi.core.auth.{ AuthService, JdbcAuthDataStorage } import me.archdev.restapi.core.profiles.{ JdbcUserProfileStorage, UserProfileService } import me.archdev.restapi.http.HttpRoute import me.archdev.restapi.utils.Config import me.archdev.restapi.utils.db.{ DatabaseConnector, DatabaseMigrationManager } import scala.concurrent.ExecutionContext object Boot extends App { def startApplication() = { implicit val actorSystem = ActorSystem() implicit val executor: ExecutionContext = actorSystem.dispatcher implicit val materializer: ActorMaterializer = ActorMaterializer() val config = Config.load() new DatabaseMigrationManager( config.database.jdbcUrl, config.database.username, config.database.password ).migrateDatabaseSchema() val databaseConnector = new DatabaseConnector( config.database.jdbcUrl, config.database.username, config.database.password ) val userProfileStorage = new JdbcUserProfileStorage(databaseConnector) val authDataStorage = new JdbcAuthDataStorage(databaseConnector) val usersService = new UserProfileService(userProfileStorage) val authService = new AuthService(authDataStorage, config.secretKey) val httpRoute = new HttpRoute(usersService, authService, config.secretKey) Http().bindAndHandle(httpRoute.route, config.http.host, config.http.port) } startApplication() }
Example 16
Source File: LogCollector.scala From pulse with Apache License 2.0 | 5 votes |
package io.phdata.pulse.logcollector import java.io.FileInputStream import java.util.Properties import java.util.concurrent.TimeUnit import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.{ ActorMaterializer, Materializer } import com.typesafe.scalalogging.LazyLogging import io.phdata.pulse.common.SolrService import io.phdata.pulse.solr.SolrProvider import org.apache.kudu.client.KuduClient.KuduClientBuilder import scala.concurrent.duration.Duration import scala.concurrent.{ Await, Future } import scala.util.{ Failure, Success } def main(args: Array[String]): Unit = System.getProperty("java.security.auth.login.config") match { case null => { logger.info( "java.security.auth.login.config is not set, continuing without kerberos authentication") } case _ => { KerberosContext.scheduleKerberosLogin(0, 9, TimeUnit.HOURS) } start(args) } private def start(args: Array[String]): Unit = { val cliParser = new LogCollectorCliParser(args) val solrService = SolrProvider.create(cliParser.zkHosts().split(",").toList) val solrStream = new SolrCloudStream(solrService) val kuduClient = cliParser.kuduMasters.toOption.map(masters => KerberosContext.runPrivileged(new KuduClientBuilder(masters).build())) val kuduService = kuduClient.map(client => KerberosContext.runPrivileged(new KuduService(client))) val routes = new LogCollectorRoutes(solrStream, kuduService) cliParser.mode() match { case "kafka" => { kafka(solrService, cliParser.kafkaProps(), cliParser.topic()) } case _ => { http(cliParser.port(), routes) } } } // Starts Http Service def http(port: Int, routes: LogCollectorRoutes): Future[Unit] = { implicit val actorSystem: ActorSystem = ActorSystem() implicit val ec = actorSystem.dispatchers.lookup("akka.actor.http-dispatcher") implicit val materializer: Materializer = ActorMaterializer.create(actorSystem) val httpServerFuture = Http().bindAndHandle(routes.routes, "0.0.0.0", port)(materializer) map { binding => logger.info(s"Log Collector interface bound to: ${binding.localAddress}") } httpServerFuture.onComplete { case Success(v) => () case Failure(ex) => { logger.error("HTTP server failed, exiting. ", ex) System.exit(1) } } Await.ready( httpServerFuture, Duration.Inf ) } // Starts Kafka Consumer def kafka(solrService: SolrService, kafkaProps: String, topic: String): Unit = { val solrCloudStream = new SolrCloudStream(solrService) val kafkaConsumer = new PulseKafkaConsumer(solrCloudStream) val kafkaConsumerProps = new Properties() kafkaConsumerProps.load(new FileInputStream(kafkaProps)) kafkaConsumer.read(kafkaConsumerProps, topic) } }
Example 17
Source File: App.scala From avoin-voitto with MIT License | 5 votes |
package liigavoitto import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives import akka.stream.ActorMaterializer import liigavoitto.api.{ ApiRoutes, BaseRoutes } import liigavoitto.util.Logging import scala.util.Properties object App extends Directives with ApiRoutes with Logging { implicit lazy val system = ActorSystem("liiga-voitto") lazy val port = Properties.envOrElse("APP_PORT", "45258").toInt def main(args: Array[String]) { implicit val executionContext = system.dispatcher implicit val fm = ActorMaterializer() Http().bindAndHandle(routes, "0.0.0.0", port) log.info(s"Server online at http://0.0.0.0:$port/") } val routes = BaseRoutes.baseRoutes ~ apiRoutes ~ localRoute }
Example 18
Source File: ScoresApiSupport.scala From avoin-voitto with MIT License | 5 votes |
package liigavoitto.scores import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpMethods._ import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.stream.ActorMaterializer import liigavoitto.util.Logging import org.joda.time.format.DateTimeFormat import scala.concurrent.Await import scala.concurrent.duration._ import scala.util.{ Failure, Properties, Success, Try } trait ScoresApiSupport extends Logging { implicit val system: ActorSystem implicit val ec = system.dispatcher implicit val fm = ActorMaterializer() val oneHundredMegabytes = 100000000 val apiUrl = Properties.envOrElse("SCORES_API_URL", "http://scores.api.yle.fi/v0/") val scoresAuth = Map[String, String]( "app_id" -> Properties.envOrElse("SCORES_API_APP_ID", ""), "app_key" -> Properties.envOrElse("SCORES_API_APP_KEY", "") ) val dateFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss") val timeout = 15.seconds protected def get(url: String) = { Try { val request = HttpRequest(GET, url) log.info("REQUEST: " + request) Http().singleRequest(request).map(r => getStr(r)) } match { case Success(s) => s case Failure(e) => log.warn(s"Failed to get $url: " + e.getMessage) e.printStackTrace() throw new RuntimeException("Failure: " + e) } } protected def getStr(r: HttpResponse) = { Try { val entity = Await.result(r.entity.withSizeLimit(oneHundredMegabytes).toStrict(timeout), timeout) entity.data.decodeString("UTF-8") } match { case Success(s) => s case Failure(e) => throw new RuntimeException(s"Scores api failure: " + e.getMessage) } } }
Example 19
Source File: ReliableHttpProxyFactory.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc.akkahttp.proxy import akka.NotUsed import akka.actor._ import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse} import akka.stream.Materializer import akka.stream.scaladsl.{Flow, Sink, Source} import org.slf4j.LoggerFactory import rhttpc.client.protocol.{Correlated, Request} import rhttpc.client.proxy._ import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} import scala.util.control.NonFatal import scala.util.{Failure, Success} object ReliableHttpProxyFactory { private lazy val logger = LoggerFactory.getLogger(getClass) def send(successRecognizer: SuccessHttpResponseRecognizer, batchSize: Int, parallelConsumers: Int) (request: Request[HttpRequest]) (implicit actorSystem: ActorSystem, materialize: Materializer): Future[HttpResponse] = { import actorSystem.dispatcher send(prepareHttpFlow(batchSize * parallelConsumers), successRecognizer)(request.correlated) } private def prepareHttpFlow(parallelism: Int) (implicit actorSystem: ActorSystem, materialize: Materializer): Flow[(HttpRequest, String), HttpResponse, NotUsed] = { import actorSystem.dispatcher Http().superPool[String]().mapAsync(parallelism) { case (tryResponse, id) => tryResponse match { case Success(response) => response.toStrict(1 minute) case Failure(ex) => Future.failed(ex) } } } private def send(httpFlow: Flow[(HttpRequest, String), HttpResponse, Any], successRecognizer: SuccessHttpResponseRecognizer) (corr: Correlated[HttpRequest]) (implicit ec: ExecutionContext, materialize: Materializer): Future[HttpResponse] = { import collection.JavaConverters._ logger.debug( s"""Sending request for ${corr.correlationId} to ${corr.msg.getUri()}. Headers: |${corr.msg.getHeaders().asScala.toSeq.map(h => " " + h.name() + ": " + h.value()).mkString("\n")} |Body: |${corr.msg.entity.asInstanceOf[HttpEntity.Strict].data.utf8String}""".stripMargin ) val logResp = logResponse(corr) _ val responseFuture = Source.single((corr.msg, corr.correlationId)).via(httpFlow).runWith(Sink.head) responseFuture.onComplete { case Failure(ex) => logger.error(s"Got failure for ${corr.correlationId} to ${corr.msg.getUri()}", ex) case Success(_) => } for { response <- responseFuture transformedToFailureIfNeed <- { if (successRecognizer.isSuccess(response)) { logResp(response, "success response") Future.successful(response) } else { logResp(response, "response recognized as non-success") Future.failed(NonSuccessResponse) } } } yield transformedToFailureIfNeed } private def logResponse(corr: Correlated[HttpRequest]) (response: HttpResponse, additionalInfo: String): Unit = { import collection.JavaConverters._ logger.debug( s"""Got $additionalInfo for ${corr.correlationId} to ${corr.msg.getUri()}. Status: ${response.status.value}. Headers: |${response.getHeaders().asScala.toSeq.map(h => " " + h.name() + ": " + h.value()).mkString("\n")} |Body: |${response.entity.asInstanceOf[HttpEntity.Strict].data.utf8String}""".stripMargin ) } }
Example 20
Source File: AmqpSubscriberPerfSpec.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc.transport.amqp import akka.Done import akka.actor.{Actor, ActorSystem, Props} import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.pattern._ import akka.stream.ActorMaterializer import akka.testkit.{TestKit, TestProbe} import dispatch.url import org.scalatest.{BeforeAndAfterAll, FlatSpecLike, Ignore} import rhttpc.transport.{Deserializer, InboundQueueData, OutboundQueueData, Serializer} import scala.concurrent.duration._ import scala.concurrent.{Await, Future} import scala.util.{Random, Try} @Ignore class AmqpSubscriberPerfSpec extends TestKit(ActorSystem("AmqpSubscriberPerfSpec")) with FlatSpecLike with BeforeAndAfterAll { import system.dispatcher implicit val materializer = ActorMaterializer() implicit def serializer[Msg] = new Serializer[Msg] { override def serialize(obj: Msg): String = obj.toString } implicit def deserializer[Msg] = new Deserializer[Msg] { override def deserialize(value: String): Try[Msg] = Try(value.asInstanceOf[Msg]) } val queueName = "request" val outboundQueueData = OutboundQueueData(queueName, autoDelete = true, durability = false) val inboundQueueData = InboundQueueData(queueName, batchSize = 10, parallelConsumers = 10, autoDelete = true, durability = false) val count = 100 private val interface = "localhost" private val port = 8081 def handle(request: HttpRequest) = { val delay = 5 + Random.nextInt(10) after(delay.seconds, system.scheduler)(Future.successful(HttpResponse())) } it should "have a good throughput" in { val bound = Await.result( Http().bindAndHandleAsync( handle, interface, port ), 5.seconds ) val http = dispatch.Http() // .configure(_.setMaxConnections(count) // .setExecutorService(Executors.newFixedThreadPool(count))) val connection = Await.result(AmqpConnectionFactory.connect(system), 5 seconds) val transport = AmqpTransport( connection = connection ) val publisher = transport.publisher[String](outboundQueueData) val probe = TestProbe() val actor = system.actorOf(Props(new Actor { override def receive: Receive = { case str: String => http(url(s"http://$interface:$port") OK identity).map(_ => Done).pipeTo(self)(sender()) case Done => probe.ref ! Done sender() ! Done } })) val subscriber = transport.subscriber[String](inboundQueueData, actor) subscriber.start() try { measureMeanThroughput(count) { (1 to count).foreach { _ => publisher.publish("x") } probe.receiveWhile(10 minutes, messages = count) { case a => a } } } finally { Await.result(subscriber.stop(), 5.seconds) connection.close(5 * 1000) Await.result(bound.unbind(), 5.seconds) } } def measureMeanThroughput(count: Int)(consume: => Unit) = { val before = System.currentTimeMillis() consume val msgsPerSecond = count / ((System.currentTimeMillis() - before).toDouble / 1000) println(s"Throughput was: $msgsPerSecond msgs/sec") } override protected def afterAll(): Unit = { shutdown() } }
Example 21
Source File: SampleApp.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc.sample import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.server._ import akka.pattern._ import akka.stream.ActorMaterializer import akka.util.Timeout import rhttpc.akkahttp.ReliableHttpClientFactory import rhttpc.akkapersistence.{RecoverAllActors, RecoverableActorsManager, SendMsgToChild} import rhttpc.client.subscription.ReplyFuture import scala.concurrent.Await import scala.concurrent.duration._ import scala.language.reflectiveCalls object SampleApp extends App with Directives { implicit val system = ActorSystem("rhttpc-sample") implicit val materializer = ActorMaterializer() import system.dispatcher val rhttpc = Await.result(ReliableHttpClientFactory().withOwnAmqpConnection.inOutWithSubscriptions(), 10 seconds) val client = new DelayedEchoClient { override def requestResponse(msg: String): ReplyFuture = { rhttpc.send(HttpRequest().withUri("http://sampleecho:8082").withMethod(HttpMethods.POST).withEntity(msg)) } } val manager = system.actorOf(RecoverableActorsManager.props( FooBarActor.persistenceCategory, id => FooBarActor.props(id, rhttpc.subscriptionManager, client) ), "foobar") Await.result((manager ? RecoverAllActors)(Timeout(20 seconds)), 15 seconds) rhttpc.start() val route = path("healthcheck") { get { complete("OK") } } ~ path(Segment) { id => (post & entity(as[String])) { msg => complete { implicit val sendMsgTimeout = Timeout(5 seconds) (manager ? SendMsgToChild(id, SendMsg(msg))).map(_ => "OK") } } ~ get { complete { implicit val currentStateTimeout = Timeout(5 seconds) (manager ? SendMsgToChild(id, CurrentState)).mapTo[FooBarState].map(_.toString) } } } Http().bindAndHandle(route, interface = "0.0.0.0", port = 8081).map { binding => Runtime.getRuntime.addShutdownHook(new Thread { override def run(): Unit = { Await.result(rhttpc.stop(), 10 seconds) } }) } }
Example 22
Source File: EchoApp.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc.echo import akka.actor.ActorSystem import akka.agent.Agent import akka.http.scaladsl.Http import akka.http.scaladsl.server._ import akka.pattern._ import akka.stream.ActorMaterializer import scala.concurrent.Future import scala.concurrent.duration._ object EchoApp extends App with Directives { implicit val system = ActorSystem("rhttpc-echo") implicit val materializer = ActorMaterializer() import system.dispatcher val retryAgent = Agent(Map.empty[String, Int]) val route = (post & entity(as[String])) { case request@FailNTimesThanReplyWithMessage(failsCount, eventualMessage) => complete { retryAgent.alter { currectRetryMap => val current = currectRetryMap.getOrElse(request, 0) val next = current + 1 if (next > failsCount) { currectRetryMap - request } else { currectRetryMap + (request -> next) } }.flatMap { retryMapAfterChange => retryMapAfterChange.get(request) match { case Some(retry) => Future.failed(new Exception(s"Failed $retry time")) case None => Future.successful(eventualMessage) } } } case msg => complete { system.log.debug(s"Got: $msg") after(5 seconds, system.scheduler) { system.log.debug(s"Reply with: $msg") Future.successful(msg) } } } Http().bindAndHandle(route, interface = "0.0.0.0", port = 8082) } object FailNTimesThanReplyWithMessage { private val Regex = "fail-(\\d*)-times-than-reply-with-(.*)".r("failsCount", "eventualMessage") def unapply(str: String): Option[(Int, String)] = str match { case Regex(failsCount, eventualMessage) => Some(failsCount.toInt, eventualMessage) case other => None } }
Example 23
Source File: HTTPResponseStream.scala From akka_streams_tutorial with MIT License | 5 votes |
package akkahttp import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.common.{EntityStreamingSupport, JsonEntityStreamingSupport} import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives.{complete, get, logRequestResult, path, _} import akka.http.scaladsl.server.Route import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.ThrottleMode import akka.stream.scaladsl.{Flow, Sink, Source} import com.typesafe.config.ConfigFactory import spray.json.DefaultJsonProtocol import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.{Failure, Success} object HTTPResponseStream extends App with DefaultJsonProtocol with SprayJsonSupport { implicit val system = ActorSystem("HTTPResponseStream") implicit val executionContext = system.dispatcher //JSON Protocol and streaming support final case class ExamplePerson(name: String) implicit def examplePersonFormat = jsonFormat1(ExamplePerson.apply) implicit val jsonStreamingSupport: JsonEntityStreamingSupport = EntityStreamingSupport.json() val (address, port) = ("127.0.0.1", 8080) server(address, port) client(address, port) def client(address: String, port: Int): Unit = { val requestParallelism = ConfigFactory.load.getInt("akka.http.host-connection-pool.max-connections") val requests: Source[HttpRequest, NotUsed] = Source .fromIterator(() => Range(0, requestParallelism).map(i => HttpRequest(uri = Uri(s"http://$address:$port/download/$i"))).iterator ) // Run singleRequest and completely consume response elements def runRequestDownload(req: HttpRequest) = Http() .singleRequest(req) .flatMap { response => val unmarshalled: Future[Source[ExamplePerson, NotUsed]] = Unmarshal(response).to[Source[ExamplePerson, NotUsed]] val source: Source[ExamplePerson, Future[NotUsed]] = Source.futureSource(unmarshalled) source.via(processorFlow).runWith(printSink) } requests .mapAsync(requestParallelism)(runRequestDownload) .runWith(Sink.ignore) } val printSink = Sink.foreach[ExamplePerson] { each: ExamplePerson => println(s"Client processed element: $each") } val processorFlow: Flow[ExamplePerson, ExamplePerson, NotUsed] = Flow[ExamplePerson].map { each: ExamplePerson => { //println(s"Process: $each") each } } def server(address: String, port: Int): Unit = { def routes: Route = logRequestResult("httpecho") { path("download" / Segment) { id: String => get { println(s"Server received request with id: $id, stream response...") extractRequest { r: HttpRequest => val finishedWriting = r.discardEntityBytes().future onComplete(finishedWriting) { done => //Limit response by appending eg .take(5) val responseStream: Stream[ExamplePerson] = Stream.continually(ExamplePerson(s"request:$id")) complete(Source(responseStream).throttle(1, 1.second, 1, ThrottleMode.shaping)) } } } } } val bindingFuture = Http().bindAndHandle(routes, address, port) bindingFuture.onComplete { case Success(b) => println("Server started, listening on: " + b.localAddress) case Failure(e) => println(s"Server could not bind to: $address:$port. Exception message: ${e.getMessage}") system.terminate() } } }
Example 24
Source File: SampleRoutes.scala From akka_streams_tutorial with MIT License | 5 votes |
package akkahttp import java.io.File import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import org.slf4j.{Logger, LoggerFactory} import scala.concurrent.Await import scala.concurrent.duration._ import scala.sys.process.Process import scala.util.{Failure, Success} object SampleRoutes extends App { val logger: Logger = LoggerFactory.getLogger(this.getClass) implicit val system = ActorSystem("SampleRoutes") implicit val executionContext = system.dispatcher def getFromBrowsableDir: Route = { val dirToBrowse = File.separator + "tmp" // pathPrefix allows loading dirs and files recursively pathPrefix("entries") { getFromBrowseableDirectory(dirToBrowse) } } def parseFormData: Route = path("post") { formFields('color, 'age.as[Int]) { (color, age) => complete(s"The color is '$color' and the age is $age") } } def routes: Route = { getFromBrowsableDir ~ parseFormData } val bindingFuture = Http().bindAndHandle(routes, "127.0.0.1", 8000) bindingFuture.onComplete { case Success(b) => println("Server started, listening on: " + b.localAddress) case Failure(e) => println(s"Server could not bind to... Exception message: ${e.getMessage}") system.terminate() } def browserClient() = { val os = System.getProperty("os.name").toLowerCase if (os == "mac os x") Process("open ./src/main/resources/SampleRoutes.html").! } browserClient() sys.addShutdownHook { println("About to shutdown...") val fut = bindingFuture.map(serverBinding => serverBinding.terminate(hardDeadline = 3.seconds)) println("Waiting for connections to terminate...") val onceAllConnectionsTerminated = Await.result(fut, 10.seconds) println("Connections terminated") onceAllConnectionsTerminated.flatMap { _ => system.terminate() } } }
Example 25
Source File: WebSocketClient.scala From akka_streams_tutorial with MIT License | 5 votes |
package sample.stream_actor import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.Http import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.ws._ import akka.stream.scaladsl.{Flow, GraphDSL, Keep, Sink, Source} import akka.stream.{FlowShape, SourceShape} import sample.stream_actor.WindTurbineSimulator._ import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success} object WebSocketClient { def apply(id: String, endpoint: String, windTurbineSimulator: ActorRef) (implicit system: ActorSystem, executionContext: ExecutionContext) = { new WebSocketClient(id, endpoint, windTurbineSimulator)(system, executionContext) } } class WebSocketClient(id: String, endpoint: String, windTurbineSimulator: ActorRef) (implicit system: ActorSystem, executionContext: ExecutionContext) { val webSocketFlow: Flow[Message, Message, Future[WebSocketUpgradeResponse]] = { val websocketUri = s"$endpoint/measurements/$id" Http().webSocketClientFlow(WebSocketRequest(websocketUri)) } val outgoing = GraphDSL.create() { implicit builder => val data = WindTurbineData(id) val flow = builder.add { Source.tick(1.second, 100.millis,()) //valve for the WindTurbineData frequency .map(_ => TextMessage(data.getNext)) } SourceShape(flow.out) } val incoming = GraphDSL.create() { implicit builder => val flow = builder.add { Flow[Message] .collect { case TextMessage.Strict(text) => Future.successful(text) case TextMessage.Streamed(textStream) => textStream.runFold("")(_ + _) .flatMap(Future.successful) } .mapAsync(1)(identity) .map(each => println(s"Client received msg: $each")) } FlowShape(flow.in, flow.out) } val (upgradeResponse, closed) = Source.fromGraph(outgoing) .viaMat(webSocketFlow)(Keep.right) // keep the materialized Future[WebSocketUpgradeResponse] .via(incoming) .toMat(Sink.ignore)(Keep.both) // also keep the Future[Done] .run() val connected = upgradeResponse.map { upgrade => upgrade.response.status match { case StatusCodes.SwitchingProtocols => windTurbineSimulator ! Upgraded case statusCode => windTurbineSimulator ! FailedUpgrade(statusCode) } } connected.onComplete { case Success(_) => windTurbineSimulator ! Connected case Failure(ex) => windTurbineSimulator ! ConnectionFailure(ex) } closed.map { _ => windTurbineSimulator ! Terminated } closed.onComplete { case Success(_) => windTurbineSimulator ! Connected case Failure(ex) => windTurbineSimulator ! ConnectionFailure(ex) } }
Example 26
Source File: WebsocketServer.scala From akka_streams_tutorial with MIT License | 5 votes |
package alpakka.env import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.ws._ import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.directives.WebSocketDirectives import akka.stream.scaladsl.{Flow, Sink, Source} import scala.concurrent.Await import scala.concurrent.duration._ import scala.language.postfixOps import scala.util.{Failure, Success} object WebsocketServer extends App with WebSocketDirectives { implicit val system = ActorSystem("WebsocketServer") implicit val executionContext = system.dispatcher val (address, port) = ("127.0.0.1", 6002) server(address, port) def server(address: String, port: Int) = { def echoFlow: Flow[Message, Message, Any] = Flow[Message].mapConcat { case tm: TextMessage => println(s"Server received: $tm") TextMessage(Source.single("Echo: ") ++ tm.textStream) :: Nil case bm: BinaryMessage => // ignore binary messages but drain content to avoid the stream being clogged bm.dataStream.runWith(Sink.ignore) Nil } val websocketRoute: Route = path("echo") { handleWebSocketMessages(echoFlow) } val bindingFuture = Http().bindAndHandle(websocketRoute, address, port) bindingFuture.onComplete { case Success(b) => println("Server started, listening on: " + b.localAddress) case Failure(e) => println(s"Server could not bind to $address:$port. Exception message: ${e.getMessage}") system.terminate() } sys.addShutdownHook { println("About to shutdown...") val fut = bindingFuture.map(serverBinding => serverBinding.terminate(hardDeadline = 3.seconds)) println("Waiting for connections to terminate...") val onceAllConnectionsTerminated = Await.result(fut, 10.seconds) println("Connections terminated") onceAllConnectionsTerminated.flatMap { _ => system.terminate() } } } }
Example 27
Source File: DemoApp.scala From sbt-reactive-app with Apache License 2.0 | 5 votes |
package foo import akka.actor.{ Actor, ActorLogging, ActorSystem, Props } import akka.cluster.ClusterEvent.ClusterDomainEvent import akka.cluster.{ Cluster, ClusterEvent } import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.management.AkkaManagement import akka.management.cluster.bootstrap.ClusterBootstrap import akka.stream.ActorMaterializer object DemoApp extends App { implicit val system = ActorSystem("Appka") import system.log implicit val mat = ActorMaterializer() val cluster = Cluster(system) log.info(s"Started [$system], cluster.selfAddress = ${cluster.selfAddress}") log.info("something2") //#start-akka-management AkkaManagement(system).start() //#start-akka-management ClusterBootstrap(system).start() cluster.subscribe( system.actorOf(Props[ClusterWatcher]), ClusterEvent.InitialStateAsEvents, classOf[ClusterDomainEvent]) // add real app routes here val routes = path("hello") { get { complete( HttpEntity( ContentTypes.`text/html(UTF-8)`, "<h1>Hello</h1>")) } } Http().bindAndHandle(routes, "0.0.0.0", 8080) Cluster(system).registerOnMemberUp({ log.info("Cluster member is up!") }) } class ClusterWatcher extends Actor with ActorLogging { val cluster = Cluster(context.system) override def receive = { case msg ⇒ log.info(s"Cluster ${cluster.selfAddress} >>> " + msg) } }
Example 28
Source File: Main.scala From akka-api-gateway-example with MIT License | 5 votes |
package jp.co.dzl.example.akka.api import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.{ ActorMaterializer, Materializer } import jp.co.dzl.example.akka.api.di.{ ServiceModule, HandlerModule, ConfigModule, AkkaModule } import jp.co.dzl.example.akka.api.handler.RootHandler import scaldi.{ Injector, Injectable } import scala.concurrent.ExecutionContextExecutor trait MainService extends Injectable { implicit val module: Injector = new AkkaModule :: new ConfigModule :: new HandlerModule :: new ServiceModule implicit val system: ActorSystem = inject[ActorSystem] implicit val executor: ExecutionContextExecutor = system.dispatcher implicit val materializer: Materializer = ActorMaterializer() val host = inject[String](identified by "http.listen.host") val port = inject[Int](identified by "http.listen.port") val handler = inject[RootHandler] } object Main extends App with MainService { Http().bindAndHandle(handler.routes, host, port) }
Example 29
Source File: MainServiceSpec.scala From akka-api-gateway-example with MIT License | 5 votes |
package jp.co.dzl.example.akka.api import akka.http.scaladsl.Http import com.typesafe.config.ConfigFactory import org.scalatest.concurrent.ScalaFutures import org.scalatest.{ BeforeAndAfterAll, Matchers, FlatSpec } import scala.concurrent.Await import scala.concurrent.duration.Duration class MainServiceSpec extends FlatSpec with Matchers with BeforeAndAfterAll with ScalaFutures with MainService { override protected def afterAll: Unit = { Await.result(system.terminate(), Duration.Inf) } it should "inject configuration of http" in { val config = ConfigFactory.load() host shouldEqual config.getString("http.listen.host") port shouldEqual config.getInt("http.listen.port") } it should "bind and handle" in { val http = Http().bindAndHandle(handler.routes, host, port) http.futureValue.localAddress.getPort shouldEqual port http.futureValue.unbind() } }
Example 30
Source File: ClickhouseHostHealth.scala From clickhouse-scala-client with GNU Lesser General Public License v3.0 | 5 votes |
package com.crobox.clickhouse.balancing.discovery.health import akka.NotUsed import akka.actor.{ActorSystem, Cancellable} import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.http.scaladsl.unmarshalling.Unmarshaller import akka.stream.Materializer import akka.stream.scaladsl.{Flow, Source} import com.crobox.clickhouse.internal.ClickhouseResponseParser import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success, Try} object ClickhouseHostHealth extends ClickhouseResponseParser { sealed trait ClickhouseHostStatus { val host: Uri val code: String } case class Alive(host: Uri) extends ClickhouseHostStatus { override val code: String = "ok" } case class Dead(host: Uri, reason: Throwable) extends ClickhouseHostStatus { override val code: String = "nok" } def healthFlow(host: Uri)( implicit system: ActorSystem, materializer: Materializer, executionContext: ExecutionContext ): Source[ClickhouseHostStatus, Cancellable] = { val healthCheckInterval: FiniteDuration = system.settings.config .getDuration("connection.health-check.interval") .getSeconds.seconds val healthCheckTimeout: FiniteDuration = system.settings.config .getDuration("connection.health-check.timeout") .getSeconds.seconds val healthCachedPool = Http(system).cachedHostConnectionPool[Int]( host.authority.host.address(), host.effectivePort, settings = ConnectionPoolSettings(system) .withMaxConnections(1) .withMinConnections(1) .withMaxOpenRequests(2) .withMaxRetries(3) .withUpdatedConnectionSettings( _.withIdleTimeout(healthCheckTimeout + healthCheckInterval).withConnectingTimeout(healthCheckTimeout) ) ) Source .tick(0.milliseconds, healthCheckInterval, 0) .map(tick => { (HttpRequest(method = HttpMethods.GET, uri = host), tick) }) .via(healthCachedPool) .via(parsingFlow(host)) } private[health] def parsingFlow[T]( host: Uri )(implicit ec: ExecutionContext, mat: Materializer): Flow[(Try[HttpResponse], T), ClickhouseHostStatus, NotUsed] = Flow[(Try[HttpResponse], T)].mapAsync(1) { case (Success(response @ akka.http.scaladsl.model.HttpResponse(StatusCodes.OK, _, _, _)), _) => Unmarshaller.stringUnmarshaller(decodeResponse(response).entity) .map(splitResponse) .map( stringResponse => if (stringResponse.equals(Seq("Ok."))) { Alive(host) } else { Dead(host, new IllegalArgumentException(s"Got wrong result $stringResponse")) } ) case (Success(response), _) => Future.successful(Dead(host, new IllegalArgumentException(s"Got response with status code ${response.status}"))) case (Failure(ex), _) => Future.successful(Dead(host, ex)) } }
Example 31
Source File: ClusterConnectionFlow.scala From clickhouse-scala-client with GNU Lesser General Public License v3.0 | 5 votes |
package com.crobox.clickhouse.balancing.discovery.cluster import akka.actor.{ActorSystem, Cancellable} import akka.http.scaladsl.Http import akka.http.scaladsl.model.Uri import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.stream.Materializer import akka.stream.scaladsl.Source import com.crobox.clickhouse.balancing.discovery.ConnectionManagerActor.Connections import com.crobox.clickhouse.internal.QuerySettings.ReadQueries import com.crobox.clickhouse.internal.{ClickhouseHostBuilder, ClickhouseQueryBuilder, ClickhouseResponseParser, QuerySettings} import com.typesafe.scalalogging.LazyLogging import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} private[clickhouse] object ClusterConnectionFlow extends ClickhouseQueryBuilder with ClickhouseResponseParser with LazyLogging { def clusterConnectionsFlow( targetHost: => Future[Uri], scanningInterval: FiniteDuration, cluster: String )(implicit system: ActorSystem, materializer: Materializer, ec: ExecutionContext): Source[Connections, Cancellable] = { val http = Http(system) val settings = ConnectionPoolSettings(system) .withMaxConnections(1) .withMinConnections(1) .withMaxOpenRequests(2) .withMaxRetries(3) .withUpdatedConnectionSettings( _.withIdleTimeout(scanningInterval.plus(1.second)) ) Source .tick(0.millis, scanningInterval, {}) .mapAsync(1)(_ => targetHost) .mapAsync(1)(host => { val query = s"SELECT host_address FROM system.clusters WHERE cluster='$cluster'" val request = toRequest(host, query, None, QuerySettings(readOnly = ReadQueries, idempotent = Some(true)), None)( system.settings.config ) processClickhouseResponse(http.singleRequest(request, settings = settings), query, host, None) .map(splitResponse) .map(_.toSet.filter(_.nonEmpty)) .map(result => { if (result.isEmpty) { throw new IllegalArgumentException( s"Could not determine clickhouse cluster hosts for cluster $cluster and host $host. " + s"This could indicate that you are trying to use the cluster balancer to connect to a non cluster based clickhouse server. " + s"Please use the `SingleHostQueryBalancer` in that case." ) } Connections(result.map(ClickhouseHostBuilder.toHost(_, Some(8123)))) }) }) } }
Example 32
Source File: TestHttpProxy.scala From akka-stream-sqs with Apache License 2.0 | 5 votes |
package me.snov.akka.sqs import akka.actor.{ActorSystem, Terminated} import akka.http.scaladsl.Http import akka.http.scaladsl.server.{RequestContext, Route} import akka.stream.ActorMaterializer import akka.stream.scaladsl.{Sink, Source} import scala.concurrent.{Await, Future} import scala.concurrent.duration._ class TestHttpProxy( interface: String = "localhost", port: Int, remoteHost: String = "localhost", remotePort: Int = 9324 ) { implicit var system: ActorSystem = createActorSystem() private def createActorSystem() = ActorSystem("test-http-server") def start(): Unit = { implicit val materializer: ActorMaterializer = ActorMaterializer() implicit val executionContext = system.dispatcher val proxy = Route { context: RequestContext => context.log.debug("Opening connection to %s:%d".format(remoteHost, remotePort)) Source.single(context.request) .via(Http(system).outgoingConnection(remoteHost, remotePort)) .runWith(Sink.head) .flatMap(context.complete(_)) } Http().bindAndHandle(handler = proxy, interface = interface, port = port) } def stop(): Unit = { Await.ready(system.terminate(), 1.second) } def asyncStartAfter(d: FiniteDuration) = { system = createActorSystem() system.scheduler.scheduleOnce(d, new Runnable { override def run(): Unit = start() })(system.dispatcher) } }
Example 33
Source File: RokkuS3Proxy.scala From rokku with Apache License 2.0 | 5 votes |
package com.ing.wbaa.rokku.proxy import akka.Done import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import com.ing.wbaa.rokku.proxy.api.{ AdminService, HealthService, PostRequestActions, ProxyServiceWithListAllBuckets } import com.ing.wbaa.rokku.proxy.config.HttpSettings import com.typesafe.scalalogging.LazyLogging import scala.concurrent.{ ExecutionContext, Future } import scala.util.{ Failure, Success } trait RokkuS3Proxy extends LazyLogging with ProxyServiceWithListAllBuckets with PostRequestActions with HealthService with AdminService { protected[this] implicit def system: ActorSystem implicit val materializer: ActorMaterializer = ActorMaterializer()(system) protected[this] def httpSettings: HttpSettings protected[this] implicit val executionContext: ExecutionContext = system.dispatcher // The routes we serve. final val allRoutes = adminRoute ~ healthRoute ~ proxyServiceRoute // Details about the server binding. lazy val startup: Future[Http.ServerBinding] = Http(system).bindAndHandle(allRoutes, httpSettings.httpBind, httpSettings.httpPort) .andThen { case Success(binding) => logger.info(s"Proxy service started listening: ${binding.localAddress}") case Failure(reason) => logger.error("Proxy service failed to start.", reason) } def shutdown(): Future[Done] = { startup.flatMap(_.unbind) .andThen { case Success(_) => logger.info("Proxy service stopped.") case Failure(reason) => logger.error("Proxy service failed to stop.", reason) } } }
Example 34
Source File: RequestHandlerS3.scala From rokku with Apache License 2.0 | 5 votes |
package com.ing.wbaa.rokku.proxy.handler import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.model.headers.RawHeader import com.ing.wbaa.rokku.proxy.config.StorageS3Settings import com.ing.wbaa.rokku.proxy.data.{ RequestId, S3Request, User } import com.ing.wbaa.rokku.proxy.handler.exception.RokkuThrottlingException import com.ing.wbaa.rokku.proxy.handler.radosgw.RadosGatewayHandler import com.ing.wbaa.rokku.proxy.provider.aws.S3Client import com.ing.wbaa.rokku.proxy.queue.UserRequestQueue import scala.concurrent.{ ExecutionContext, Future } import scala.util.Success trait RequestHandlerS3 extends RadosGatewayHandler with S3Client with UserRequestQueue { private val logger = new LoggerHandlerWithId protected[this] implicit def system: ActorSystem protected[this] implicit def executionContext: ExecutionContext protected[this] def storageS3Settings: StorageS3Settings protected[this] def filterResponse(request: HttpRequest, userSTS: User, s3request: S3Request, response: HttpResponse)(implicit id: RequestId): HttpResponse protected[this] def fireRequestToS3(request: HttpRequest, user: User)(implicit id: RequestId): Future[HttpResponse] = { if (storageS3Settings.isRequestUserQueueEnabled) { if (addIfAllowedUserToRequestQueue(user)) { fireRequestToS3(request).andThen { case _ => decrement(user) } } else { implicit val returnStatusCode: StatusCodes.ServerError = StatusCodes.ServiceUnavailable logger.warn("user {} is sending too many requests", user.userName.value) Future.failed(new RokkuThrottlingException("Throttling")) } } else { fireRequestToS3(request) } } }
Example 35
Source File: AuthenticationProviderSTS.scala From rokku with Apache License 2.0 | 5 votes |
package com.ing.wbaa.rokku.proxy.provider import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.RawHeader import akka.http.scaladsl.model.{ HttpRequest, StatusCodes, Uri } import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.Materializer import com.ing.wbaa.rokku.proxy.config.StsSettings import com.ing.wbaa.rokku.proxy.data.{ AwsRequestCredential, JsonProtocols, RequestId, User, UserRawJson } import com.ing.wbaa.rokku.proxy.handler.LoggerHandlerWithId import com.ing.wbaa.rokku.proxy.util.JwtToken import scala.concurrent.{ ExecutionContext, Future } trait AuthenticationProviderSTS extends JsonProtocols with JwtToken { private val logger = new LoggerHandlerWithId import AuthenticationProviderSTS.STSException import spray.json._ protected[this] implicit def system: ActorSystem protected[this] implicit def executionContext: ExecutionContext protected[this] implicit def materializer: Materializer protected[this] def stsSettings: StsSettings protected[this] def areCredentialsActive(awsRequestCredential: AwsRequestCredential)(implicit id: RequestId): Future[Option[User]] = { val QueryParameters = Map("accessKey" -> awsRequestCredential.accessKey.value) ++ awsRequestCredential.sessionToken.map(s => "sessionToken" -> s.value) val uri = stsSettings.stsBaseUri .withPath(Uri.Path("/isCredentialActive")) .withQuery(Uri.Query(QueryParameters)) Http() .singleRequest( HttpRequest(uri = uri) .addHeader(RawHeader("Authorization", createInternalToken)) .addHeader(RawHeader("x-rokku-request-id", id.value)) ) .flatMap { response => response.status match { case StatusCodes.OK => Unmarshal(response.entity).to[String].map { jsonString => Some(User(jsonString.parseJson.convertTo[UserRawJson])) } case StatusCodes.Forbidden => logger.error(s"User not authenticated " + s"with accessKey (${awsRequestCredential.accessKey.value}) " + s"and sessionToken (${awsRequestCredential.sessionToken})") Future.successful(None) case c => val msg = s"Received unexpected StatusCode ($c) for " + s"accessKey (${awsRequestCredential.accessKey.value}) " + s"and sessionToken (${awsRequestCredential.sessionToken})" logger.error(msg) Future.failed(STSException(msg)) } } } } object AuthenticationProviderSTS { final case class STSException(private val message: String, private val cause: Throwable = None.orNull) extends Exception(message, cause) }
Example 36
Source File: DemoApp.scala From constructr-consul with Apache License 2.0 | 5 votes |
package com.tecsisa.constructr.coordination package demo import akka.actor.{ ActorRef, ActorSystem, Address } import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives import akka.pattern.ask import akka.stream.ActorMaterializer import akka.util.Timeout import com.typesafe.config.ConfigFactory import scala.concurrent.duration.{ Duration, MILLISECONDS } object DemoApp { val conf = ConfigFactory.load() val hostname = conf.getString("demo.hostname") val httpPort = conf.getInt("demo.port") def main(args: Array[String]): Unit = { // Create an Akka system implicit val system = ActorSystem("ConstructR-Consul") import system.dispatcher implicit val mat = ActorMaterializer() // Create an actor that handles cluster domain events val cluster = system.actorOf(SimpleClusterListener.props, SimpleClusterListener.Name) Http().bindAndHandle(route(cluster), hostname, httpPort) } private def route(cluster: ActorRef) = { import Directives._ implicit val timeout = Timeout( Duration( conf.getDuration("demo.cluster-view-timeout").toMillis, MILLISECONDS ) ) path("member-nodes") { // List cluster nodes get { onSuccess( (cluster ? SimpleClusterListener.GetMemberNodes).mapTo[Set[Address]] )(addresses => complete(addresses.mkString("\n"))) } } } }
Example 37
Source File: HttpApp.scala From darwin with Apache License 2.0 | 5 votes |
package it.agilelab.darwin.server.rest import java.util.concurrent.Executor import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.RouteConcatenation import akka.stream.ActorMaterializer import com.typesafe.config.Config import it.agilelab.darwin.common.Logging import scala.concurrent.duration.Duration import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor} class HttpApp(config: Config, services: Service*) (implicit system: ActorSystem, materializer: ActorMaterializer) extends Logging { def run(): Unit = { val interface = config.getString("interface") val port = config.getInt("port") val route = RouteConcatenation.concat(services.map(_.route): _*) log.info("Starting http server on {}:{}", interface, port) val eventuallyBinding = Http().bindAndHandle(route, interface, port) val binding = Await.result(eventuallyBinding, Duration.Inf) log.info("Started http server on {}:{}", interface, port) val shutdownThread = new Thread(new Runnable { override def run(): Unit = { implicit val ec: ExecutionContext = newSameThreadExecutor log.info("Received shutdown hook") val termination = for { _ <- binding.unbind() terminated <- system.terminate() } yield terminated Await.ready(termination, Duration.Inf) log.info("Shutdown") } }) shutdownThread.setName("shutdown") Runtime.getRuntime.addShutdownHook(shutdownThread) log.info("registered shutdown hook") } private def newSameThreadExecutor: ExecutionContextExecutor = ExecutionContext.fromExecutor(new Executor { override def execute(command: Runnable): Unit = command.run() }) } object HttpApp { def apply(config:Config, services: Service*)(implicit system: ActorSystem, materializer: ActorMaterializer): HttpApp = new HttpApp(config, services: _*) }
Example 38
Source File: AkkaServerProvider.scala From play-ws with Apache License 2.0 | 5 votes |
package play import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Route import org.specs2.concurrent.ExecutionEnv import org.specs2.specification.BeforeAfterAll import scala.concurrent.duration._ import scala.concurrent.Await import scala.concurrent.Future import akka.stream.Materializer trait AkkaServerProvider extends BeforeAfterAll { def executionEnv: ExecutionEnv var testServerPort: Int = _ val defaultTimeout: FiniteDuration = 5.seconds // Create Akka system for thread and streaming management implicit val system = ActorSystem() implicit val materializer = Materializer.matFromSystem lazy val futureServer: Future[Http.ServerBinding] = { // Using 0 (zero) means that a random free port will be used. // So our tests can run in parallel and won't mess with each other. Http().bindAndHandle(routes, "localhost", 0) } override def beforeAll(): Unit = { val portFuture = futureServer.map(_.localAddress.getPort)(executionEnv.executionContext) portFuture.foreach(port => testServerPort = port)(executionEnv.executionContext) Await.ready(portFuture, defaultTimeout) } override def afterAll(): Unit = { futureServer.foreach(_.unbind())(executionEnv.executionContext) val terminate = system.terminate() Await.ready(terminate, defaultTimeout) } }
Example 39
Source File: Client.scala From akka-http-oauth2-client with Apache License 2.0 | 5 votes |
package com.github.dakatsuka.akka.http.oauth2.client import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.model.{ HttpRequest, HttpResponse, Uri } import akka.stream.Materializer import akka.stream.scaladsl.{ Flow, Sink } import com.github.dakatsuka.akka.http.oauth2.client.Error.UnauthorizedException import com.github.dakatsuka.akka.http.oauth2.client.strategy.Strategy import scala.concurrent.{ ExecutionContext, Future } class Client(config: ConfigLike, connection: Option[Flow[HttpRequest, HttpResponse, _]] = None)(implicit system: ActorSystem) extends ClientLike { def getAuthorizeUrl[A <: GrantType](grant: A, params: Map[String, String] = Map.empty)(implicit s: Strategy[A]): Option[Uri] = s.getAuthorizeUrl(config, params) def getAccessToken[A <: GrantType]( grant: A, params: Map[String, String] = Map.empty )(implicit s: Strategy[A], ec: ExecutionContext, mat: Materializer): Future[Either[Throwable, AccessToken]] = { val source = s.getAccessTokenSource(config, params) source .via(connection.getOrElse(defaultConnection)) .mapAsync(1)(handleError) .mapAsync(1)(AccessToken.apply) .runWith(Sink.head) .map(Right.apply) .recover { case ex => Left(ex) } } def getConnectionWithAccessToken(accessToken: AccessToken): Flow[HttpRequest, HttpResponse, _] = Flow[HttpRequest] .map(_.addCredentials(OAuth2BearerToken(accessToken.accessToken))) .via(connection.getOrElse(defaultConnection)) private def defaultConnection: Flow[HttpRequest, HttpResponse, _] = config.site.getScheme match { case "http" => Http().outgoingConnection(config.getHost, config.getPort) case "https" => Http().outgoingConnectionHttps(config.getHost, config.getPort) } private def handleError(response: HttpResponse)(implicit ec: ExecutionContext, mat: Materializer): Future[HttpResponse] = { if (response.status.isFailure()) UnauthorizedException.fromHttpResponse(response).flatMap(Future.failed(_)) else Future.successful(response) } } object Client { def apply(config: ConfigLike)(implicit system: ActorSystem): Client = new Client(config) def apply(config: ConfigLike, connection: Flow[HttpRequest, HttpResponse, _])(implicit system: ActorSystem): Client = new Client(config, Some(connection)) }
Example 40
Source File: JustinDB.scala From JustinDB with Apache License 2.0 | 5 votes |
package justin.db import akka.actor.ActorSystem import akka.cluster.Cluster import akka.cluster.http.management.ClusterHttpManagement import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.stream.{ActorMaterializer, Materializer} import buildinfo.BuildInfo import com.typesafe.scalalogging.StrictLogging import justin.db.actors.{StorageNodeActor, StorageNodeActorRef} import justin.db.client.ActorRefStorageNodeClient import justin.db.cluster.datacenter.Datacenter import justin.db.consistenthashing.{NodeId, Ring} import justin.db.replica.N import justin.db.storage.PluggableStorageProtocol import justin.db.storage.provider.StorageProvider import justin.httpapi.{BuildInfoRouter, HealthCheckRouter, HttpRouter} import scala.concurrent.duration._ import scala.concurrent.{Await, ExecutionContext, Promise} import scala.language.reflectiveCalls // $COVERAGE-OFF$ final class JustinDB object JustinDB extends StrictLogging { private[this] def validConfiguration(justinDBConfig: JustinDBConfig): Unit = { require(justinDBConfig.replication.N > 0, "replication N factor can't be smaller or equal 0") require(justinDBConfig.ring.`members-count` > 0, "members-counter can't be smaller or equal 0") require(justinDBConfig.ring.partitions > 0, "ring partitions can't be smaller or equal 0") require(justinDBConfig.ring.partitions >= justinDBConfig.ring.`members-count`, "number of ring partitions can't be smaller than number of members-count") require(justinDBConfig.replication.N <= justinDBConfig.ring.`members-count`, "replication N factor can't be bigger than defined members-count number") } private[this] def initStorage(justinConfig: JustinDBConfig) = { val provider = StorageProvider.apply(justinConfig.storage.provider) logger.info("Storage provider: " + provider.name) provider.init } def init(justinConfig: JustinDBConfig)(implicit actorSystem: ActorSystem): JustinDB = { validConfiguration(justinConfig) val processOrchestrator = Promise[JustinDB] implicit val executor: ExecutionContext = actorSystem.dispatcher implicit val materializer: Materializer = ActorMaterializer() val storage: PluggableStorageProtocol = initStorage(justinConfig) val cluster = Cluster(actorSystem) cluster.registerOnMemberUp { // STORAGE ACTOR val storageNodeActorRef = StorageNodeActorRef { val nodeId = NodeId(justinConfig.`kubernetes-hostname`.split("-").last.toInt) val ring = Ring(justinConfig.ring.`members-count`, justinConfig.ring.partitions) val n = N(justinConfig.replication.N) val datacenter = Datacenter(justinConfig.dc.`self-data-center`) actorSystem.actorOf( props = StorageNodeActor.props(nodeId, datacenter, storage, ring, n), name = StorageNodeActor.name(nodeId, datacenter) ) } // AKKA-MANAGEMENT ClusterHttpManagement(cluster).start().map { _ => logger.info("Cluster HTTP-Management is ready!") }.recover { case ex => processOrchestrator.failure(ex) } // HTTP API val routes = logRequestResult(actorSystem.name) { new HttpRouter(new ActorRefStorageNodeClient(storageNodeActorRef)).routes ~ new HealthCheckRouter().routes ~ new BuildInfoRouter().routes(BuildInfo.toJson) } Http() .bindAndHandle(routes, justinConfig.http.interface, justinConfig.http.port) .map { binding => logger.info(s"HTTP server started at ${binding.localAddress}"); processOrchestrator.trySuccess(new JustinDB) } .recover { case ex => logger.error("Could not start HTTP server", ex); processOrchestrator.failure(ex) } } Await.result(processOrchestrator.future, 2.minutes) } } // $COVERAGE-ON$
Example 41
Source File: Main.scala From reactive-kafka-microservice-template with Apache License 2.0 | 5 votes |
package com.omearac import akka.actor.{ActorSystem, Props} import akka.event.Logging import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import com.omearac.consumers.ConsumerStreamManager.InitializeConsumerStream import com.omearac.consumers.{ConsumerStreamManager, DataConsumer, EventConsumer} import com.omearac.http.HttpService import com.omearac.producers.ProducerStreamManager.InitializeProducerStream import com.omearac.producers.{DataProducer, EventProducer, ProducerStreamManager} import com.omearac.settings.Settings import com.omearac.shared.AkkaStreams import com.omearac.shared.KafkaMessages.{ExampleAppEvent, KafkaMessage} import scala.concurrent.{Await, Future} import scala.concurrent.duration._ import scala.io.StdIn object Main extends App with HttpService with AkkaStreams { implicit val system = ActorSystem("akka-reactive-kafka-app") val log = Logging(system, this.getClass.getName) //Start the akka-http server and listen for http requests val akkaHttpServer = startAkkaHTTPServer() //Create the Producer Stream Manager and Consumer Stream Manager val producerStreamManager = system.actorOf(Props(new ProducerStreamManager), "producerStreamManager") val consumerStreamManager = system.actorOf(Props(new ConsumerStreamManager), "consumerStreamManager") //Create actor to publish event messages to kafka stream. val eventProducer = system.actorOf(EventProducer.props, "eventProducer") producerStreamManager ! InitializeProducerStream(eventProducer, ExampleAppEvent) //Create actor to consume event messages from kafka stream. val eventConsumer = system.actorOf(EventConsumer.props, "eventConsumer") consumerStreamManager ! InitializeConsumerStream(eventConsumer, ExampleAppEvent) //Create actor to publish data messages to kafka stream. val dataProducer = system.actorOf(DataProducer.props, "dataProducer") producerStreamManager ! InitializeProducerStream(dataProducer, KafkaMessage) //Create actor to consume data messages from kafka stream. val dataConsumer = system.actorOf(DataConsumer.props, "dataConsumer") consumerStreamManager ! InitializeConsumerStream(dataConsumer, KafkaMessage) //Shutdown shutdownApplication() private def startAkkaHTTPServer(): Future[ServerBinding] = { val settings = Settings(system).Http val host = settings.host println(s"Specify the TCP port do you want to host the HTTP server at (e.g. 8001, 8080..etc)? \nHit Return when finished:") val portNum = StdIn.readInt() println(s"Waiting for http requests at http://$host:$portNum/") Http().bindAndHandle(routes, host, portNum) } private def shutdownApplication(): Unit = { scala.sys.addShutdownHook({ println("Terminating the Application...") akkaHttpServer.flatMap(_.unbind()) system.terminate() Await.result(system.whenTerminated, 30 seconds) println("Application Terminated") }) } }
Example 42
Source File: WskActorSystem.scala From openwhisk with Apache License 2.0 | 5 votes |
package common import scala.concurrent.Await import scala.concurrent.ExecutionContext import scala.concurrent.duration.DurationInt import akka.actor.ActorSystem import akka.http.scaladsl.Http import org.scalatest.BeforeAndAfterAll import org.scalatest.Suite trait WskActorSystem extends BeforeAndAfterAll { self: Suite => implicit val actorSystem: ActorSystem = ActorSystem() implicit def executionContext: ExecutionContext = actorSystem.dispatcher override def afterAll() = { try { Await.result(Http().shutdownAllConnectionPools(), 30.seconds) } finally { actorSystem.terminate() Await.result(actorSystem.whenTerminated, 30.seconds) } super.afterAll() } }
Example 43
Source File: PoolingRestClient.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.http import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.marshalling._ import akka.http.scaladsl.model._ import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.http.scaladsl.unmarshalling._ import akka.stream.{ActorMaterializer, OverflowStrategy, QueueOfferResult} import akka.stream.scaladsl.{Flow, _} import spray.json._ import scala.concurrent.{ExecutionContext, Future, Promise} import scala.concurrent.duration._ import scala.util.{Failure, Success, Try} def requestJson[T: RootJsonReader](futureRequest: Future[HttpRequest]): Future[Either[StatusCode, T]] = request(futureRequest).flatMap { response => if (response.status.isSuccess) { Unmarshal(response.entity.withoutSizeLimit).to[T].map(Right.apply) } else { Unmarshal(response.entity).to[String].flatMap { body => val statusCode = response.status val reason = if (body.nonEmpty) s"${statusCode.reason} (details: $body)" else statusCode.reason val customStatusCode = StatusCodes .custom(intValue = statusCode.intValue, reason = reason, defaultMessage = statusCode.defaultMessage) // This is important, as it drains the entity stream. // Otherwise the connection stays open and the pool dries up. response.discardEntityBytes().future.map(_ => Left(customStatusCode)) } } } def shutdown(): Future[Unit] = Future.successful(materializer.shutdown()) } object PoolingRestClient { def mkRequest(method: HttpMethod, uri: Uri, body: Future[MessageEntity] = Future.successful(HttpEntity.Empty), headers: List[HttpHeader] = List.empty)(implicit ec: ExecutionContext): Future[HttpRequest] = { body.map { b => HttpRequest(method, uri, headers, b) } } def mkJsonRequest(method: HttpMethod, uri: Uri, body: JsValue, headers: List[HttpHeader] = List.empty)( implicit ec: ExecutionContext): Future[HttpRequest] = { val b = Marshal(body).to[MessageEntity] mkRequest(method, uri, b, headers) } }
Example 44
Source File: Main.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.monitoring.metrics import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import kamon.Kamon import scala.concurrent.duration.DurationInt import scala.concurrent.{Await, ExecutionContextExecutor, Future} object Main { def main(args: Array[String]): Unit = { Kamon.init() implicit val system: ActorSystem = ActorSystem("events-actor-system") implicit val materializer: ActorMaterializer = ActorMaterializer() val binding = OpenWhiskEvents.start(system.settings.config) addShutdownHook(binding) } private def addShutdownHook(binding: Future[Http.ServerBinding])(implicit actorSystem: ActorSystem, materializer: ActorMaterializer): Unit = { implicit val ec: ExecutionContextExecutor = actorSystem.dispatcher sys.addShutdownHook { Await.result(binding.map(_.unbind()), 30.seconds) Await.result(actorSystem.whenTerminated, 30.seconds) } } }
Example 45
Source File: OpenWhiskEvents.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.monitoring.metrics import akka.actor.{ActorSystem, CoordinatedShutdown} import akka.event.slf4j.SLF4JLogging import akka.http.scaladsl.Http import akka.kafka.ConsumerSettings import akka.stream.ActorMaterializer import com.typesafe.config.Config import kamon.Kamon import kamon.prometheus.PrometheusReporter import org.apache.kafka.common.serialization.StringDeserializer import pureconfig._ import pureconfig.generic.auto._ import scala.concurrent.duration.FiniteDuration import scala.concurrent.{ExecutionContext, Future} object OpenWhiskEvents extends SLF4JLogging { case class MetricConfig(port: Int, enableKamon: Boolean, ignoredNamespaces: Set[String], renameTags: Map[String, String], retry: RetryConfig) case class RetryConfig(minBackoff: FiniteDuration, maxBackoff: FiniteDuration, randomFactor: Double, maxRestarts: Int) def start(config: Config)(implicit system: ActorSystem, materializer: ActorMaterializer): Future[Http.ServerBinding] = { implicit val ec: ExecutionContext = system.dispatcher val prometheusReporter = new PrometheusReporter() Kamon.registerModule("prometheus", prometheusReporter) Kamon.init(config) val metricConfig = loadConfigOrThrow[MetricConfig](config, "whisk.user-events") val prometheusRecorder = PrometheusRecorder(prometheusReporter, metricConfig) val recorders = if (metricConfig.enableKamon) Seq(prometheusRecorder, KamonRecorder) else Seq(prometheusRecorder) val eventConsumer = EventConsumer(eventConsumerSettings(defaultConsumerConfig(config)), recorders, metricConfig) CoordinatedShutdown(system).addTask(CoordinatedShutdown.PhaseBeforeServiceUnbind, "shutdownConsumer") { () => eventConsumer.shutdown() } val port = metricConfig.port val api = new PrometheusEventsApi(eventConsumer, prometheusRecorder) val httpBinding = Http().bindAndHandle(api.routes, "0.0.0.0", port) httpBinding.foreach(_ => log.info(s"Started the http server on http://localhost:$port"))(system.dispatcher) httpBinding } def eventConsumerSettings(config: Config): ConsumerSettings[String, String] = ConsumerSettings(config, new StringDeserializer, new StringDeserializer) def defaultConsumerConfig(globalConfig: Config): Config = globalConfig.getConfig("akka.kafka.consumer") }
Example 46
Source File: OpenWhiskEventsTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.monitoring.metrics import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, StatusCodes} import akka.http.scaladsl.unmarshalling.Unmarshal import com.typesafe.config.ConfigFactory import io.prometheus.client.CollectorRegistry import kamon.Kamon import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import scala.concurrent.duration._ import scala.util.Try @RunWith(classOf[JUnitRunner]) class OpenWhiskEventsTests extends KafkaSpecBase { behavior of "Server" it should "start working http server" in { val httpPort = freePort() val globalConfig = system.settings.config val config = ConfigFactory.parseString(s""" | akka.kafka.consumer.kafka-clients { | bootstrap.servers = "localhost:$kafkaPort" | } | kamon { | metric { | tick-interval = 50 ms | optimistic-tick-alignment = no | } | } | whisk { | user-events { | port = $httpPort | rename-tags { | namespace = "ow_namespace" | } | } | } """.stripMargin).withFallback(globalConfig) CollectorRegistry.defaultRegistry.clear() val binding = OpenWhiskEvents.start(config).futureValue val res = get("localhost", httpPort, "/ping") res shouldBe Some(StatusCodes.OK, "pong") //Check if metrics using Kamon API gets included in consolidated Prometheus Kamon.counter("fooTest").withoutTags().increment(42) sleep(1.second) val metricRes = get("localhost", httpPort, "/metrics") metricRes.get._2 should include("fooTest") binding.unbind().futureValue } def get(host: String, port: Int, path: String = "/") = { val response = Try { Http() .singleRequest(HttpRequest(uri = s"http://$host:$port$path")) .futureValue }.toOption response.map { res => (res.status, Unmarshal(res).to[String].futureValue) } } }
Example 47
Source File: ServiceApp.scala From BusFloatingData with Apache License 2.0 | 5 votes |
package de.nierbeck.floating.data.server import akka.actor.{ActorRef, ActorSystem, Props} import akka.event.Logging import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpMethods._ import akka.http.scaladsl.model.ws.UpgradeToWebSocket import akka.http.scaladsl.model.{HttpRequest, HttpResponse, Uri} import akka.stream.ActorMaterializer import de.nierbeck.floating.data.server.actors.websocket.{FLINK, RouterActor, SPARK, TiledVehiclesFromKafkaActor} import scala.concurrent.Await import scala.concurrent.duration.Duration import scala.util.{Failure, Success} object ServiceApp extends RestService { import ServiceConfig._ import system.dispatcher implicit val system = ActorSystem("service-api-http") implicit val mat = ActorMaterializer() override val logger = Logging(system, getClass.getName) override val session = CassandraConnector.connect() def main(args: Array[String]): Unit = { val router: ActorRef = system.actorOf(Props[RouterActor], "router") val sparkKafkaConsumer: ActorRef = system.actorOf(TiledVehiclesFromKafkaActor.props(router, "tiledVehicles", SPARK), "Kafka-Consumer-Spark") val flinkKafkaConsumer: ActorRef = system.actorOf(TiledVehiclesFromKafkaActor.props(router, "flinkTiledVehicles", FLINK), "Kafka-Consumer-Flink") val requestHandler: HttpRequest => HttpResponse = { case req@HttpRequest(GET, Uri.Path("/ws/vehicles"), _, _, _) => req.header[UpgradeToWebSocket] match { case Some(upgrade) => upgrade.handleMessages(Flows.graphFlowWithStats(router)) case None => HttpResponse(400, entity = "Not a valid websocket request!") } case _: HttpRequest => HttpResponse(404, entity = "Unknown resource!") } Http() .bindAndHandle(route(), serviceInterface, servicePort) .onComplete { case Success(_) => logger.info(s"Successfully bound to $serviceInterface:$servicePort") case Failure(e) => logger.error(s"Failed !!!! ${e.getMessage}") } Http() .bindAndHandleSync(requestHandler, serviceInterface, 8001) .onComplete { case Success(_) => logger.info(s"Successfully started Server to $serviceInterface:8001") case Failure(e) => logger.error(s"Failed !!!! ${e.getMessage}") } Await.ready(system.whenTerminated, Duration.Inf) CassandraConnector.close(session) } }
Example 48
Source File: VisualMailboxMetricServer.scala From akka-visualmailbox with Apache License 2.0 | 5 votes |
package de.aktey.akka.visualmailbox import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.io.Udp.{Bind, Bound, CommandFailed} import akka.io.{IO, Udp} import akka.pattern._ import akka.stream.ActorMaterializer import akka.util.Timeout import com.typesafe.config.ConfigFactory import de.aktey.akka.visualmailbox.data.DataSourceEndpoint import de.aktey.akka.visualmailbox.web.{Routing, WebConfig} import scala.concurrent.duration._ object VisualMailboxMetricServer extends App { val allConfig = ConfigFactory.load() val config = VisualMailboxMetricClientConfig.fromConfig(allConfig) implicit val system = ActorSystem("visualmailbox-visualizer") implicit val meterializer = ActorMaterializer() implicit val bindTimeout = Timeout(2.seconds) import system._ val router = system.actorOf(MetricsRouter.props(), "router") val dataHandler = system.actorOf(DataSourceEndpoint.props(router), "data-sink") (IO(Udp) ? Bind(dataHandler, config.serverAddress)).map { case CommandFailed(cmd) => system.terminate() case Bound(address) => log.info(s"""{"type":"udp-bound","address":"$address"}""") } val webConfig = WebConfig.fromConfig(allConfig) Http() .bindAndHandle(Routing.root(MetricFlow.metricSource(router)), webConfig.host, webConfig.port) .foreach { case ServerBinding(address) => log.info(s"""{"type":"http-bound","address":"$address"}""") } }
Example 49
Source File: Xmlrpc.scala From xmlrpc with MIT License | 5 votes |
package xmlrpc import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding._ import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._ import akka.http.scaladsl.model._ import akka.http.scaladsl.unmarshalling.{FromResponseUnmarshaller, Unmarshal} import akka.stream.Materializer import akka.util.Timeout import xmlrpc.protocol._ import scala.concurrent.{ExecutionContext, Future} import scala.xml.NodeSeq object Xmlrpc { import XmlrpcProtocol._ case class XmlrpcServer(fullAddress: String) { def uri: Uri = Uri(fullAddress) } def invokeMethod[P: Datatype, R: Datatype](name: String, parameter: P = Void) (implicit xmlrpcServer: XmlrpcServer, as: ActorSystem, ma: Materializer, ec: ExecutionContext, fc: Timeout): XmlrpcResponse[R] = { import XmlrpcResponse.AkkaHttpToXmlrpcResponse def unmarshall[A](f: Future[HttpResponse])(implicit um: FromResponseUnmarshaller[A]): Future[A] = f.flatMap(Unmarshal(_).to[A]) val request: NodeSeq = writeXmlRequest(name, parameter) val requestWithHeader: String = """<?xml version="1.0"?>""" + request.toString try { (Http().singleRequest(Post(xmlrpcServer.uri, request)) ~> unmarshall[NodeSeq]).asXmlrpcResponse[R] } catch { case t: Throwable => XmlrpcResponse(ConnectionError("An exception has been thrown by Spray", Some(t)).failures) } } }
Example 50
Source File: AkkaHttpBackend.scala From drunk with Apache License 2.0 | 5 votes |
package com.github.jarlakxen.drunk.backend import scala.collection.immutable import scala.concurrent.{ExecutionContext, Future} import akka.actor.ActorSystem import akka.http.scaladsl.{Http, HttpExt} import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpHeader, HttpMethods, HttpRequest, Uri} import akka.stream.ActorMaterializer class AkkaHttpBackend private[AkkaHttpBackend] ( uri: Uri, headers: immutable.Seq[HttpHeader], httpExt: HttpExt )(override implicit val as: ActorSystem, override implicit val mat: ActorMaterializer) extends AkkaBackend { def send(body: String): Future[(Int, String)] = { implicit val ec: ExecutionContext = as.dispatcher val req = HttpRequest(HttpMethods.POST, uri, headers, HttpEntity(ContentTypes.`application/json`, body)) val res = httpExt.singleRequest(req) res.flatMap { hr => val code = hr.status.intValue() val charsetFromHeaders = encodingFromContentType(hr.entity.contentType.toString).getOrElse("utf-8") val decodedResponse = decodeResponse(hr) val stringBody = bodyToString(decodedResponse, charsetFromHeaders) if (code >= 200 && code < 300) { stringBody.map { body => hr.discardEntityBytes() (code, body) } } else { stringBody.flatMap { body => hr.discardEntityBytes() Future.failed(new RuntimeException(s"${uri.toString} return $code with body: $body")) } } } } } object AkkaHttpBackend { val ContentTypeHeader = "Content-Type" def apply( uri: Uri, headers: immutable.Seq[HttpHeader] = Nil, httpExt: Option[HttpExt] = None )(implicit as: ActorSystem, mat: ActorMaterializer): AkkaHttpBackend = { val http = httpExt.getOrElse { Http(as) } new AkkaHttpBackend(uri, headers, http) } }
Example 51
Source File: package.scala From drunk with Apache License 2.0 | 5 votes |
package com.github.jarlakxen import java.net.InetSocketAddress import java.nio.channels.ServerSocketChannel import scala.concurrent._ import scala.concurrent.duration._ import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Route import akka.stream.ActorMaterializer import akka.testkit._ import org.scalatest.BeforeAndAfterAll package object drunk { trait TestHttpServer extends BeforeAndAfterAll { this: Spec => implicit val system: ActorSystem = ActorSystem("drunk-test") implicit def executor = system.dispatcher implicit val materializer = ActorMaterializer() private def temporaryServerAddress(interface: String = "127.0.0.1"): InetSocketAddress = { val serverSocket = ServerSocketChannel.open() try { serverSocket.socket.bind(new InetSocketAddress(interface, 0)) val port = serverSocket.socket.getLocalPort new InetSocketAddress(interface, port) } finally serverSocket.close() } private def temporaryServerHostnameAndPort(interface: String = "127.0.0.1"): (String, Int) = { val socketAddress = temporaryServerAddress(interface) (socketAddress.getHostName, socketAddress.getPort) } val (host, port) = temporaryServerHostnameAndPort() override protected def beforeAll(): Unit = Http().bindAndHandle(serverRoutes, host, port).futureValue override protected def afterAll(): Unit = TestKit.shutdownActorSystem(system) def serverRoutes: Route } }
Example 52
Source File: LowLevelServer.scala From akka-http-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend.component.lowlevelserver import akka.NotUsed import akka.actor.{ ActorSystem, Props } import akka.event.{ Logging, LoggingAdapter } import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.pattern.ask import akka.stream.scaladsl.{ Flow, Sink, Source } import akka.stream.{ ActorMaterializer, Materializer } import akka.util.Timeout import com.github.dnvriend.component.lowlevelserver.dto.{ Person, PersonWithId } import com.github.dnvriend.component.lowlevelserver.marshaller.Marshaller import com.github.dnvriend.component.lowlevelserver.repository.PersonRepository import spray.json.{ DefaultJsonProtocol, _ } import scala.concurrent.duration._ import scala.concurrent.{ ExecutionContext, Future } class LowLevelServer(implicit val system: ActorSystem, mat: Materializer, ec: ExecutionContext, log: LoggingAdapter, timeout: Timeout) extends DefaultJsonProtocol with Marshaller { val personDb = system.actorOf(Props[PersonRepository]) def debug(t: Any)(implicit log: LoggingAdapter = null): Unit = if (Option(log).isEmpty) println(t) else log.debug(t.toString) def http200Okay(req: HttpRequest): HttpResponse = HttpResponse(StatusCodes.OK) def http200AsyncOkay(req: HttpRequest): Future[HttpResponse] = Future(http200Okay(req)) val http200OkayFlow: Flow[HttpRequest, HttpResponse, NotUsed] = Flow[HttpRequest].map { req => HttpResponse(StatusCodes.OK) } val serverSource: Source[Http.IncomingConnection, Future[Http.ServerBinding]] = Http().bind(interface = "localhost", port = 8080) val binding: Future[Http.ServerBinding] = serverSource.to(Sink.foreach { conn => // conn.handleWith(http200OkayFlow) // conn.handleWithSyncHandler(http200Okay) // conn.handleWithAsyncHandler(http200AsyncOkay, 8) conn.handleWithAsyncHandler(personRequestHandler) }).run() def personRequestHandler(req: HttpRequest): Future[HttpResponse] = req match { case HttpRequest(HttpMethods.GET, Uri.Path("/api/person"), _, _, _) => for { xs <- (personDb ? "findAll").mapTo[List[PersonWithId]] entity = HttpEntity(ContentTypes.`application/json`, xs.toJson.compactPrint) } yield HttpResponse(StatusCodes.OK, entity = entity) case HttpRequest(HttpMethods.POST, Uri.Path("/api/person"), _, ent, _) => for { strictEntity <- ent.toStrict(1.second) person <- (personDb ? strictEntity.data.utf8String.parseJson.convertTo[Person]).mapTo[PersonWithId] } yield HttpResponse(StatusCodes.OK, entity = person.toJson.compactPrint) case req => req.discardEntityBytes() Future.successful(HttpResponse(StatusCodes.NotFound)) } } object LowLevelServerLauncher extends App with DefaultJsonProtocol { // setting up some machinery implicit val system: ActorSystem = ActorSystem() implicit val mat: Materializer = ActorMaterializer() implicit val ec: ExecutionContext = system.dispatcher implicit val log: LoggingAdapter = Logging(system, this.getClass) implicit val timeout: Timeout = Timeout(10.seconds) new LowLevelServer() }
Example 53
Source File: Main.scala From Pi-Akka-Cluster with Apache License 2.0 | 5 votes |
package com.lightbend.akka_oled import akka.NotUsed import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.adapter.TypedActorSystemOps import akka.actor.typed.{ActorSystem, Behavior, Terminated} import akka.cluster.ddata.LWWMapKey import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.management.scaladsl.AkkaManagement import akka.stream.Materializer import akkapi.cluster.{ClusterStatusTracker, OledClusterVisualizer, OledDriver, Settings} import spray.json.DefaultJsonProtocol object Main extends SprayJsonSupport with DefaultJsonProtocol { case class NodeStatus(status: String) implicit val transactionFormat = jsonFormat1(NodeStatus) def apply(settings: Settings): Behavior[NotUsed] = Behaviors.setup { ctx => val oledDriver = ctx.spawn(OledDriver(settings), "oled-driver") oledDriver ! OledDriver.RegisterView("Cluster State", 0) oledDriver ! OledDriver.RegisterView("Distributed Data State", 1) val clusterView = ctx.spawn(OledClusterVisualizer(0, settings, oledDriver), "oled-cluster-view") val clusterStatusTracker = ctx.spawn(ClusterStatusTracker(settings, None), "cluster-status-tracker") clusterStatusTracker ! ClusterStatusTracker.SubscribeVisualiser(clusterView) val ddataTracker = ctx.spawn( DistributedDataTracker(1, LWWMapKey[String, String]("cache"), oledDriver), "oled-ddata-view") val routes = new Routes(ddataTracker)(ctx.system) implicit val untypedSystem: akka.actor.ActorSystem = ctx.system.toClassic implicit val mat: Materializer = Materializer.createMaterializer(ctx.system.toClassic) Http()(ctx.system.toClassic).bindAndHandle(routes.route, settings.config.getString("cluster-node-configuration.external-ip"), 8080) Behaviors.receiveSignal { case (_, Terminated(_)) => Behaviors.stopped } } } object DisplayDistributedDataMain { def main(args: Array[String]): Unit = { val settings = Settings() val system = ActorSystem[NotUsed](Main(settings), "akka-oled", settings.config) // Start Akka HTTP Management extension AkkaManagement(system).start() } }
Example 54
Source File: Main.scala From Pi-Akka-Cluster with Apache License 2.0 | 5 votes |
package com.lightbend.akka_oled import akka.NotUsed import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.adapter.TypedActorSystemOps import akka.actor.typed.{ActorSystem, Behavior, Terminated} import akka.cluster.sharding.typed.scaladsl.{ClusterSharding, Entity} import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.management.scaladsl.AkkaManagement import akka.persistence.typed.PersistenceId import akka.stream.Materializer import akkapi.cluster.{ClusterStatusTracker, OledClusterVisualizer, OledDriver, Settings} import spray.json._ import scala.concurrent.ExecutionContextExecutor object Main extends SprayJsonSupport with DefaultJsonProtocol { case class AddPoints(points: Int) implicit val transactionFormat = jsonFormat1(AddPoints) def apply(settings: Settings): Behavior[NotUsed] = Behaviors.setup { ctx => implicit val system = ctx.system implicit val untypedSystem: akka.actor.ActorSystem = ctx.system.toClassic implicit val ec: ExecutionContextExecutor = ctx.system.executionContext val oledDriver = ctx.spawn(OledDriver(settings), "oled-driver") oledDriver ! OledDriver.RegisterView("Cluster State", 0) oledDriver ! OledDriver.RegisterView("Sharding State", 1) val clusterView = ctx.spawn(OledClusterVisualizer(0, settings, oledDriver), "oled-cluster-view") val clusterStatusTracker = ctx.spawn(ClusterStatusTracker(settings, None), "cluster-status-tracker") clusterStatusTracker ! ClusterStatusTracker.SubscribeVisualiser(clusterView) val shardVisualizer = ctx.spawn(OledShardingVisualizer(1, oledDriver), "oled-sharding-view") val sharding = ClusterSharding(ctx.system) sharding.init(Entity(typeKey = ClientEntity.TypeKey) { entityContext => ClientEntity(entityContext.entityId, PersistenceId(entityContext.entityTypeKey.name, entityContext.entityId), shardVisualizer) }) val tracker = ctx.spawn(ShardStateTracker(shardVisualizer), "oled-sharding-tracker") ctx.spawn(ShardStateScheduler(sharding.shardState, tracker), "oled-sharding-scheduler") val routes = new Routes(sharding) //materializer Materializer.createMaterializer(ctx.system.toClassic) implicit val mat: Materializer = Materializer.createMaterializer(ctx.system.toClassic) Http()(ctx.system.toClassic).bindAndHandle(routes.route, settings.config.getString("cluster-node-configuration.external-ip"), 8080) Behaviors.receiveSignal { case (_, Terminated(_)) => Behaviors.stopped } } } object DisplayClusterShardingMain { def main(args: Array[String]): Unit = { val settings = Settings() val system = ActorSystem[NotUsed](Main(settings), "akka-oled", settings.config) // Start Akka HTTP Management extension AkkaManagement(system).start() } }
Example 55
Source File: ServerTestBase.scala From endpoints4s with MIT License | 5 votes |
package endpoints4s.algebra.server import java.nio.charset.StandardCharsets import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.`Content-Type` import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.util.ByteString import endpoints4s.algebra import org.scalatest.concurrent.ScalaFutures import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll} import scala.concurrent.duration._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import scala.concurrent.{ExecutionContext, Future} trait ServerTestBase[T <: algebra.Endpoints] extends AnyWordSpec with Matchers with ScalaFutures with BeforeAndAfterAll with BeforeAndAfter { override implicit def patienceConfig: PatienceConfig = PatienceConfig(10.seconds, 10.millisecond) val serverApi: T case class Malformed(errors: Seq[String]) extends DecodedUrl[Nothing] }
Example 56
Source File: Main.scala From endpoints4s with MIT License | 5 votes |
package quickstart //#relevant-code import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ object Main extends App { implicit val system: ActorSystem = ActorSystem("server-system") val routes = CounterServer.routes ~ DocumentationServer.routes Http().bindAndHandle(routes, "0.0.0.0", 8000) } // Additional route for serving the OpenAPI documentation //#serving-documentation import endpoints4s.openapi.model.OpenApi import endpoints4s.akkahttp.server object DocumentationServer extends server.Endpoints with server.JsonEntitiesFromEncodersAndDecoders { val routes = endpoint(get(path / "documentation.json"), ok(jsonResponse[OpenApi])) .implementedBy(_ => CounterDocumentation.api) } //#serving-documentation //#relevant-code
Example 57
Source File: CounterTest.scala From endpoints4s with MIT License | 5 votes |
package quickstart import java.net.ServerSocket import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpMethods, HttpRequest, StatusCodes} import akka.http.scaladsl.server.Directives._ import org.scalatest.BeforeAndAfterAll import scala.concurrent.Await import scala.concurrent.duration.DurationInt import org.scalatest.freespec.AsyncFreeSpec class CounterTest extends AsyncFreeSpec with BeforeAndAfterAll { implicit val actorSystem: ActorSystem = ActorSystem() val routes = CounterServer.routes ~ DocumentationServer.routes val interface = "0.0.0.0" val port = findOpenPort() val server = Http().bindAndHandle(routes, interface, port) override protected def afterAll(): Unit = { Await.result( Await.result(server, 10.seconds).terminate(3.seconds), 15.seconds ) Await.result(actorSystem.terminate(), 5.seconds) super.afterAll() } "CounterServer" - { "Query counter value" in { for { response <- Http().singleRequest( HttpRequest(uri = uri("/current-value")) ) entity <- response.entity.toStrict(1.second) } yield { assert(response.status == StatusCodes.OK) assert(entity.contentType == ContentTypes.`application/json`) assert(entity.data.utf8String == "{\"value\":0}") } } "Increment counter value" in { val request = HttpRequest( method = HttpMethods.POST, uri = uri("/increment"), entity = HttpEntity(ContentTypes.`application/json`, "{\"step\":1}") ) for { response <- Http().singleRequest(request) } yield { assert(response.status == StatusCodes.OK) } } "Query API documentation" in { for { response <- Http().singleRequest( HttpRequest(uri = uri("/documentation.json")) ) entity <- response.entity.toStrict(1.second) } yield { assert(response.status == StatusCodes.OK) assert(entity.contentType == ContentTypes.`application/json`) } } } def findOpenPort(): Int = { val socket = new ServerSocket(0) try socket.getLocalPort finally if (socket != null) socket.close() } def uri(suffix: String) = s"http://$interface:$port$suffix" }
Example 58
Source File: Server.scala From endpoints4s with MIT License | 5 votes |
package sample import akka.actor.ActorSystem import akka.http.scaladsl.Http import scala.io.StdIn object Server { def main(args: Array[String]): Unit = { implicit val system = ActorSystem("my-system") implicit val executionContext = system.dispatcher val bindingFuture = Http().bindAndHandle(Api.routes, "localhost", 8080) println(s"Server online at http://localhost:8080/\nPress RETURN to stop...") StdIn.readLine() bindingFuture .flatMap(_.unbind()) .onComplete(_ => system.terminate()) } }
Example 59
Source File: EndpointsSettings.scala From endpoints4s with MIT License | 5 votes |
package endpoints4s.akkahttp.client import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse, Uri} import akka.stream.Materializer import akka.stream.scaladsl.{Flow, Sink, Source} import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.Try final case class EndpointsSettings( requestExecutor: AkkaHttpRequestExecutor, baseUri: Uri = Uri("/"), toStrictTimeout: FiniteDuration = 2.seconds, stringContentExtractor: HttpEntity.Strict => String = _.data.utf8String ) trait AkkaHttpRequestExecutor { def apply(request: HttpRequest): Future[HttpResponse] } object AkkaHttpRequestExecutor { def cachedHostConnectionPool(host: String, port: Int)(implicit system: ActorSystem, materializer: Materializer ): AkkaHttpRequestExecutor = default(Http().cachedHostConnectionPool[Int](host, port)) def default( poolClientFlow: Flow[ (HttpRequest, Int), (Try[HttpResponse], Int), Http.HostConnectionPool ] )(implicit materializer: Materializer): AkkaHttpRequestExecutor = new AkkaHttpRequestExecutor { override def apply(request: HttpRequest): Future[HttpResponse] = Source .single(request -> 1) .via(poolClientFlow) .map(_._1.get) .runWith(Sink.head) } }
Example 60
Source File: RequestManager.scala From wix-http-testkit with MIT License | 5 votes |
package com.wix.e2e.http.client.internals import akka.http.scaladsl.Http import akka.http.scaladsl.model.TransferEncodings.chunked import akka.http.scaladsl.model.headers.{ProductVersion, `Transfer-Encoding`, `User-Agent`} import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.stream.StreamTcpException import com.wix.e2e.http._ import com.wix.e2e.http.exceptions.ConnectionRefusedException import com.wix.e2e.http.info.HttpTestkitVersion import com.wix.e2e.http.utils._ import scala.concurrent.Future import scala.concurrent.duration._ trait RequestManager[R] { def apply(path: String, but: RequestTransformer = identity, withTimeout: FiniteDuration = 5.seconds)(implicit baseUri: BaseUri): R } class NonBlockingRequestManager(request: HttpRequest) extends RequestManager[Future[HttpResponse]] { def apply(path: String, but: RequestTransformer, withTimeout: FiniteDuration)(implicit baseUri: BaseUri): Future[HttpResponse] = { val transformed = Seq(composeUrlFor(baseUri, path), but) .foldLeft(request) { case (r, tr) => tr(r) } import WixHttpTestkitResources.{executionContext, materializer, system} Http().singleRequest(request = transformed, settings = settingsWith(withTimeout)) .map( recreateTransferEncodingHeader ) .flatMap( _.toStrict(withTimeout) ) .recoverWith( { case _: StreamTcpException => Future.failed(new ConnectionRefusedException(baseUri)) } ) } private def composeUrlFor(baseUri: BaseUri, path: String): RequestTransformer = _.copy(uri = baseUri.asUriWith(path) ) private def recreateTransferEncodingHeader(r: HttpResponse) = if ( !r.entity.isChunked ) r else { val encodings = r.header[`Transfer-Encoding`] .map( _.encodings ) .getOrElse( Seq.empty ) r.removeHeader("Transfer-Encoding") .addHeader(`Transfer-Encoding`(chunked, encodings:_*)) } private def settingsWith(timeout: FiniteDuration) = { val settings = ConnectionPoolSettings(WixHttpTestkitResources.system) settings.withConnectionSettings( settings.connectionSettings .withConnectingTimeout(timeout) .withIdleTimeout(timeout) .withUserAgentHeader(Some(`User-Agent`(ProductVersion("client-http-testkit", HttpTestkitVersion)))) ) .withMaxConnections(32) .withPipeliningLimit(4) .withMaxRetries(0) } } class BlockingRequestManager(request: HttpRequest) extends RequestManager[HttpResponse] { def apply(path: String, but: RequestTransformer, withTimeout: FiniteDuration)(implicit baseUri: BaseUri): HttpResponse = waitFor(nonBlockingRequestManager(path, but, withTimeout))(withTimeout + 1.second) private val nonBlockingRequestManager = new NonBlockingRequestManager(request) }
Example 61
Source File: AkkaHttpMockWebServer.scala From wix-http-testkit with MIT License | 5 votes |
package com.wix.e2e.http.server.internals import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.model.headers.{ProductVersion, Server} import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.settings.ServerSettings import com.wix.e2e.http.api.BaseWebServer import com.wix.e2e.http.info.HttpTestkitVersion import com.wix.e2e.http.utils._ import com.wix.e2e.http.{BaseUri, RequestHandler, WixHttpTestkitResources} import scala.concurrent.Future import scala.concurrent.duration._ abstract class AkkaHttpMockWebServer(specificPort: Option[Int], val initialHandlers: Seq[RequestHandler]) extends BaseWebServer with AdjustableServerBehaviorSupport { import WixHttpTestkitResources.{executionContext, materializer, system} protected def serverBehavior: RequestHandler def start() = this.synchronized { val s = waitFor( Http().bindAndHandleAsync(handler = TransformToStrictAndHandle, interface = "localhost", settings = customSettings, port = specificPort.getOrElse( AllocateDynamicPort )) ) serverBinding = Option(s) println(s"Web server started on port: ${baseUri.port}.") this } def stop() = this.synchronized { serverBinding.foreach{ s => waitFor( s.unbind() ) } serverBinding = None this } def baseUri = specificPort.map( p => BaseUri("localhost", port = p) ) .orElse( serverBinding.map( s => BaseUri(port = s.localAddress.getPort) )) .getOrElse( throw new IllegalStateException("Server port and baseUri will have value after server is started") ) private var serverBinding: Option[ServerBinding] = None private val AllocateDynamicPort = 0 private val TransformToStrictAndHandle: HttpRequest => Future[HttpResponse] = _.toStrict(1.minutes).map( serverBehavior ) private def customSettings = ServerSettings(system).withTransparentHeadRequests(false) .withServerHeader( Some(Server(ProductVersion("server-http-testkit", HttpTestkitVersion))) ) }
Example 62
Source File: Server.scala From scalachain with MIT License | 5 votes |
package com.elleflorio.scalachain import akka.actor.{ActorRef, ActorSystem} import akka.cluster.pubsub.DistributedPubSub import akka.http.scaladsl.Http import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import com.elleflorio.scalachain.actor.Node import com.elleflorio.scalachain.api.NodeRoutes import com.elleflorio.scalachain.cluster.ClusterManager import com.typesafe.config.{Config, ConfigFactory} import scala.concurrent.Await import scala.concurrent.duration.Duration object Server extends App with NodeRoutes { implicit val system: ActorSystem = ActorSystem("scalachain") implicit val materializer: ActorMaterializer = ActorMaterializer() val config: Config = ConfigFactory.load() val address = config.getString("http.ip") val port = config.getInt("http.port") val nodeId = config.getString("scalachain.node.id") lazy val routes: Route = statusRoutes ~ transactionRoutes ~ mineRoutes val clusterManager: ActorRef = system.actorOf(ClusterManager.props(nodeId), "clusterManager") val mediator: ActorRef = DistributedPubSub(system).mediator val node: ActorRef = system.actorOf(Node.props(nodeId, mediator), "node") Http().bindAndHandle(routes, address, port) println(s"Server online at http://$address:$port/") Await.result(system.whenTerminated, Duration.Inf) }
Example 63
Source File: package.scala From healthchecks with MIT License | 5 votes |
package com.github.everpeace.healthchecks import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.stream.ActorMaterializer import scala.concurrent.{ExecutionContext, Future} package object k8s { private def configPathRoot = "k8s_probe" private def config(system: ActorSystem, subPath: String = "") = { val path = if (subPath.nonEmpty) configPathRoot + "." + subPath else configPathRoot system.settings.config.getConfig(path) } def livenessProbe( checks: HealthCheck* )(implicit system: ActorSystem, ec: ExecutionContext ) = { LivenessProbe(checks.toList, config(system, "path").getString("liveness"), ec) } def readinessProbe( checks: HealthCheck* )(implicit system: ActorSystem, ec: ExecutionContext ) = { ReadinessProbe(checks.toList, config(system, "path").getString("readiness"), ec) } def bindAndHandleProbes( probe: K8sProbe, probes: K8sProbe* )(implicit system: ActorSystem, am: ActorMaterializer ): Future[Http.ServerBinding] = { val host = config(system).getString("host") val port = config(system).getInt("port") val routes = (probe +: probes).toList .map(_.toRoute) .reduce((r1: Route, r2: Route) => r1 ~ r2) Http(system).bindAndHandle(routes, host, port) } }
Example 64
Source File: K8sProbesTest.scala From healthchecks with MIT License | 5 votes |
package com.chatwork.healthcheck.k8s import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, StatusCodes} import akka.stream.ActorMaterializer import com.github.everpeace.healthchecks._ import com.github.everpeace.healthchecks.k8s._ import org.scalatest._ import scala.concurrent.duration._ import scala.concurrent.{Await, Future} class K8sProbesTest extends FreeSpec with Matchers { private def fixture(probe: K8sProbe, probes: K8sProbe*) = new {} "K8sProbes" - { "should start successfully and return correct response" in { implicit val system = ActorSystem() implicit val am = ActorMaterializer() implicit val ec = system.dispatcher val probeBinding = bindAndHandleProbes( readinessProbe(healthCheck("readiness_check")(healthy)), livenessProbe(asyncHealthCheck("liveness_check")(Future(healthy))) ) def requestToLivenessProbe = Http().singleRequest(HttpRequest(uri = "http://localhost:8086/live")) def requestToReadinessProbe = Http().singleRequest(HttpRequest(uri = "http://localhost:8086/ready")) val livenessResponse = Await.result(requestToLivenessProbe, 10 seconds) val redinessResponse = Await.result(requestToReadinessProbe, 10 seconds) livenessResponse.status shouldEqual StatusCodes.OK redinessResponse.status shouldEqual StatusCodes.OK system.terminate() } } }
Example 65
Source File: Main.scala From sns with Apache License 2.0 | 5 votes |
package me.snov.sns import akka.actor.ActorSystem import akka.event.{Logging, LoggingAdapter} import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server._ import akka.stream.ActorMaterializer import akka.util.Timeout import com.typesafe.config.ConfigFactory import me.snov.sns.actor._ import me.snov.sns.api._ import me.snov.sns.service.FileDbService import me.snov.sns.util.ToStrict import scala.concurrent.ExecutionContext import scala.concurrent.duration._ import scala.util.Properties object Main extends App with ToStrict { implicit val system = ActorSystem("sns") implicit val executor: ExecutionContext = system.dispatcher implicit val materializer: ActorMaterializer = ActorMaterializer() implicit val logger: LoggingAdapter = Logging(system, getClass) implicit val timeout = new Timeout(1.second) val config = ConfigFactory.load() val dbService = new FileDbService(Properties.envOrElse("DB_PATH", config.getString("db.path"))) val dbActor = system.actorOf(DbActor.props(dbService), name = "DbActor") val homeActor = system.actorOf(HomeActor.props, name = "HomeActor") val subscribeActor = system.actorOf(SubscribeActor.props(dbActor), name = "SubscribeActor") val publishActor = system.actorOf(PublishActor.props(subscribeActor), name = "PublishActor") val routes: Route = toStrict { TopicApi.route(subscribeActor) ~ SubscribeApi.route(subscribeActor) ~ PublishApi.route(publishActor) ~ HealthCheckApi.route ~ HomeApi.route(homeActor) } logger.info("SNS v{} is starting", getClass.getPackage.getImplementationVersion) Http().bindAndHandle( handler = logRequestResult("akka-http-sns")(routes), interface = Properties.envOrElse("HTTP_INTERFACE", config.getString("http.interface")), port = Properties.envOrElse("HTTP_PORT", config.getString("http.port")).toInt ) }
Example 66
Source File: DonutStoreHttpController.scala From scala-for-beginners with Apache License 2.0 | 5 votes |
package com.allaboutscala.donutstore.httpserver import akka.http.scaladsl.Http import com.typesafe.scalalogging.LazyLogging import scala.io.StdIn import scala.util.{Failure, Success} trait DonutStoreHttpController extends LazyLogging { this: DonutStoreServices => def startAndBind(): Unit = { logger.info("Initializing and binding Akka HTTP server") val httpServerFuture = Http().bindAndHandle(donutApiRoutes, cfg.httpServer.ip, cfg.httpServer.port) httpServerFuture.onComplete { case Success(binding) => logger.info(s"Akka Http Server is bound to ${binding.localAddress}") logger.info(s"To stop the server, press the [Enter] key in IntelliJ's console.") case Failure(e) => logger.error(s"Akka Http server failed to bind to ${cfg.httpServer.ip}:${cfg.httpServer.port}",e) system.terminate() } // pressing enter key will kill the server StdIn.readLine() for { serverBinding <- httpServerFuture _ <- serverBinding.unbind() terminated <- system.terminate() } yield logger.info(s"Akka Http server was terminated = $terminated") } }
Example 67
Source File: DonutStoreHttpController.scala From scala-for-beginners with Apache License 2.0 | 5 votes |
package com.allaboutscala.donutstore.httpserver import akka.http.scaladsl.Http import com.typesafe.scalalogging.LazyLogging import scala.io.StdIn import scala.util.{Failure, Success} trait DonutStoreHttpController extends LazyLogging { this: DonutStoreServices => def startAndBind(): Unit = { logger.info("Initializing and binding Akka HTTP server") val httpServerFuture = Http().bindAndHandle(donutApiRoutes, cfg.httpServer.ip, cfg.httpServer.port) httpServerFuture.onComplete { case Success(binding) => logger.info(s"Akka Http Server is bound to ${binding.localAddress}") logger.info(s"To stop the server, press the [Enter] key in IntelliJ's console.") case Failure(e) => logger.error(s"Akka Http server failed to bind to ${cfg.httpServer.ip}:${cfg.httpServer.port}",e) system.terminate() } // pressing enter key will kill the server StdIn.readLine() for { serverBinding <- httpServerFuture _ <- serverBinding.unbind() terminated <- system.terminate() } yield logger.info(s"Akka Http server was terminated = $terminated") } }
Example 68
Source File: AkkaInterpExampleMain.scala From hammock with MIT License | 5 votes |
package example.interpret import akka.actor.ActorSystem import akka.http.scaladsl.{Http, HttpExt} import akka.stream.ActorMaterializer import cats.effect.{ContextShift, IO} import example.repr.{GetResp, GetRespWithQueryString, Req, Resp} import hammock.{Hammock, Method} import hammock.marshalling._ import hammock.circe.implicits._ import io.circe.generic.auto._ import hammock.akka.AkkaInterpreter._ import scala.concurrent.ExecutionContext object AkkaInterpExampleMain extends App { implicit val actorSystem: ActorSystem = ActorSystem() implicit val materializer: ActorMaterializer = ActorMaterializer() implicit val ec: ExecutionContext = ExecutionContext.Implicits.global implicit val cs: ContextShift[IO] = IO.contextShift(ec) implicit val client: HttpExt = Http(actorSystem) //GET val getResp = Hammock .request(Method.GET, getUri, Map()) .as[GetResp] .exec[IO] .unsafeRunSync println(s"GET::Response = $getResp") //GET with query string val getRespWithQueryString = Hammock .request(Method.GET, getUriWithQueryString, Map()) .as[GetRespWithQueryString] .exec[IO] .unsafeRunSync println(s"GET with query string::Response = $getRespWithQueryString") //POST val postResp = Hammock .request(Method.POST, postUri, Map(), Some(Req("name", 4))) .as[Resp] .exec[IO] .unsafeRunSync println(s"POST::Response = $postResp") //PUT val putResp = Hammock .request(Method.PUT, putUri, Map(), Some(Req("name", 4))) .as[Resp] .exec[IO] .unsafeRunSync println(s"PUT::Response = $putResp") //DELETE val deleteResp = Hammock .request(Method.DELETE, deleteUri, Map(), Some(Req("name", 4))) .exec[IO] .unsafeRunSync println(s"DELETE::Response = $deleteResp") actorSystem.terminate() }
Example 69
Source File: MockOAuth2Server.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.services.security.oauth2 import scala.concurrent.{Await, Future} import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.stream.ActorMaterializer import akka.stream.scaladsl.Sink import org.apache.gearpump.util.Util // NOTE: This cannot be removed!! import org.apache.gearpump.services.util.UpickleUtil._ class MockOAuth2Server( actorSystem: ActorSystem, var requestHandler: HttpRequest => HttpResponse) { implicit val system: ActorSystem = actorSystem implicit val materializer = ActorMaterializer() implicit val ec = system.dispatcher private var _port: Int = 0 private var bindingFuture: Future[ServerBinding] = null def port: Int = _port def start(): Unit = { _port = Util.findFreePort().get val serverSource = Http().bind(interface = "127.0.0.1", port = _port) bindingFuture = { serverSource.to(Sink.foreach { connection => connection handleWithSyncHandler requestHandler }).run() } } def stop(): Unit = { import scala.concurrent.duration._ Await.result(bindingFuture.map(_.unbind()), 120.seconds) } }
Example 70
Source File: OrderServiceApp.scala From 006877 with MIT License | 5 votes |
package aia.integration import scala.concurrent.Future import akka.actor.{ ActorSystem , Actor, Props } import akka.event.Logging import akka.util.Timeout import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import com.typesafe.config.{ Config, ConfigFactory } object OrderServiceApp extends App with RequestTimeout { val config = ConfigFactory.load() val host = config.getString("http.host") val port = config.getInt("http.port") implicit val system = ActorSystem() implicit val ec = system.dispatcher val processOrders = system.actorOf( Props(new ProcessOrders), "process-orders" ) val api = new OrderServiceApi(system, requestTimeout(config), processOrders).routes implicit val materializer = ActorMaterializer() val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(api, host, port) val log = Logging(system.eventStream, "order-service") bindingFuture.map { serverBinding => log.info(s"Bound to ${serverBinding.localAddress} ") }.failed.foreach { case ex: Exception => log.error(ex, "Failed to bind to {}:{}!", host, port) system.terminate() } } trait RequestTimeout { import scala.concurrent.duration._ def requestTimeout(config: Config): Timeout = { val t = config.getString("akka.http.server.request-timeout") val d = Duration(t) FiniteDuration(d.length, d.unit) } }
Example 71
Source File: ShoppersServiceSupport.scala From 006877 with MIT License | 5 votes |
package aia.persistence.rest import com.typesafe.config.Config import scala.concurrent.Future import akka.actor._ import akka.event.Logging import akka.util.Timeout import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.stream.ActorMaterializer import aia.persistence._ trait ShoppersServiceSupport extends RequestTimeout { def startService(shoppers: ActorRef)(implicit system: ActorSystem) = { val config = system.settings.config val settings = Settings(system) val host = settings.http.host val port = settings.http.port implicit val ec = system.dispatcher //bindAndHandle requires an implicit ExecutionContext val api = new ShoppersService(shoppers, system, requestTimeout(config)).routes // the RestApi provides a Route implicit val materializer = ActorMaterializer() val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(api, host, port) val log = Logging(system.eventStream, "shoppers") bindingFuture.map { serverBinding => log.info(s"Shoppers API bound to ${serverBinding.localAddress} ") }.failed.foreach { case ex: Exception => log.error(ex, "Failed to bind to {}:{}!", host, port) system.terminate() } } } trait RequestTimeout { import scala.concurrent.duration._ def requestTimeout(config: Config): Timeout = { val t = config.getString("akka.http.server.request-timeout") val d = Duration(t) FiniteDuration(d.length, d.unit) } }
Example 72
Source File: Main.scala From 006877 with MIT License | 5 votes |
package com.goticks import scala.concurrent.Future import scala.util.{Failure, Success} import akka.actor.{ ActorSystem , Actor, Props } import akka.event.Logging import akka.util.Timeout import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import com.typesafe.config.{ Config, ConfigFactory } object Main extends App with RequestTimeout { val config = ConfigFactory.load() val host = config.getString("http.host") // 설정으로부터 호스트와 포트를 가져온다 val port = config.getInt("http.port") implicit val system = ActorSystem() implicit val ec = system.dispatcher // bindAndHandle은 비동기적이며, ExecutionContext를 암시적으로 사용해야 한다 val api = new RestApi(system, requestTimeout(config)).routes // RestApi는 HTTP 루트를 제공한다 implicit val materializer = ActorMaterializer() val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(api, host, port) // RestApi 루트를 가지고 HTTP 서버를 시작한다 val log = Logging(system.eventStream, "go-ticks") bindingFuture.map { serverBinding => log.info(s"RestApi bound to ${serverBinding.localAddress} ") }.onComplete { case Success(v) => case Failure(ex) => log.error(ex, "Failed to bind to {}:{}!", host, port) system.terminate() } } trait RequestTimeout { import scala.concurrent.duration._ def requestTimeout(config: Config): Timeout = { val t = config.getString("akka.http.server.request-timeout") val d = Duration(t) FiniteDuration(d.length, d.unit) } }
Example 73
Source File: Startup.scala From 006877 with MIT License | 5 votes |
package com.goticks import scala.concurrent.Future import scala.util.{Failure, Success} import akka.actor.ActorSystem import akka.event.Logging import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.server.Route import akka.stream.ActorMaterializer trait Startup extends RequestTimeout { def startup(api: Route)(implicit system: ActorSystem) = { val host = system.settings.config.getString("http.host") // 설정에서 호스트와 포트를 가져온다 val port = system.settings.config.getInt("http.port") startHttpServer(api, host, port) } def startHttpServer(api: Route, host: String, port: Int) (implicit system: ActorSystem) = { implicit val ec = system.dispatcher // bindAndHandle에는 암시적인 ExecutionContext가 필요하다 implicit val materializer = ActorMaterializer() val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(api, host, port) // HTTP 서버를 시작한다 val log = Logging(system.eventStream, "go-ticks") bindingFuture.map { serverBinding => log.info(s"RestApi bound to ${serverBinding.localAddress} ") }.onComplete { case Success(v) => case Failure(ex) => log.error(ex, "Failed to bind to {}:{}!", host, port) system.terminate() } } }
Example 74
Source File: FanLogsApp.scala From 006877 with MIT License | 5 votes |
package aia.stream import java.nio.file.{ Files, FileSystems, Path } import scala.concurrent.Future import scala.concurrent.duration._ import akka.NotUsed import akka.actor.{ ActorSystem , Actor, Props } import akka.event.Logging import akka.stream.{ ActorMaterializer, ActorMaterializerSettings, Supervision } import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.server.Directives._ import com.typesafe.config.{ Config, ConfigFactory } object FanLogsApp extends App { val config = ConfigFactory.load() val host = config.getString("http.host") val port = config.getInt("http.port") val logsDir = { val dir = config.getString("log-stream-processor.logs-dir") Files.createDirectories(FileSystems.getDefault.getPath(dir)) } val maxLine = config.getInt("log-stream-processor.max-line") val maxJsObject = config.getInt("log-stream-processor.max-json-object") implicit val system = ActorSystem() implicit val ec = system.dispatcher val decider : Supervision.Decider = { case _: LogStreamProcessor.LogParseException => Supervision.Resume case _ => Supervision.Stop } implicit val materializer = ActorMaterializer( ActorMaterializerSettings(system) .withSupervisionStrategy(decider) ) val api = new FanLogsApi(logsDir, maxLine, maxJsObject).routes val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(api, host, port) val log = Logging(system.eventStream, "fan-logs") bindingFuture.map { serverBinding => log.info(s"Bound to ${serverBinding.localAddress} ") }.onFailure { case ex: Exception => log.error(ex, "Failed to bind to {}:{}!", host, port) system.terminate() } }
Example 75
Source File: LogsApp.scala From 006877 with MIT License | 5 votes |
package aia.stream import java.nio.file.{ Files, FileSystems, Path } import scala.concurrent.Future import scala.concurrent.duration._ import akka.NotUsed import akka.actor.{ ActorSystem , Actor, Props } import akka.event.Logging import akka.stream.{ ActorMaterializer, ActorMaterializerSettings, Supervision } import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.server.Directives._ import com.typesafe.config.{ Config, ConfigFactory } object LogsApp extends App { val config = ConfigFactory.load() val host = config.getString("http.host") val port = config.getInt("http.port") val logsDir = { val dir = config.getString("log-stream-processor.logs-dir") Files.createDirectories(FileSystems.getDefault.getPath(dir)) } val maxLine = config.getInt("log-stream-processor.max-line") implicit val system = ActorSystem() implicit val ec = system.dispatcher val decider : Supervision.Decider = { case _: LogStreamProcessor.LogParseException => Supervision.Stop case _ => Supervision.Stop } implicit val materializer = ActorMaterializer( ActorMaterializerSettings(system) .withSupervisionStrategy(decider) ) val api = new LogsApi(logsDir, maxLine).routes val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(api, host, port) val log = Logging(system.eventStream, "logs") bindingFuture.map { serverBinding => log.info(s"Bound to ${serverBinding.localAddress} ") }.onFailure { case ex: Exception => log.error(ex, "Failed to bind to {}:{}!", host, port) system.terminate() } }
Example 76
Source File: LogStreamProcessorApp.scala From 006877 with MIT License | 5 votes |
package aia.stream import java.nio.file.{ Files, FileSystems, Path } import scala.concurrent.Future import scala.concurrent.duration._ import akka.NotUsed import akka.actor.{ ActorSystem , Actor, Props } import akka.event.Logging import akka.stream.{ ActorMaterializer, ActorMaterializerSettings, Supervision } import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.server.Directives._ import com.typesafe.config.{ Config, ConfigFactory } object LogStreamProcessorApp extends App { val config = ConfigFactory.load() val host = config.getString("http.host") val port = config.getInt("http.port") val logsDir = { val dir = config.getString("log-stream-processor.logs-dir") Files.createDirectories(FileSystems.getDefault.getPath(dir)) } val notificationsDir = { val dir = config.getString("log-stream-processor.notifications-dir") Files.createDirectories(FileSystems.getDefault.getPath(dir)) } val metricsDir = { val dir = config.getString("log-stream-processor.metrics-dir") Files.createDirectories(FileSystems.getDefault.getPath(dir)) } val maxLine = config.getInt("log-stream-processor.max-line") val maxJsObject = config.getInt("log-stream-processor.max-json-object") implicit val system = ActorSystem() implicit val ec = system.dispatcher val decider : Supervision.Decider = { case _: LogStreamProcessor.LogParseException => Supervision.Resume case _ => Supervision.Stop } implicit val materializer = ActorMaterializer( ActorMaterializerSettings(system) .withSupervisionStrategy(decider) ) val api = new LogStreamProcessorApi(logsDir, notificationsDir, metricsDir, maxLine, maxJsObject).routes val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(api, host, port) val log = Logging(system.eventStream, "processor") bindingFuture.map { serverBinding => log.info(s"Bound to ${serverBinding.localAddress} ") }.onFailure { case ex: Exception => log.error(ex, "Failed to bind to {}:{}!", host, port) system.terminate() } }
Example 77
Source File: ContentNegLogsApp.scala From 006877 with MIT License | 5 votes |
package aia.stream import java.nio.file.{ Files, FileSystems, Path } import scala.concurrent.Future import scala.concurrent.duration._ import akka.NotUsed import akka.actor.{ ActorSystem , Actor, Props } import akka.event.Logging import akka.stream.{ ActorMaterializer, ActorMaterializerSettings, Supervision } import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.server.Directives._ import com.typesafe.config.{ Config, ConfigFactory } object ContentNegLogsApp extends App { val config = ConfigFactory.load() val host = config.getString("http.host") val port = config.getInt("http.port") val logsDir = { val dir = config.getString("log-stream-processor.logs-dir") Files.createDirectories(FileSystems.getDefault.getPath(dir)) } val maxLine = config.getInt("log-stream-processor.max-line") val maxJsObject = config.getInt("log-stream-processor.max-json-object") implicit val system = ActorSystem() implicit val ec = system.dispatcher val decider : Supervision.Decider = { case _: LogStreamProcessor.LogParseException => Supervision.Stop case _ => Supervision.Stop } implicit val materializer = ActorMaterializer( ActorMaterializerSettings(system) .withSupervisionStrategy(decider) ) val api = new ContentNegLogsApi(logsDir, maxLine, maxJsObject).routes val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(api, host, port) val log = Logging(system.eventStream, "content-neg-logs") bindingFuture.map { serverBinding => log.info(s"Bound to ${serverBinding.localAddress} ") }.onFailure { case ex: Exception => log.error(ex, "Failed to bind to {}:{}!", host, port) system.terminate() } }
Example 78
Source File: HttpFeeRateProvider.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.feeprovider import java.time.{Duration, Instant} import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, Uri} import akka.util.ByteString import org.bitcoins.core.api.FeeRateApi import org.bitcoins.core.util.TimeUtil import org.bitcoins.core.wallet.fee.FeeUnit import scala.concurrent.{ExecutionContextExecutor, Future} import scala.util.Try object HttpFeeRateProvider { def makeApiCall(uri: Uri)(implicit system: ActorSystem): Future[String] = { implicit val ec: ExecutionContextExecutor = system.dispatcher Http() .singleRequest(HttpRequest(uri = uri)) .flatMap(response => response.entity.dataBytes .runFold(ByteString.empty)(_ ++ _) .map(payload => payload.decodeString(ByteString.UTF_8))) } } abstract class HttpFeeRateProvider extends FeeRateApi { implicit protected val system: ActorSystem protected def uri: Uri protected def converter(str: String): Try[FeeUnit] def getFeeRate: Future[FeeUnit] = { HttpFeeRateProvider .makeApiCall(uri) .flatMap(ret => Future.fromTry(converter(ret)))(system.dispatcher) } } abstract class CachedHttpFeeRateProvider extends HttpFeeRateProvider { private var cachedFeeRateOpt: Option[(FeeUnit, Instant)] = None val cacheDuration: Duration = Duration.ofMinutes(5) private def updateFeeRate(): Future[FeeUnit] = { implicit val ec: ExecutionContextExecutor = system.dispatcher super.getFeeRate.map { feeRate => cachedFeeRateOpt = Some(feeRate, TimeUtil.now) feeRate } } override def getFeeRate: Future[FeeUnit] = { cachedFeeRateOpt match { case None => updateFeeRate() case Some((cachedFeeRate, time)) => val now = TimeUtil.now if (time.plus(cacheDuration).isAfter(now)) { updateFeeRate() } else { Future.successful(cachedFeeRate) } } } }
Example 79
Source File: RestServer.scala From introduction-to-akkahttp with Apache License 2.0 | 5 votes |
package com.shashank.akkahttp.project import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import org.apache.spark.sql.SparkSession class RestServer(implicit val system:ActorSystem, implicit val materializer:ActorMaterializer,implicit val sparkSession:SparkSession) extends RestService{ def startServer(address:String, port:Int) = { Http().bindAndHandle(route,address,port) } } object RestServer { def main(args: Array[String]) { implicit val actorSystem = ActorSystem("rest-server") implicit val materializer = ActorMaterializer() implicit val sparkSession:SparkSession = SparkSession.builder().master("local"). appName("Rest Server context").getOrCreate() val server = new RestServer() server.startServer("localhost",8080) println("running server at localhost 8080") } }
Example 80
Source File: RoutingDSL.scala From introduction-to-akkahttp with Apache License 2.0 | 5 votes |
package com.shashank.akkahttp.basic.routing import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server._ import akka.stream.{ActorMaterializer, Materializer} object RoutingDSL { def main(args: Array[String]) { implicit val sys = ActorSystem("IntroductionToAkkaHttp") implicit val mat:Materializer = ActorMaterializer() val route = path("welcome"){ get{ complete { "welcome to rest service" } } } ~ path("demo"){ get{ complete { "welcome to demonstration" } } } Http().bindAndHandle(route, "localhost", 8090) } }
Example 81
Source File: Failure.scala From introduction-to-akkahttp with Apache License 2.0 | 5 votes |
package com.shashank.akkahttp.basic.routing import akka.actor.ActorSystem import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.ExceptionHandler import akka.stream.{ActorMaterializer, Materializer} object Failure { def main(args: Array[String]) { implicit val sys = ActorSystem("IntroductionToAkkaHttp") implicit val mat:Materializer = ActorMaterializer() implicit def myExceptionHandler = ExceptionHandler { case _: ArithmeticException => complete(HttpResponse(StatusCodes.BadRequest, entity = "Bad numbers, bad result!!!")) case e: Throwable => { println(e.getMessage) println(e.getStackTraceString) complete(HttpResponse(StatusCodes.BadRequest, entity = e.getMessage)) } } val route = path("welcome"){ get{ complete { "welcome to rest service" } } } ~ path("demo"){ get { complete { 100/0 "welcome to demonstration" } } } Http().bindAndHandle(route, "localhost", 8090) } }
Example 82
Source File: Rejection.scala From introduction-to-akkahttp with Apache License 2.0 | 5 votes |
package com.shashank.akkahttp.basic.routing import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server._ import akka.stream.{ActorMaterializer, Materializer} object Rejection { def main(args: Array[String]) { implicit val sys = ActorSystem("IntroductionToAkkaHttp") implicit val mat:Materializer = ActorMaterializer() implicit def myRejectionHandler = RejectionHandler.newBuilder().handle{ case MissingCookieRejection(cookieName) => complete(HttpResponse(StatusCodes.BadRequest, entity = "No cookies, no service!!!")) }.handleNotFound { complete((StatusCodes.NotFound, "Not here!")) }.result() val route = path("welcome"){ get{ complete { "welcome to rest service" } } } ~ path("demo"){ get{ complete { "welcome to demonstration" } } } ~ path("wrong"){ reject{ ValidationRejection("Invalid path", None) } } Http().bindAndHandle(route, "localhost", 8090) } }
Example 83
Source File: ConnectionLevel.scala From introduction-to-akkahttp with Apache License 2.0 | 5 votes |
package com.shashank.akkahttp.basic.client import akka.actor.ActorSystem import akka.http.javadsl.settings.ClientConnectionSettings import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.stream.ActorMaterializer import akka.stream.scaladsl._ import scala.concurrent.{Await, Future} import scala.concurrent.duration._ object ConnectionLevel { def main(args: Array[String]) { implicit val sys = ActorSystem("IntroductionToAkkaHttp") implicit val mat = ActorMaterializer() val connectionFlow = Http().outgoingConnection("localhost", 8090) val responseFuture = Source.single(HttpRequest(uri = "/welcome")) .via(connectionFlow) .runWith(Sink.head) val response = Await.result(responseFuture, 10 seconds) response.entity.dataBytes.map(_.utf8String).runForeach(println) sys.terminate() } }
Example 84
Source File: ReverseProxy.scala From introduction-to-akkahttp with Apache License 2.0 | 5 votes |
package com.shashank.akkahttp.basic.serving import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.model.headers.{Host, `Access-Control-Allow-Origin`} import akka.stream.scaladsl.Flow import akka.stream.{ActorMaterializer, Materializer} object ReverseProxy { def main(args: Array[String]) { implicit val sys = ActorSystem("IntroductionToAkkaHttp") implicit val mat:Materializer = ActorMaterializer() val redirectHost = "localhost" val redirectPort = 8090 val requestFlow = Flow.fromFunction[HttpRequest, HttpRequest]( request => { request .withUri(request.uri.withAuthority(redirectHost, redirectPort)) .mapHeaders(headers => headers.filterNot(_.lowercaseName() == Host.lowercaseName)) .addHeader(Host(redirectHost, redirectPort)) }) val outgoingConnection = Http().outgoingConnection(redirectHost, redirectPort) val responseFlow = Flow.fromFunction[HttpResponse, HttpResponse]( response => { response.withHeaders(`Access-Control-Allow-Origin`.*) }) Http().bindAndHandle(requestFlow via outgoingConnection via responseFlow, "localhost", 8080) } }
Example 85
Source File: EncodingDecodingClientApplication.scala From Akka-Cookbook with MIT License | 5 votes |
package com.packt.chapter9 import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.coding.{Encoder, Gzip, NoCoding} import akka.http.scaladsl.model._ import akka.http.scaladsl.model.headers._ import akka.http.scaladsl.model.headers.HttpEncodings._ import akka.http.scaladsl.model.HttpMethods._ import headers.HttpEncodings import akka.stream.ActorMaterializer import akka.util.ByteString import scala.concurrent.duration._ import scala.concurrent.Future import scala.util.{Failure, Success} object EncodingDecodingClientApplication extends App { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() import system.dispatcher val http = Http() val uriServer = "http://localhost:8088/" val requests = Seq ( HttpRequest(POST, uriServer, List(`Accept-Encoding`(gzip)), HttpEntity("Hello!")), HttpRequest(POST, uriServer, List(`Content-Encoding`(gzip), `Accept-Encoding`(gzip)), HttpEntity(compress("Hello compressed!", Gzip)) ) ) Future.traverse(requests)(http.singleRequest(_).map(decodeResponse)) andThen { case Success(responses) => responses.foreach(response => response.entity.toStrict(5 seconds).map(_.data.decodeString("UTF-8")).andThen { case Success(content) => println(s"Response: $content") case _ => }) case Failure(e) => println(s"request failed $e") } private def decodeResponse(response: HttpResponse) = { val decoder = response.encoding match { case HttpEncodings.gzip => Gzip case HttpEncodings.identity => NoCoding } decoder.decode(response) } private def compress(input: String, encoder: Encoder): ByteString = encoder.encode(ByteString(input)) }
Example 86
Source File: UploadingFileClient.scala From Akka-Cookbook with MIT License | 5 votes |
package com.packt.chapter9 import java.nio.file.Paths import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpMethods._ import akka.http.scaladsl.model._ import akka.stream.ActorMaterializer import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.{Failure, Success} object UploadingFileClient extends App { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() import system.dispatcher val http = Http() val entity = Multipart.FormData.fromPath( "file", ContentTypes.`text/plain(UTF-8)`, Paths.get("./src/main/resources/testfile.txt") ).toEntity() val uris = Seq( "http://localhost:8088/regularupload", "http://localhost:8088/streamupload" ) val requests = uris.map(uri => HttpRequest(POST, uri, Nil, entity)) Future.traverse(requests)(http.singleRequest(_)) andThen { case Success(responses) => responses.foreach(response => response.entity.toStrict(5 seconds).map(_.data.utf8String).andThen { case Success(content) => println(s"Response: $content") case _ => }) case Failure(e) => println(s"request failed $e") } }
Example 87
Source File: RequestLevelClientAPIApplication.scala From Akka-Cookbook with MIT License | 5 votes |
package com.packt.chapter9 import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpRequest import akka.stream.ActorMaterializer import scala.concurrent.duration._ import scala.util.Success object RequestLevelClientAPIApplication extends App { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher val akkaToolkitRequest = HttpRequest(uri = "https://api.github.com/repos/akka/akka-http") val responseFuture = Http().singleRequest(akkaToolkitRequest) responseFuture.andThen { case Success(response) => response.entity.toStrict(5 seconds).map(_.data.decodeString("UTF-8")).andThen { case Success(json) => val pattern = """.*"open_issues":(.*?),.*""".r pattern.findAllIn(json).matchData foreach { m => println(s"There are ${m.group(1)} open issues in Akka Http.") materializer.shutdown() system.terminate() } case _ => } case _ => println(s"request failed") } }
Example 88
Source File: HostLevelClientAPIApplication.scala From Akka-Cookbook with MIT License | 5 votes |
package com.packt.chapter9 import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpRequest import akka.stream.ActorMaterializer import akka.stream.scaladsl.{Sink, Source} import scala.concurrent.duration._ import scala.util.{Failure, Success} object HostLevelClientAPIApplication extends App { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher val poolClientFlow = Http().cachedHostConnectionPoolHttps[String]("api.github.com") val akkaToolkitRequest = HttpRequest(uri = "/repos/akka/akka-http") -> """.*"open_issues":(.*?),.*""" val responseFuture = Source.single(akkaToolkitRequest).via(poolClientFlow).runWith(Sink.head) responseFuture.andThen { case Success(result) => val (tryResponse, regex) = result tryResponse match { case Success(response) => response.entity.toStrict(5 seconds).map(_.data.decodeString("UTF-8")).andThen { case Success(json) => val pattern = regex.r pattern.findAllIn(json).matchData foreach { m => println(s"There are ${m.group(1)} open issues in Akka Http.") materializer.shutdown() system.terminate() } case _ => } case _ => println("request failed") } case _ => println("request failed") } }
Example 89
Source File: ConnectionLevelClientAPIApplication.scala From Akka-Cookbook with MIT License | 5 votes |
package com.packt.chapter9 import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpRequest import akka.stream.ActorMaterializer import akka.stream.scaladsl.{Sink, Source} import scala.util.{Failure, Success} import scala.concurrent.duration._ object ConnectionLevelClientAPIApplication extends App { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher val connectionFlow = Http().outgoingConnectionHttps("api.github.com") val akkaToolkitRequest = HttpRequest(uri = "/repos/akka/akka-http") val responseFuture = Source.single(akkaToolkitRequest).via(connectionFlow).runWith(Sink.head) responseFuture.andThen { case Success(response) => response.entity.toStrict(5 seconds).map(_.data.decodeString("UTF-8")).andThen { case Success(json) => val pattern = """.*"open_issues":(.*?),.*""".r pattern.findAllIn(json).matchData foreach { m => println(s"There are ${m.group(1)} open issues in Akka Http.") materializer.shutdown() system.terminate() } case _ => } case _ => println("request failed") } }
Example 90
Source File: SetSessionScala.scala From akka-http-session with Apache License 2.0 | 5 votes |
package com.softwaremill.example.session import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import com.softwaremill.session.CsrfDirectives._ import com.softwaremill.session.CsrfOptions._ import com.softwaremill.session.SessionDirectives._ import com.softwaremill.session.SessionOptions._ import com.softwaremill.session._ import com.typesafe.scalalogging.StrictLogging import scala.io.StdIn object SetSessionScala extends App with StrictLogging { implicit val system = ActorSystem("example") implicit val materializer = ActorMaterializer() import system.dispatcher val sessionConfig = SessionConfig.default( "c05ll3lesrinf39t7mc5h6un6r0c69lgfno69dsak3vabeqamouq4328cuaekros401ajdpkh60rrtpd8ro24rbuqmgtnd1ebag6ljnb65i8a55d482ok7o0nch0bfbe") implicit val sessionManager = new SessionManager[MyScalaSession](sessionConfig) implicit val refreshTokenStorage = new InMemoryRefreshTokenStorage[MyScalaSession] { def log(msg: String) = logger.info(msg) } def mySetSession(v: MyScalaSession) = setSession(refreshable, usingCookies, v) val myRequiredSession = requiredSession(refreshable, usingCookies) val myInvalidateSession = invalidateSession(refreshable, usingCookies) val routes = randomTokenCsrfProtection(checkHeader) { pathPrefix("api") { path("do_login") { post { entity(as[String]) { body => logger.info(s"Logging in $body") mySetSession(MyScalaSession(body)) { setNewCsrfToken(checkHeader) { ctx => ctx.complete("ok") } } } } } } } val bindingFuture = Http().bindAndHandle(routes, "localhost", 8080) println("Server started, press enter to stop. Visit http://localhost:8080 to see the demo.") StdIn.readLine() import system.dispatcher bindingFuture .flatMap(_.unbind()) .onComplete { _ => system.terminate() println("Server stopped") } }
Example 91
Source File: SessionInvalidationScala.scala From akka-http-session with Apache License 2.0 | 5 votes |
package com.softwaremill.example.session import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import com.softwaremill.session.SessionDirectives._ import com.softwaremill.session.SessionOptions._ import com.softwaremill.session._ import com.typesafe.scalalogging.StrictLogging import scala.io.StdIn object SessionInvalidationScala extends App with StrictLogging { implicit val system = ActorSystem("example") implicit val materializer = ActorMaterializer() import system.dispatcher val sessionConfig = SessionConfig.default( "c05ll3lesrinf39t7mc5h6un6r0c69lgfno69dsak3vabeqamouq4328cuaekros401ajdpkh60rrtpd8ro24rbuqmgtnd1ebag6ljnb65i8a55d482ok7o0nch0bfbe") implicit val sessionManager = new SessionManager[MyScalaSession](sessionConfig) implicit val refreshTokenStorage = new InMemoryRefreshTokenStorage[MyScalaSession] { def log(msg: String) = logger.info(msg) } def mySetSession(v: MyScalaSession) = setSession(refreshable, usingCookies, v) val myRequiredSession = requiredSession(refreshable, usingCookies) val myInvalidateSession = invalidateSession(refreshable, usingCookies) val routes = path("logout") { post { myRequiredSession { session => myInvalidateSession { ctx => logger.info(s"Logging out $session") ctx.complete("ok") } } } } val bindingFuture = Http().bindAndHandle(routes, "localhost", 8080) println("Server started, press enter to stop. Visit http://localhost:8080 to see the demo.") StdIn.readLine() import system.dispatcher bindingFuture .flatMap(_.unbind()) .onComplete { _ => system.terminate() println("Server stopped") } }
Example 92
Source File: ScalaExample.scala From akka-http-session with Apache License 2.0 | 5 votes |
package com.softwaremill.example import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import com.softwaremill.example.session.MyScalaSession import com.softwaremill.session.CsrfDirectives._ import com.softwaremill.session.CsrfOptions._ import com.softwaremill.session.SessionDirectives._ import com.softwaremill.session.SessionOptions._ import com.softwaremill.session._ import com.typesafe.scalalogging.StrictLogging import scala.io.StdIn object Example extends App with StrictLogging { implicit val system = ActorSystem("example") implicit val materializer = ActorMaterializer() import system.dispatcher val sessionConfig = SessionConfig.default( "c05ll3lesrinf39t7mc5h6un6r0c69lgfno69dsak3vabeqamouq4328cuaekros401ajdpkh60rrtpd8ro24rbuqmgtnd1ebag6ljnb65i8a55d482ok7o0nch0bfbe") implicit val sessionManager = new SessionManager[MyScalaSession](sessionConfig) implicit val refreshTokenStorage = new InMemoryRefreshTokenStorage[MyScalaSession] { def log(msg: String) = logger.info(msg) } def mySetSession(v: MyScalaSession) = setSession(refreshable, usingCookies, v) val myRequiredSession = requiredSession(refreshable, usingCookies) val myInvalidateSession = invalidateSession(refreshable, usingCookies) val routes = path("") { redirect("/site/index.html", Found) } ~ randomTokenCsrfProtection(checkHeader) { pathPrefix("api") { path("do_login") { post { entity(as[String]) { body => logger.info(s"Logging in $body") mySetSession(MyScalaSession(body)) { setNewCsrfToken(checkHeader) { ctx => ctx.complete("ok") } } } } } ~ // This should be protected and accessible only when logged in path("do_logout") { post { myRequiredSession { session => myInvalidateSession { ctx => logger.info(s"Logging out $session") ctx.complete("ok") } } } } ~ // This should be protected and accessible only when logged in path("current_login") { get { myRequiredSession { session => ctx => logger.info("Current session: " + session) ctx.complete(session.username) } } } } ~ pathPrefix("site") { getFromResourceDirectory("") } } val bindingFuture = Http().bindAndHandle(routes, "localhost", 8080) println("Server started, press enter to stop. Visit http://localhost:8080 to see the demo.") StdIn.readLine() import system.dispatcher bindingFuture .flatMap(_.unbind()) .onComplete { _ => system.terminate() println("Server stopped") } }
Example 93
Source File: QrCodesBot.scala From telegram with Apache License 2.0 | 5 votes |
import java.net.URLEncoder import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, Uri} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.util.ByteString import com.bot4s.telegram.api.declarative.Commands import com.bot4s.telegram.api._ import com.bot4s.telegram.future.Polling import com.bot4s.telegram.methods._ import com.bot4s.telegram.models.AkkaInputFile import scala.concurrent.Future class QrCodesBot(token: String) extends AkkaExampleBot(token) with Polling with Commands[Future] with ChatActions[Future] { // Multiple variants onCommand('qr | 'qrcode | 'qr_code) { implicit msg => withArgs { args => val url = "https://api.qrserver.com/v1/create-qr-code/?data=" + URLEncoder.encode(args mkString " ", "UTF-8") for { response <- Http().singleRequest(HttpRequest(uri = Uri(url))) if response.status.isSuccess() bytes <- Unmarshal(response).to[ByteString] photo = AkkaInputFile("qrcode.png", bytes) _ <- uploadingPhoto // Hint the user _ <- request(SendPhoto(msg.source, photo)) } yield () } } }
Example 94
Source File: VoiceFileBot.scala From telegram with Apache License 2.0 | 5 votes |
import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, Uri} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.util.ByteString import cats.instances.future._ import cats.syntax.functor._ import com.bot4s.telegram.api.declarative.Commands import com.bot4s.telegram.future.Polling import com.bot4s.telegram.methods._ import scala.concurrent.Future import scala.util.{Failure, Success} class VoiceFileBot(token: String) extends AkkaExampleBot(token) with Polling with Commands[Future] { onMessage { implicit msg => using(_.voice) { voice => request(GetFile(voice.fileId)).andThen({ case Success(file) => file.filePath match { case Some(filePath) => // See https://core.telegram.org/bots/api#getfile val url = s"https://api.telegram.org/file/bot${token}/${filePath}" for { res <- Http().singleRequest(HttpRequest(uri = Uri(url))) if res.status.isSuccess() bytes <- Unmarshal(res).to[ByteString] _ <- reply(s"File with ${bytes.size} bytes received.") } yield () case None => reply("No file_path was returned") } case Failure(e) => logger.error("Exception: " + e) // poor's man logging }).void } } }
Example 95
Source File: WebhookBot.scala From telegram with Apache License 2.0 | 5 votes |
import java.net.URLEncoder import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, Uri} import akka.http.scaladsl.unmarshalling.Unmarshal import com.bot4s.telegram.api.Webhook import com.bot4s.telegram.methods._ import com.bot4s.telegram.models.Message import scala.concurrent.Future class WebhookBot(token: String) extends AkkaExampleBot(token) with Webhook { val port = 8080 val webhookUrl = "https://88c444ab.ngrok.io" val baseUrl = "http://api.mathjs.org/v1/?expr=" override def receiveMessage(msg: Message): Future[Unit] = { msg.text.fold(Future.successful(())) { text => val url = baseUrl + URLEncoder.encode(text, "UTF-8") for { res <- Http().singleRequest(HttpRequest(uri = Uri(url))) if res.status.isSuccess() result <- Unmarshal(res).to[String] _ <- request(SendMessage(msg.source, result)) } yield () } } }
Example 96
Source File: WebRoutes.scala From telegram with Apache License 2.0 | 5 votes |
package com.bot4s.telegram.api import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import com.bot4s.telegram.future.BotExecutionContext import slogging.StrictLogging import scala.concurrent.{Future, Promise} trait WebRoutes extends BotBase[Future] with StrictLogging { _: BotExecutionContext with AkkaImplicits => val port: Int val interfaceIp: String = "::0" def routes: Route = reject private var bindingFuture: Future[Http.ServerBinding] = _ @volatile private var eol: Promise[Unit] = _ abstract override def run(): Future[Unit] = synchronized { if (eol != null) { throw new RuntimeException("Bot is already running") } bindingFuture = Http().bindAndHandle(routes, interfaceIp, port) bindingFuture.foreach { _ => logger.info(s"Listening on $interfaceIp:$port") } sys.addShutdownHook { shutdown() } eol = Promise[Unit]() val t = Future.sequence(Seq(eol.future, super.run())) t.map(_ => ()) } abstract override def shutdown(): Unit = synchronized { if (eol == null) { throw new RuntimeException("Bot is not running") } super.shutdown() for { b <- bindingFuture _ <- b.unbind() t <- system.terminate() } { eol.success(()) eol = null } } }
Example 97
Source File: YetAnotherAkkaClient.scala From telegram with Apache License 2.0 | 5 votes |
package com.bot4s.telegram.clients import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshalling.Marshal import akka.http.scaladsl.model.Uri.Path import akka.http.scaladsl.model._ import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.Materializer import akka.stream.scaladsl.{Sink, Source} import cats.instances.future._ import com.bot4s.telegram.api.RequestHandler import com.bot4s.telegram.methods.{Request, Response} import io.circe.{Decoder, Encoder} import slogging.StrictLogging import com.bot4s.telegram.marshalling.responseDecoder import scala.concurrent.{ExecutionContext, Future} class YetAnotherAkkaClient(token: String, telegramHost: String = "api.telegram.org") (implicit system: ActorSystem, materializer: Materializer, ec: ExecutionContext) extends RequestHandler[Future] with StrictLogging { private val flow = Http().outgoingConnectionHttps(telegramHost) import com.bot4s.telegram.marshalling.AkkaHttpMarshalling._ override def sendRequest[R, T <: Request[_]](request: T)(implicit encT: Encoder[T], decR: Decoder[R]): Future[R] = { Source.fromFuture( Marshal(request).to[RequestEntity] .map { re => HttpRequest(HttpMethods.POST, Uri(path = Path(s"/bot$token/" + request.methodName)), entity = re) }) .via(flow) .mapAsync(1)(r => Unmarshal(r.entity).to[Response[R]]) .runWith(Sink.head) .map(processApiResponse[R]) } }
Example 98
Source File: AkkaHttpClient.scala From telegram with Apache License 2.0 | 5 votes |
package com.bot4s.telegram.clients import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshalling._ import akka.http.scaladsl.model._ import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.Materializer import cats.instances.future._ import com.bot4s.telegram.api.RequestHandler import com.bot4s.telegram.marshalling.AkkaHttpMarshalling import com.bot4s.telegram.marshalling._ import com.bot4s.telegram.methods.{Request, Response} import io.circe.{Decoder, Encoder} import slogging.StrictLogging import scala.concurrent.{ExecutionContext, Future} class AkkaHttpClient(token: String, telegramHost: String = "api.telegram.org") (implicit system: ActorSystem, materializer: Materializer, ec: ExecutionContext) extends RequestHandler[Future] with StrictLogging { import AkkaHttpMarshalling._ private val apiBaseUrl = s"https://$telegramHost/bot$token/" private val http = Http() override def sendRequest[R, T <: Request[_]](request: T)(implicit encT: Encoder[T], decR: Decoder[R]): Future[R] = { Marshal(request).to[RequestEntity] .map { re => HttpRequest(HttpMethods.POST, Uri(apiBaseUrl + request.methodName), entity = re) } .flatMap(http.singleRequest(_)) .flatMap(r => Unmarshal(r.entity).to[Response[R]]) .map(t => processApiResponse[R](t)) } }
Example 99
Source File: Demo.scala From chordial with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.tristanpenman.chordial.demo import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.ws.TextMessage import akka.stream.scaladsl._ import akka.stream.{ActorAttributes, ActorMaterializer, OverflowStrategy, Supervision} import akka.util.Timeout import com.tristanpenman.chordial.core.Event import com.tristanpenman.chordial.core.Event._ import scala.concurrent.Await import scala.concurrent.duration._ object Demo extends App { implicit val system = ActorSystem("chordial-demo") implicit val mat = ActorMaterializer() implicit val ec = system.dispatcher implicit val timeout: Timeout = 3.seconds // Generate IDs ranging from 0 to 63 (inclusive) so that when visualising the network, // each node represents a ~5.625 degree arc on the ring private val keyspaceBits = 6 // Create an actor that is responsible for creating and terminating nodes, while ensuring // that nodes are assigned unique IDs in the Chord key-space private val governor = system.actorOf(Governor.props(keyspaceBits), "Governor") // Create an actor that will log events published by nodes private val eventWriter = system.actorOf(EventWriter.props, "EventWriter") // Subscribe the EventWriter actor to events published by nodes system.eventStream.subscribe(eventWriter, classOf[Event]) val (listener, eventsSource) = Source .actorRef[Event](Int.MaxValue, OverflowStrategy.fail) .map { case FingerReset(nodeId: Long, index: Int) => s"""{ "type": "FingerReset", "nodeId": $nodeId, "index": $index }""" case FingerUpdated(nodeId: Long, index: Int, fingerId: Long) => s"""{ "type": "FingerUpdated", "nodeId": $nodeId, "index": $index, "fingerId": $fingerId }""" case NodeCreated(nodeId, successorId) => s"""{ "type": "NodeCreated", "nodeId": $nodeId, "successorId": $successorId }""" case NodeShuttingDown(nodeId) => s"""{ "type": "NodeDeleted", "nodeId": $nodeId }""" case PredecessorReset(nodeId) => s"""{ "type": "PredecessorReset", "nodeId": $nodeId }""" case PredecessorUpdated(nodeId, predecessorId) => s"""{ "type": "PredecessorUpdated", "nodeId": $nodeId, "predecessorId": $predecessorId }""" case SuccessorListUpdated(nodeId, primarySuccessorId, _) => s"""{ "type": "SuccessorUpdated", "nodeId": $nodeId, "successorId": $primarySuccessorId }""" } .map(TextMessage(_)) .withAttributes(ActorAttributes.supervisionStrategy(Supervision.resumingDecider)) .toMat(BroadcastHub.sink[TextMessage](bufferSize = 16))(Keep.both) .run() system.eventStream.subscribe(listener, classOf[Event]) Http().bindAndHandle(WebSocketWorker(governor, eventsSource), "0.0.0.0", 4567) Await.result(system.whenTerminated, Duration.Inf) }
Example 100
Source File: Launcher.scala From udash-demos with GNU General Public License v3.0 | 5 votes |
package io.udash.demos.rest import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import io.udash.demos.rest.api.PhoneBookWebService import io.udash.logging.CrossLogging import scala.io.StdIn object Launcher extends CrossLogging { def main(args: Array[String]): Unit = { implicit val system = ActorSystem("my-system") implicit val materializer = ActorMaterializer() // needed for the future flatMap/onComplete in the end implicit val executionContext = system.dispatcher val service = new PhoneBookWebService val bindingFuture = Http().bindAndHandle(service.route, "localhost", 8080) logger.info(s"Server online at http://localhost:8080/\nPress Enter to stop...") StdIn.readLine() // let it run until user presses return bindingFuture .flatMap(_.unbind()) // trigger unbinding from the port .onComplete(_ => system.terminate()) // and shutdown when done } }
Example 101
Source File: LoadTest.scala From ws_to_kafka with MIT License | 5 votes |
package com.pkinsky import java.util.concurrent.atomic.AtomicInteger import akka.http.scaladsl.model.ws.{InvalidUpgradeResponse, WebsocketUpgradeResponse, WebsocketRequest, TextMessage} import akka.http.scaladsl.Http import akka.http.scaladsl.model.Uri import akka.stream.ThrottleMode import akka.stream.scaladsl.{Keep, Sink, RunnableGraph, Source} import play.api.libs.json.Json import scala.concurrent.{Future, Await} import scala.concurrent.duration._ import scala.language.postfixOps object LoadTest extends App with AppContext { val clients = 256 val eventsPerClient = 256 val eventsSent = new AtomicInteger(0) def testData(clientId: String): Source[Event, Unit] = Source.unfoldInf(1) { n => val event = Event(s"msg number $n", clientId, System.currentTimeMillis()) (n + 1, event) }.take(eventsPerClient).throttle(1, 100 millis, 1, ThrottleMode.Shaping) def wsClient(clientId: String): RunnableGraph[Future[WebsocketUpgradeResponse]] = testData(clientId).map(e => TextMessage.Strict(Json.toJson(e).toString)) .map { x => eventsSent.incrementAndGet(); x } .viaMat(Http().websocketClientFlow(WebsocketRequest(Uri(s"ws://localhost:$port/ws"))))(Keep.right).to(Sink.ignore) //set up websocket connections (1 to clients).foreach { id => wsClient(s"client $id").run() } //watch kafka for messages sent via websocket val kafkaConsumerGraph: RunnableGraph[Future[Seq[Event]]] = kafka.consume[Event](eventTopic, "group_new") .take(clients * eventsPerClient).takeWithin(2 minutes) .toMat(Sink.seq)(Keep.right) val res = Await.result(kafkaConsumerGraph.run, 5 minutes) println(s"sent ${eventsSent.get()} events total") println(s"res size: ${res.length}") }
Example 102
Source File: StreamingUpload.scala From ws_to_kafka with MIT License | 5 votes |
package com.pkinsky import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpResponse, HttpRequest} import akka.http.scaladsl.model.ws.{TextMessage, Message} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.PathMatchers.PathEnd import akka.stream._ import akka.stream.scaladsl._ import com.softwaremill.react.kafka.ReactiveKafka import play.api.libs.json.Json import scala.concurrent.{Future, ExecutionContext} import scala.concurrent.duration._ import scala.util.Success import scala.language.postfixOps object StreamingUpload extends App with AppContext { val kafkaPublisherGraph: RunnableGraph[SourceQueue[Event]] = Source.queue[Event](1024, OverflowStrategy.backpressure).to(kafka.publish[Event](eventTopic)) val sourceQueue: SourceQueue[Event] = kafkaPublisherGraph.run val queueWriter: Sink[Event, Unit] = Flow[Event].mapAsync(1){ elem => sourceQueue.offer(elem) .andThen{ case Success(false) => println(s"failed to publish $elem to topic $eventTopic") case Success(true) => println(s"published $elem to topic $eventTopic") } }.to(Sink.ignore) val parseMessages: Flow[Message, Event, Unit] = Flow[Message] .collect{ case TextMessage.Strict(t) => val js = Json.parse(t) Json.fromJson[Event](js).get } val wsHandlerFlow: Flow[Message, Message, Unit] = Flow.fromSinkAndSource( sink = parseMessages.to(queueWriter), source = Source.maybe ) val routes: Flow[HttpRequest, HttpResponse, Unit] = get { path(PathEnd) { getFromResource("test.html") } ~ path("ws") { println("ws connection accepted") handleWebsocketMessages(wsHandlerFlow) } } Http().bindAndHandle(routes, "localhost", port).andThen{ case util.Failure(t) => println(s"error binding to localhost: $t")} println(s"listening on port $port, press ENTER to stop") awaitTermination() } object KafkaListener extends App with AppContext { val graph = kafka.consume[Event](eventTopic, "kafka_listener").toMat(Sink.foreach(println))(Keep.right) graph.run.onComplete(println) println(s"listening to Kafka topic $eventTopic, press ENTER to stop") awaitTermination() }
Example 103
Source File: DemoApp.scala From akka-management with Apache License 2.0 | 5 votes |
package akka.cluster.bootstrap import akka.actor.ActorSystem import akka.cluster.{ Cluster, MemberStatus } import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.management.scaladsl.AkkaManagement import akka.management.cluster.bootstrap.ClusterBootstrap import akka.stream.ActorMaterializer object DemoApp extends App { implicit val system = ActorSystem("my-system") implicit val materializer = ActorMaterializer() val cluster = Cluster(system) def isReady() = { val selfNow = cluster.selfMember selfNow.status == MemberStatus.Up } def isHealthy() = { isReady() } val route = concat( path("ping")(complete("pong!")), path("healthy")(complete(if (isHealthy()) StatusCodes.OK else StatusCodes.ServiceUnavailable)), path("ready")(complete(if (isReady()) StatusCodes.OK else StatusCodes.ServiceUnavailable))) AkkaManagement(system).start() ClusterBootstrap(system).start() Http().bindAndHandle( route, sys.env.get("HOST").getOrElse("127.0.0.1"), sys.env.get("PORT_HTTP").map(_.toInt).getOrElse(8080)) }
Example 104
Source File: DemoApp.scala From akka-management with Apache License 2.0 | 5 votes |
package akka.cluster.bootstrap import akka.actor.{ Actor, ActorLogging, ActorSystem, Props } import akka.cluster.ClusterEvent.ClusterDomainEvent import akka.cluster.{ Cluster, ClusterEvent } import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.management.cluster.bootstrap.ClusterBootstrap //#start-akka-management import akka.management.scaladsl.AkkaManagement //#start-akka-management import akka.stream.ActorMaterializer object DemoApp extends App { implicit val system = ActorSystem("Appka") import system.log implicit val mat = ActorMaterializer() val cluster = Cluster(system) log.info(s"Started [$system], cluster.selfAddress = ${cluster.selfAddress}") //#start-akka-management AkkaManagement(system).start() //#start-akka-management ClusterBootstrap(system).start() cluster.subscribe( system.actorOf(Props[ClusterWatcher]), ClusterEvent.InitialStateAsEvents, classOf[ClusterDomainEvent] ) // add real app routes here val routes = path("hello") { get { complete( HttpEntity( ContentTypes.`text/html(UTF-8)`, "<h1>Hello</h1>" ) ) } } Http().bindAndHandle(routes, "0.0.0.0", 8080) Cluster(system).registerOnMemberUp({ log.info("Cluster member is up!") }) } class ClusterWatcher extends Actor with ActorLogging { val cluster = Cluster(context.system) override def receive = { case msg => log.info(s"Cluster ${cluster.selfAddress} >>> " + msg) } }
Example 105
Source File: MarathonApiDockerDemoApp.scala From akka-management with Apache License 2.0 | 5 votes |
package akka.cluster.bootstrap import akka.actor.ActorSystem import akka.cluster.{ Cluster, MemberStatus } import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.management.cluster.bootstrap.ClusterBootstrap import akka.management.scaladsl.AkkaManagement import akka.stream.ActorMaterializer object MarathonApiDockerDemoApp extends App { implicit val system = ActorSystem("my-system") implicit val materializer = ActorMaterializer() val cluster = Cluster(system) def isReady() = { val selfNow = cluster.selfMember selfNow.status == MemberStatus.Up } def isHealthy() = { isReady() } val route = concat( path("ping")(complete("pong!")), path("healthy")(complete(if (isHealthy()) StatusCodes.OK else StatusCodes.ServiceUnavailable)), path("ready")(complete(if (isReady()) StatusCodes.OK else StatusCodes.ServiceUnavailable)) ) AkkaManagement(system).start() ClusterBootstrap(system).start() Http().bindAndHandle( route, sys.env.get("HOST").getOrElse("127.0.0.1"), sys.env.get("PORT_HTTP").map(_.toInt).getOrElse(8080)) }
Example 106
Source File: ClusterApp.scala From akka-management with Apache License 2.0 | 5 votes |
package akka.cluster.bootstrap import akka.actor.{ Actor, ActorLogging, ActorSystem, PoisonPill, Props } import akka.cluster.ClusterEvent.ClusterDomainEvent import akka.cluster.singleton.{ ClusterSingletonManager, ClusterSingletonManagerSettings } import akka.cluster.{ Cluster, ClusterEvent } import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.management.cluster.bootstrap.ClusterBootstrap import akka.management.scaladsl.AkkaManagement import akka.stream.ActorMaterializer object ClusterApp { def main(args: Array[String]): Unit = { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher val cluster = Cluster(system) system.log.info("Starting Akka Management") AkkaManagement(system).start() ClusterBootstrap(system).start() system.actorOf( ClusterSingletonManager.props( Props[NoisySingleton], PoisonPill, ClusterSingletonManagerSettings(system) ) ) Cluster(system).subscribe( system.actorOf(Props[ClusterWatcher]), ClusterEvent.InitialStateAsEvents, classOf[ClusterDomainEvent] ) // add real app routes here val routes = path("hello") { get { complete( HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Hello</h1>") ) } } Http().bindAndHandle(routes, "0.0.0.0", 8080) system.log.info( s"Server online at http://localhost:8080/\nPress RETURN to stop..." ) cluster.registerOnMemberUp(() => { system.log.info("Cluster member is up!") }) } class ClusterWatcher extends Actor with ActorLogging { val cluster = Cluster(context.system) override def receive = { case msg => log.info(s"Cluster ${cluster.selfAddress} >>> " + msg) } } }
Example 107
Source File: DemoApp.scala From akka-management with Apache License 2.0 | 5 votes |
package akka.cluster.bootstrap import akka.actor.{ Actor, ActorLogging, ActorSystem, Props } import akka.cluster.ClusterEvent.ClusterDomainEvent import akka.cluster.{ Cluster, ClusterEvent } import akka.http.scaladsl.Http import akka.management.scaladsl.AkkaManagement import akka.management.cluster.bootstrap.ClusterBootstrap import akka.stream.ActorMaterializer import akka.stream.scaladsl.Sink import akka.stream.scaladsl.Source import com.typesafe.config.ConfigFactory object DemoApp extends App { implicit val system = ActorSystem("simple") import system.log import system.dispatcher implicit val mat = ActorMaterializer() val cluster = Cluster(system) log.info("Started [{}], cluster.selfAddress = {}", system, cluster.selfAddress) AkkaManagement(system).start() ClusterBootstrap(system).start() cluster .subscribe(system.actorOf(Props[ClusterWatcher]), ClusterEvent.InitialStateAsEvents, classOf[ClusterDomainEvent]) import akka.http.scaladsl.server.Directives._ Http().bindAndHandle(complete("Hello world"), "0.0.0.0", 8080) } class ClusterWatcher extends Actor with ActorLogging { val cluster = Cluster(context.system) override def receive = { case msg => log.info("Cluster {} >>> {}", msg, cluster.selfAddress) } }
Example 108
Source File: MultiDcSpec.scala From akka-management with Apache License 2.0 | 5 votes |
package akka.management.cluster import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{ HttpRequest, StatusCodes } import akka.http.scaladsl.unmarshalling.Unmarshal import akka.management.scaladsl.ManagementRouteProviderSettings import akka.stream.ActorMaterializer import akka.testkit.SocketUtil import com.typesafe.config.ConfigFactory import org.scalatest.{ Matchers, WordSpec } import org.scalatest.concurrent.{ Eventually, ScalaFutures } import org.scalatest.time.{ Millis, Seconds, Span } class MultiDcSpec extends WordSpec with Matchers with ScalaFutures with ClusterHttpManagementJsonProtocol with Eventually { implicit val patience: PatienceConfig = PatienceConfig(timeout = Span(10, Seconds), interval = Span(50, Millis)) val config = ConfigFactory.parseString( """ |akka.actor.provider = "cluster" |akka.remote.log-remote-lifecycle-events = off |akka.remote.netty.tcp.hostname = "127.0.0.1" |#akka.loglevel = DEBUG """.stripMargin ) "Http cluster management" must { "allow multiple DCs" in { val Vector(httpPortA, portA, portB) = SocketUtil.temporaryServerAddresses(3, "127.0.0.1").map(_.getPort) val dcA = ConfigFactory.parseString( s""" |akka.management.http.hostname = "127.0.0.1" |akka.management.http.port = $httpPortA |akka.cluster.seed-nodes = ["akka.tcp://[email protected]:$portA"] |akka.cluster.multi-data-center.self-data-center = "DC-A" |akka.remote.netty.tcp.port = $portA """.stripMargin ) val dcB = ConfigFactory.parseString( s""" |akka.cluster.seed-nodes = ["akka.tcp://[email protected]:$portA"] |akka.cluster.multi-data-center.self-data-center = "DC-B" |akka.remote.netty.tcp.port = $portB """.stripMargin ) implicit val dcASystem = ActorSystem("MultiDcSystem", config.withFallback(dcA)) val dcBSystem = ActorSystem("MultiDcSystem", config.withFallback(dcB)) implicit val materializer = ActorMaterializer() val routeSettings = ManagementRouteProviderSettings(selfBaseUri = s"http://127.0.0.1:$httpPortA", readOnly = false) try { Http() .bindAndHandle(ClusterHttpManagementRouteProvider(dcASystem).routes(routeSettings), "127.0.0.1", httpPortA) .futureValue eventually { val response = Http().singleRequest(HttpRequest(uri = s"http://127.0.0.1:$httpPortA/cluster/members")).futureValue response.status should equal(StatusCodes.OK) val members = Unmarshal(response.entity).to[ClusterMembers].futureValue members.members.size should equal(2) members.members.map(_.status) should equal(Set("Up")) } } finally { dcASystem.terminate() dcBSystem.terminate() } } } }
Example 109
Source File: InfluxAkkaClient.scala From chronicler with Apache License 2.0 | 5 votes |
package com.github.fsanaulla.chronicler.akka.shared import akka.actor.ActorSystem import akka.http.scaladsl.{Http, HttpExt, HttpsConnectionContext} import scala.concurrent.duration.Duration import scala.concurrent.{Await, ExecutionContext, Future} abstract class InfluxAkkaClient( terminateActorSystem: Boolean, httpsContext: Option[HttpsConnectionContext] )(implicit system: ActorSystem, ec: ExecutionContext) { self: AutoCloseable => private[akka] implicit val http: HttpExt = Http() private[akka] val (ctx, schema) = httpsContext .map(_ -> "https") .getOrElse(http.defaultClientHttpsContext -> "http") def close(): Unit = Await.ready(closeAsync(), Duration.Inf) def closeAsync(): Future[Unit] = { for { _ <- http.shutdownAllConnectionPools() _ <- if (terminateActorSystem) system.terminate().map(_ => {}) else Future.successful({}) } yield {} } }
Example 110
Source File: HttpService.scala From ddd-leaven-akka-v2 with MIT License | 5 votes |
package ecommerce.sales import akka.actor.{Actor, ActorLogging, Props} import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives import pl.newicom.dddd.streams.ImplicitMaterializer import akka.util.Timeout import com.typesafe.config.Config import ecommerce.sales.app.ReservationViewEndpoint import io.github.lhotari.akka.http.health.HealthEndpoint.createDefaultHealthRoute import org.json4s.Formats import pl.newicom.dddd.serialization.JsonSerHints._ import pl.newicom.dddd.view.sql.SqlViewStore import scala.concurrent.duration.FiniteDuration import slick.jdbc.{JdbcProfile, PostgresProfile} object HttpService { def props(interface: String, port: Int, askTimeout: FiniteDuration): Props = Props(new HttpService(interface, port)(askTimeout)) } class HttpService(interface: String, port: Int)(implicit askTimeout: Timeout) extends Actor with ActorLogging with SalesReadFrontConfiguration with ImplicitMaterializer with Directives { import context.dispatcher implicit val formats: Formats = fromConfig(config) implicit val profile: JdbcProfile = PostgresProfile Http(context.system).bindAndHandle(route, interface, port) log.info(s"Listening on $interface:$port") override def receive: Receive = Actor.emptyBehavior override def config: Config = context.system.settings.config lazy val endpoints: ReservationViewEndpoint = new ReservationViewEndpoint private def route = pathPrefix("ecommerce" / "sales") { createDefaultHealthRoute() ~ provide(new SqlViewStore(config))(endpoints) } }
Example 111
Source File: HttpService.scala From ddd-leaven-akka-v2 with MIT License | 5 votes |
package ecommerce.sales.app import akka.actor.{Actor, ActorLogging, Props} import akka.event.Logging import akka.http.scaladsl.Http import akka.util.Timeout import ecommerce.sales.ReservationOfficeId import io.github.lhotari.akka.http.health.HealthEndpoint.createDefaultHealthRoute import org.json4s.Formats import pl.newicom.dddd.serialization.JsonSerHints.fromConfig import pl.newicom.dddd.writefront.HttpCommandHandler import scala.concurrent.duration.FiniteDuration object HttpService { def props(interface: String, port: Int, askTimeout: FiniteDuration): Props = Props(new HttpService(interface, port)(askTimeout)) } class HttpService(interface: String, port: Int)(implicit val timeout: Timeout) extends Actor with SalesFrontConfiguration with HttpCommandHandler with ActorLogging { import context.dispatcher implicit val formats: Formats = fromConfig(config) Http(context.system).bindAndHandle(route, interface, port) log.info(s"Listening on $interface:$port") override def receive: Receive = Actor.emptyBehavior override def offices = Set(ReservationOfficeId) private def route = logRequestResult(("sales", Logging.InfoLevel)) { pathPrefix("ecommerce" / "sales") { createDefaultHealthRoute() ~ handle[ecommerce.sales.Command] } } }
Example 112
Source File: HttpService.scala From ddd-leaven-akka-v2 with MIT License | 5 votes |
package ecommerce.shipping import akka.actor.{Actor, ActorLogging, Props} import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives import pl.newicom.dddd.streams.ImplicitMaterializer import akka.util.Timeout import com.typesafe.config.Config import ecommerce.shipping.app.ShipmentViewEndpoint import org.json4s.Formats import pl.newicom.dddd.serialization.JsonSerHints._ import pl.newicom.dddd.view.sql.SqlViewStore import scala.concurrent.duration.FiniteDuration import slick.jdbc.PostgresProfile object HttpService { def props(interface: String, port: Int, askTimeout: FiniteDuration): Props = Props(new HttpService(interface, port)(askTimeout)) } class HttpService(interface: String, port: Int)(implicit askTimeout: Timeout) extends Actor with ActorLogging with ShippingReadFrontConfiguration with ImplicitMaterializer with Directives { import context.dispatcher implicit val formats: Formats = fromConfig(config) implicit val profile = PostgresProfile Http(context.system).bindAndHandle(route, interface, port) log.info(s"Listening on $interface:$port") override def receive: Receive = Actor.emptyBehavior override def config: Config = context.system.settings.config lazy val endpoints: ShipmentViewEndpoint = new ShipmentViewEndpoint private def route = (provide(new SqlViewStore(config)) & pathPrefix("ecommerce" / "shipping"))(endpoints) }
Example 113
Source File: Boot.scala From eclair with Apache License 2.0 | 5 votes |
package fr.acinq.eclair import java.io.File import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.{ActorMaterializer, BindFailedException} import fr.acinq.eclair.api.Service import grizzled.slf4j.Logging import kamon.Kamon import scala.concurrent.ExecutionContext import scala.util.{Failure, Success} def startApiServiceIfEnabled(kit: Kit)(implicit system: ActorSystem, ec: ExecutionContext) = { val config = system.settings.config.getConfig("eclair") if(config.getBoolean("api.enabled")){ logger.info(s"json API enabled on port=${config.getInt("api.port")}") implicit val materializer = ActorMaterializer() val apiPassword = config.getString("api.password") match { case "" => throw EmptyAPIPasswordException case valid => valid } val apiRoute = new Service { override val actorSystem = system override val mat = materializer override val password = apiPassword override val eclairApi: Eclair = new EclairImpl(kit) }.route Http().bindAndHandle(apiRoute, config.getString("api.binding-ip"), config.getInt("api.port")).recover { case _: BindFailedException => onError(TCPBindException(config.getInt("api.port"))) } } else { logger.info("json API disabled") } } def onError(t: Throwable): Unit = { val errorMsg = if (t.getMessage != null) t.getMessage else t.getClass.getSimpleName System.err.println(s"fatal error: $errorMsg") logger.error(s"fatal error: $errorMsg", t) System.exit(1) } }
Example 114
Source File: HttpClient.scala From heimdallr with Apache License 2.0 | 5 votes |
package chat import scala.util.{Failure, Success, Try} import akka.actor._ import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.model.headers.RawHeader import akka.stream.ActorMaterializer import akka.util.ByteString import scala.concurrent.{ExecutionContext, Future} import chat.HttpClient._ import UserActor.CustomResponse object HttpClient { case class HttpClientGet(event: String, path : String) case class HttpClientPost(event: String, path : String, token: String, jsonBody: String) case class HttpClientResponseSuccess(event: String, resHttp: HttpResponse, recipient: ActorRef) case class HttpClientResponseFailure(event: String, reason: String, recipient: ActorRef) } class HttpClient()(implicit system: ActorSystem, mat: ActorMaterializer, dispatcher: ExecutionContext) extends Actor with ActorLogging { def pipeToSelf(event: String, future: Future[HttpResponse], recipient: ActorRef): Future[HttpResponse] = { future andThen { case Success(r) => self ! HttpClientResponseSuccess(event, r, recipient) case Failure(f) => self ! HttpClientResponseFailure(event, f.toString, recipient) } } def post(event: String, path: String, token: String, jsonBody: String, recipient: ActorRef) = { val objectEntity = HttpEntity(ContentTypes.`application/json`, jsonBody) val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest( method = HttpMethods.POST, uri = path, entity = objectEntity ).withHeaders( RawHeader("Authorization", "Token " + token) ) ) pipeToSelf(event, responseFuture, recipient) } def get(event: String, path: String, recipient:ActorRef) = { val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest( method = HttpMethods.GET, uri = path ) ) pipeToSelf(event, responseFuture, recipient) } def receive = { case HttpClientGet(event, path) => get(event, path, sender) case HttpClientPost(event, path, token, jsonBody) => post(event, path, token, jsonBody, sender) // connection success case HttpClientResponseSuccess(event, resp, recipient) => resp match { case HttpResponse(StatusCodes.OK, headers, entity, _) => entity.dataBytes.runFold(ByteString(""))(_ ++ _).foreach { body => log.info("Got response, body: " + body.utf8String) recipient ! CustomResponse(event, 200, body.utf8String) } case resp @ HttpResponse(code, _, _, _) => log.info("Request failed, response code: " + code) resp.discardEntityBytes() recipient ! CustomResponse(event, code.intValue(), s"Request failed, response code: $code") } // connection failure case HttpClientResponseFailure(event, resp, recipient) => log.info("Request failed, reason: " + resp) recipient ! CustomResponse(event, 599, s"Request failed, response code: ${resp}") case x => log.info(s"HttpClient Request failed: ${x}") } override def preStart(): Unit = { } override def preRestart(reason: Throwable, message: Option[Any]): Unit = { preStart() } override def postRestart(reason: Throwable): Unit = { log.info( reason.toString ) } override def postStop(): Unit = { } }
Example 115
Source File: WebService.scala From heimdallr with Apache License 2.0 | 5 votes |
package chat import scala.concurrent.ExecutionContext.Implicits._ import scala.util.{Failure,Success} import akka.actor.ActorSystem import akka.stream.Materializer import akka.http.scaladsl.Http import akka.http.scaladsl.Http.{ ServerBinding } import akka.http.scaladsl.model.{ HttpRequest, HttpResponse, Uri } import akka.stream.scaladsl.{ Flow, Sink, Source } import org.slf4j.LoggerFactory trait WebService { val log = LoggerFactory.getLogger("total") private var binding: scala.concurrent.Future[ServerBinding] = null def serviceBind(serviceName: String, bindRoute: Flow[HttpRequest, HttpResponse, Any], bindPort: Int) (implicit actorSystem: ActorSystem, materializer: Materializer): Unit = { binding = Http().bindAndHandle(bindRoute,"0.0.0.0", bindPort) // the rest of the sample code will go here binding.onComplete { //binding success check case Success(binding) => val localAddress = binding.localAddress log.info(s"${serviceName} is listening on ${localAddress.getAddress}:${localAddress.getPort}") case Failure(e) => log.error(s"${serviceName} Binding failed with ${e.getMessage}") } } def serviceUnbind(serviceName: String) = { if( binding != null ) { binding .flatMap(_.unbind()) .onComplete(_ => log.info(s"${serviceName} listening port unbinding ... ") ) } else log.info( s"${serviceName} Unbinding Failed !" ) } }
Example 116
Source File: LeagueServiceImpl.scala From eventsourcing-intro with Apache License 2.0 | 5 votes |
package eu.reactivesystems.league.impl import akka.Done import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import eu.reactivesystems.league.api.{Club, Game, LeagueService} import scala.concurrent.Future import scala.io.StdIn object LeagueServiceImpl extends LeagueService { implicit val system = ActorSystem("league-actorsystem") implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher override def addClub(leagueId: String, club: Club): Future[Done] = // get sharded instance // send message using ask // process result Future.successful(Done) override def addGame(leagueId: String, game: Game): Future[Done] = Future.successful(Done) override def changeGame(leagueId: String, game: Game): Future[Done] = Future.successful(Done) def main(args: Array[String]): Unit = { val port = system.settings.config.getInt("eu.reactivesystems.league.http.port") val bindingFuture = Http().bindAndHandle(routes, "localhost", port) println( s"Server online at http://localhost:$port/\nPress RETURN to stop...") StdIn.readLine() // let it run until user presses return bindingFuture .flatMap(_.unbind()) // trigger unbinding from the port .onComplete(_ => system.terminate()) // and shutdown when done } }
Example 117
Source File: FrontendNodeApp.scala From akka-exchange with Apache License 2.0 | 5 votes |
package com.boldradius.akka_exchange.frontend import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import com.boldradius.akka_exchange.util.ExchangeNodeBootable object FrontendNodeApp extends ExchangeNodeBootable { import net.ceedubs.ficus.Ficus._ implicit val materializer = ActorMaterializer() val route = path("offers") { get { complete { "Here's some data... or would be if we had data." } } } val httpPort = config.as[Int]("akka-exchange.cluster.frontend.port") // todo - make me based on local address so we don't need special config that locks out multiple http nodes val httpAddr = config.as[String]("akka-exchange.cluster.frontend.address") Http().bindAndHandle(route, httpAddr, httpPort) } // vim: set ts=2 sw=2 sts=2 et:
Example 118
Source File: EventSource.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.client import java.util.UUID import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.persistence.query.{NoOffset, Offset, Sequence, TimeBasedUUID} import akka.stream.Materializer import akka.stream.alpakka.sse.scaladsl.{EventSource => SSESource} import akka.stream.scaladsl.Source import ch.epfl.bluebrain.nexus.iam.auth.AccessToken import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import com.typesafe.scalalogging.Logger import io.circe.Decoder import io.circe.parser.decode import scala.concurrent.{ExecutionContext, Future} import scala.util.Try trait EventSource[A] { def apply[A: Decoder]( config: KgClientConfig )(implicit as: ActorSystem, mt: Materializer, ec: ExecutionContext): EventSource[A] = new EventSource[A] { private val logger = Logger[this.type] private val http = Http() private def addCredentials(request: HttpRequest)(implicit cred: Option[AccessToken]): HttpRequest = cred.map(token => request.addCredentials(OAuth2BearerToken(token.value))).getOrElse(request) private def send(request: HttpRequest)(implicit cred: Option[AccessToken]): Future[HttpResponse] = http.singleRequest(addCredentials(request)).map { resp => if (!resp.status.isSuccess()) logger.warn(s"HTTP response when performing SSE request: status = '${resp.status}'") resp } private def toOffset(id: String): Offset = Try(TimeBasedUUID(UUID.fromString(id))).orElse(Try(Sequence(id.toLong))).getOrElse(NoOffset) override def apply(iri: AbsoluteIri, offset: Option[String])(implicit cred: Option[AccessToken] ): Source[(Offset, A), NotUsed] = SSESource(iri.asAkka, send, offset, config.sseRetryDelay).flatMapConcat { sse => val offset = sse.id.map(toOffset).getOrElse(NoOffset) decode[A](sse.data) match { case Right(ev) => Source.single(offset -> ev) case Left(err) => logger.error(s"Failed to decode admin event '$sse'", err) Source.empty } } } }
Example 119
Source File: Main.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.storage import java.nio.file.Paths import java.time.Clock import akka.actor.ActorSystem import akka.event.{Logging, LoggingAdapter} import akka.http.scaladsl.Http import akka.http.scaladsl.server.Route import akka.util.Timeout import cats.effect.Effect import ch.epfl.bluebrain.nexus.storage.Storages.DiskStorage import ch.epfl.bluebrain.nexus.storage.attributes.AttributesCache import ch.epfl.bluebrain.nexus.storage.config.{AppConfig, Settings} import ch.epfl.bluebrain.nexus.storage.config.AppConfig._ import ch.epfl.bluebrain.nexus.storage.routes.Routes import com.typesafe.config.{Config, ConfigFactory} import kamon.Kamon import monix.eval.Task import monix.execution.Scheduler import scala.concurrent.duration._ import scala.concurrent.{Await, ExecutionContext, Future} import scala.util.{Failure, Success} //noinspection TypeAnnotation // $COVERAGE-OFF$ object Main { def loadConfig(): Config = { val cfg = sys.env.get("STORAGE_CONFIG_FILE") orElse sys.props.get("storage.config.file") map { str => val file = Paths.get(str).toAbsolutePath.toFile ConfigFactory.parseFile(file) } getOrElse ConfigFactory.empty() (cfg withFallback ConfigFactory.load()).resolve() } def setupMonitoring(config: Config): Unit = { if (sys.env.getOrElse("KAMON_ENABLED", "false").toBoolean) { Kamon.reconfigure(config) Kamon.loadModules() } } def shutdownMonitoring(): Unit = { if (sys.env.getOrElse("KAMON_ENABLED", "false").toBoolean) { Await.result(Kamon.stopModules(), 10.seconds) } } @SuppressWarnings(Array("UnusedMethodParameter")) def main(args: Array[String]): Unit = { val config = loadConfig() setupMonitoring(config) implicit val appConfig: AppConfig = Settings(config).appConfig implicit val as: ActorSystem = ActorSystem(appConfig.description.fullName, config) implicit val ec: ExecutionContext = as.dispatcher implicit val eff: Effect[Task] = Task.catsEffect(Scheduler.global) implicit val iamIdentities: IamIdentitiesClient[Task] = new IamIdentitiesClient[Task](appConfig.iam) implicit val timeout = Timeout(1.minute) implicit val clock = Clock.systemUTC val storages: Storages[Task, AkkaSource] = new DiskStorage(appConfig.storage, appConfig.digest, AttributesCache[Task, AkkaSource]) val logger: LoggingAdapter = Logging(as, getClass) logger.info("==== Cluster is Live ====") val routes: Route = Routes(storages) val httpBinding: Future[Http.ServerBinding] = { Http().bindAndHandle(routes, appConfig.http.interface, appConfig.http.port) } httpBinding onComplete { case Success(binding) => logger.info(s"Bound to ${binding.localAddress.getHostString}: ${binding.localAddress.getPort}") case Failure(th) => logger.error(th, "Failed to perform an http binding on {}:{}", appConfig.http.interface, appConfig.http.port) Await.result(as.terminate(), 10.seconds) } as.registerOnTermination { shutdownMonitoring() } // attempt to leave the cluster before shutting down val _ = sys.addShutdownHook { Await.result(as.terminate().map(_ => ()), 10.seconds) } } } // $COVERAGE-ON$
Example 120
Source File: IamIdentitiesClient.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.storage import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding.Get import akka.http.scaladsl.model.HttpRequest import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.unmarshalling.FromEntityUnmarshaller import akka.util.ByteString import cats.effect.{ContextShift, Effect, IO} import cats.implicits._ import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.storage.IamIdentitiesClient.Identity._ import ch.epfl.bluebrain.nexus.storage.IamIdentitiesClient._ import ch.epfl.bluebrain.nexus.storage.IamIdentitiesClientError.IdentitiesSerializationError import ch.epfl.bluebrain.nexus.storage.config.IamClientConfig import de.heikoseeberger.akkahttpcirce.ErrorAccumulatingCirceSupport.{DecodingFailures => AccDecodingFailures} import io.circe.Decoder.Result import io.circe.{Decoder, DecodingFailure, HCursor} import scala.concurrent.ExecutionContext class IamIdentitiesClient[F[_]](config: IamClientConfig)(implicit F: Effect[F], as: ActorSystem) extends JsonLdCirceSupport { private val um: FromEntityUnmarshaller[Caller] = unmarshaller[Caller] implicit private val ec: ExecutionContext = as.dispatcher implicit private val contextShift: ContextShift[IO] = IO.contextShift(ec) def apply()(implicit credentials: Option[AccessToken]): F[Caller] = credentials match { case Some(token) => execute(Get(config.identitiesIri.asAkka).addCredentials(OAuth2BearerToken(token.value))) case None => F.pure(Caller.anonymous) } private def execute(req: HttpRequest): F[Caller] = { IO.fromFuture(IO(Http().singleRequest(req))).to[F].flatMap { resp => if (resp.status.isSuccess()) IO.fromFuture(IO(um(resp.entity))).to[F].recoverWith { case err: AccDecodingFailures => F.raiseError(IdentitiesSerializationError(err.getMessage)) case err: Error => F.raiseError(IdentitiesSerializationError(err.getMessage)) } else IO.fromFuture(IO(resp.entity.dataBytes.runFold(ByteString(""))(_ ++ _).map(_.utf8String))) .to[F] .flatMap { err => F.raiseError(IamIdentitiesClientError.unsafe(resp.status, err)) } } } } object IamIdentitiesClient { final case class Authenticated(realm: String) extends Identity private def decodeAnonymous(hc: HCursor): Result[Subject] = hc.get[String]("@type").flatMap { case "Anonymous" => Right(Anonymous) case _ => Left(DecodingFailure("Cannot decode Anonymous Identity", hc.history)) } private def decodeUser(hc: HCursor): Result[Subject] = (hc.get[String]("subject"), hc.get[String]("realm")).mapN { case (subject, realm) => User(subject, realm) } private def decodeGroup(hc: HCursor): Result[Identity] = (hc.get[String]("group"), hc.get[String]("realm")).mapN { case (group, realm) => Group(group, realm) } private def decodeAuthenticated(hc: HCursor): Result[Identity] = hc.get[String]("realm").map(Authenticated) private val attempts = List[HCursor => Result[Identity]](decodeAnonymous, decodeUser, decodeGroup, decodeAuthenticated) implicit val identityDecoder: Decoder[Identity] = Decoder.instance { hc => attempts.foldLeft(Left(DecodingFailure("Unexpected", hc.history)): Result[Identity]) { case (acc @ Right(_), _) => acc case (_, f) => f(hc) } } } }
Example 121
Source File: Conseil.scala From Conseil with Apache License 2.0 | 5 votes |
package tech.cryptonomic.conseil.api import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import com.typesafe.scalalogging.LazyLogging import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport import tech.cryptonomic.conseil.api.config.{ConseilAppConfig, ConseilConfiguration} import tech.cryptonomic.conseil.api.util.Retry.retry import tech.cryptonomic.conseil.common.config.Platforms.PlatformsConfiguration import tech.cryptonomic.conseil.common.config._ import scala.concurrent.duration._ import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor} import scala.language.postfixOps import scala.util.Failure object Conseil extends App with LazyLogging with ConseilAppConfig with FailFastCirceSupport with ConseilMainOutput { loadApplicationConfiguration(args) match { case Left(errors) => //nothing to do case Right(config) => implicit val system: ActorSystem = ActorSystem("conseil-system") implicit val materializer: ActorMaterializer = ActorMaterializer() implicit val executionContext: ExecutionContextExecutor = system.dispatcher val retries = if (config.failFast.on) Some(0) else None val serverBinding = retry(maxRetry = retries, deadline = Some(config.server.startupDeadline fromNow))(ConseilApi.create(config)).andThen { case Failure(error) => logger.error( "The server was not started correctly, I failed to create the required Metadata service", error ) Await.ready(system.terminate(), 10.seconds) }.flatMap( runServer(_, config.server, config.platforms, config.verbose) ) sys.addShutdownHook { serverBinding .flatMap(_.unbind().andThen { case _ => logger.info("Server stopped...") }) .andThen { case _ => system.terminate() } .onComplete(_ => logger.info("We're done here, nothing else to see")) } } def runServer( api: ConseilApi, server: ConseilConfiguration, platforms: PlatformsConfiguration, verbose: VerboseOutput )(implicit executionContext: ExecutionContext, system: ActorSystem, mat: ActorMaterializer) = { val bindingFuture = Http().bindAndHandle(api.route, server.hostname, server.port) displayInfo(server) if (verbose.on) displayConfiguration(platforms) bindingFuture } }
Example 122
Source File: AkkaHttpPrometheusExporter.scala From cloudstate with Apache License 2.0 | 5 votes |
package io.cloudstate.proxy import java.io.OutputStreamWriter import java.util import akka.actor.ActorSystem import akka.http.scaladsl.Http import io.prometheus.client.CollectorRegistry import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.stream.Materializer import akka.util.ByteString import io.prometheus.client.exporter.common.TextFormat import scala.concurrent.Future class AkkaHttpPrometheusExporter(metricsPort: Int, registry: CollectorRegistry = CollectorRegistry.defaultRegistry)( implicit system: ActorSystem, mat: Materializer ) { private[this] final val PrometheusContentType = ContentType.parse(TextFormat.CONTENT_TYPE_004).right.get private def routes = get { (path("metrics") | pathSingleSlash) { encodeResponse { parameter(Symbol("name[]").*) { names => complete { val namesSet = new util.HashSet[String]() names.foreach(namesSet.add) val builder = ByteString.newBuilder val writer = new OutputStreamWriter(builder.asOutputStream) TextFormat.write004(writer, registry.filteredMetricFamilySamples(namesSet)) // Very important to flush the writer before we build the byte string! writer.flush() HttpEntity(PrometheusContentType, builder.result()) } } } } } def start(): Future[Http.ServerBinding] = Http().bindAndHandle(routes, "0.0.0.0", metricsPort) }
Example 123
Source File: Main.scala From quiz-management-service with Apache License 2.0 | 5 votes |
package com.danielasfregola.quiz.management import scala.concurrent.duration._ import akka.actor._ import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import akka.util.Timeout import com.typesafe.config.ConfigFactory object Main extends App with RestInterface { val config = ConfigFactory.load() val host = config.getString("http.host") val port = config.getInt("http.port") implicit val system = ActorSystem("quiz-management-service") implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher implicit val timeout = Timeout(10 seconds) val api = routes Http().bindAndHandle(handler = api, interface = host, port = port) map { binding => println(s"REST interface bound to ${binding.localAddress}") } recover { case ex => println(s"REST interface could not bind to $host:$port", ex.getMessage) } }
Example 124
Source File: AkkaHttpClientSupportSpec.scala From typedapi with MIT License | 5 votes |
package http.support.tests.client import http.support.tests.{User, UserCoding, Api} import typedapi.client._ import typedapi.client.akkahttp._ import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.http.scaladsl.Http import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport import org.specs2.mutable.Specification import org.specs2.concurrent.ExecutionEnv import scala.concurrent.duration._ import scala.concurrent.{Future, Await} final class AkkaHttpClientSupportSpec(implicit ee: ExecutionEnv) extends Specification { import UserCoding._ import FailFastCirceSupport._ sequential implicit val timeout = 5.second implicit val system = ActorSystem("akka-http-client-spec", defaultExecutionContext = Some(ee.ec)) implicit val mat = ActorMaterializer() import system.dispatcher val cm = ClientManager(Http(), "http://localhost", 9001) val server = TestServer.start() "akka http client support" >> { val (p, s, q, header, fixed, clInH, clFixH, clColl, serMatchH, serSendH, m0, m1, m2, m3, m4, m5, _, _, _) = deriveAll(Api) "paths and segments" >> { p().run[Future](cm) must beEqualTo(User("foo", 27)).awaitFor(timeout) s("jim").run[Future](cm) must beEqualTo(User("jim", 27)).awaitFor(timeout) } "queries" >> { q(42).run[Future](cm) must beEqualTo(User("foo", 42)).awaitFor(timeout) } "headers" >> { header(42).run[Future](cm) must beEqualTo(User("foo", 42)).awaitFor(timeout) fixed().run[Future](cm) must beEqualTo(User("joe", 27)).awaitFor(timeout) clInH("jim").run[Future](cm) must beEqualTo(User("jim", 27)).awaitFor(timeout) clFixH().run[Future](cm) must beEqualTo(User("joe", 27)).awaitFor(timeout) clColl(Map("coll" -> "joe", "collect" -> "jim")).run[Future](cm) must beEqualTo(User("coll: joe,collect: jim", 27)).awaitFor(timeout) serMatchH().run[Future](cm) must beEqualTo(User("joe", 27)).awaitFor(timeout) serSendH().run[Future](cm) must beEqualTo(User("joe", 27)).awaitFor(timeout) } "methods" >> { m0().run[Future](cm) must beEqualTo(User("foo", 27)).awaitFor(timeout) m1().run[Future](cm) must beEqualTo(User("foo", 27)).awaitFor(timeout) m2(User("jim", 42)).run[Future](cm) must beEqualTo(User("jim", 42)).awaitFor(timeout) m3().run[Future](cm) must beEqualTo(User("foo", 27)).awaitFor(timeout) m4(User("jim", 42)).run[Future](cm) must beEqualTo(User("jim", 42)).awaitFor(timeout) m5(List("because")).run[Future](cm) must beEqualTo(User("foo", 27)).awaitFor(timeout) } step { server.shutdown.unsafeRunSync() Await.ready(system.terminate, timeout) } } }
Example 125
Source File: AkkaHttpServerSupportSpec.scala From typedapi with MIT License | 5 votes |
package http.support.tests.server import http.support.tests.{Api, UserCoding} import typedapi.server._ import typedapi.server.akkahttp._ import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.http.scaladsl.Http import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport import cats.implicits._ import org.specs2.concurrent.ExecutionEnv import scala.concurrent.{Future, Await} import scala.concurrent.duration._ final class AkkaHttpServerSupportSpec(implicit ee: ExecutionEnv) extends ServerSupportSpec[Future]()(catsStdInstancesForFuture(ee.ec)) { import UserCoding._ import FailFastCirceSupport._ implicit val timeout = 5.second implicit val system = ActorSystem("akka-http-server-spec", defaultExecutionContext = Some(ee.ec)) implicit val mat = ActorMaterializer() import system.dispatcher val endpoints = deriveAll[Future](Api).from( path, segment, query, header, fixed, input, clientHdr, coll, matching, send, get, put, putB, post, postB, delete, code200, code400, code500 ) val sm = ServerManager(Http(), "localhost", 9000) val server = mount(sm, endpoints) "akka http implements TypedApi's server interface" >> { tests(9000) step { Await.ready(server.map(_.unbind()), timeout) Await.ready(system.terminate, timeout) } } }
Example 126
Source File: WolframServiceImpl.scala From lagom-on-kube with Apache License 2.0 | 5 votes |
package me.alexray.wolfram.impl import java.net.URLEncoder import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, Uri} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.Materializer import akka.util.ByteString import com.lightbend.lagom.scaladsl.api.ServiceCall import me.alexray.wolfram.api.WolframService import play.api.Configuration import scala.concurrent.{ExecutionContext, Future} class WolframServiceImpl(config: Configuration) (implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext) extends WolframService { val appID = config.underlying.getString("wolfram.appid") val apiUrl = s"http://api.wolframalpha.com/v2/" override def query(q: String): ServiceCall[NotUsed, String] = ServiceCall { _ => val url = apiUrl + s"query?appid=$appID&input=" + URLEncoder.encode(q, "UTF-8") for { response <- Http().singleRequest(HttpRequest(uri = Uri(url))) if response.status.isSuccess() data <- Unmarshal(response).to[String] } yield data } override def simple(q: String): ServiceCall[NotUsed, Array[Byte]] = ServiceCall { _ => println(s"quetions = '$q'") val url = apiUrl + s"simple?appid=$appID&input=" + URLEncoder.encode(q, "UTF-8").replace("+", "%20") println(s"url = '$url'") for { response <- Http().singleRequest(HttpRequest(uri = Uri(url))) if response.status.isSuccess() bytes <- Unmarshal(response).to[ByteString] } yield { println(s"received image ${bytes.size} bytes long") bytes.toArray } } }
Example 127
Source File: HTTPServer.scala From ForestFlow with Apache License 2.0 | 5 votes |
package ai.forestflow.serving.restapi import ai.forestflow.serving.config.ApplicationEnvironment import akka.Done import akka.actor.CoordinatedShutdown.{PhaseServiceUnbind, Reason} import akka.actor.SupervisorStrategy._ import akka.actor.{ActorRef, ActorSystem, CoordinatedShutdown} import akka.cluster.Cluster import akka.http.scaladsl.Http import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.stream.ActorMaterializer import akka.util.Timeout import ai.forestflow.akka.Supervisor import ai.forestflow.domain.ServableRoutes.GetProxyRoutes import ai.forestflow.utils.ThrowableImplicits._ import scala.concurrent.Await import scala.concurrent.duration._ import scala.language.postfixOps import scala.util.{Failure, Success} //noinspection TypeAnnotation object HTTPServer { private final case object BindFailure extends Reason } //noinspection TypeAnnotation class HTTPServer(servableProxyRef: ActorRef)(implicit system: ActorSystem, cluster: Cluster, shutdown: CoordinatedShutdown) { import HTTPServer._ import system.log private implicit val materializer = ActorMaterializer() private implicit val executionContext = system.dispatcher implicit lazy val timeout: Timeout = Timeout(ApplicationEnvironment.HTTP_COMMAND_TIMEOUT_SECS seconds) private val address = ApplicationEnvironment.HTTP_BIND_ADDRESS private val port = ApplicationEnvironment.HTTP_PORT val routesSupervisor = system.actorOf(Supervisor.props { case _: ArithmeticException => Resume case _: Exception => Restart }) private val servableRoutesActor = Await.result( routesSupervisor .ask(ServableRoutes.props(servableProxyRef)) .mapTo[ActorRef], ApplicationEnvironment.HTTP_COMMAND_TIMEOUT_SECS second) servableRoutesActor.ask(GetProxyRoutes()).onComplete { case Success(r: Route) => val bindingFuture = Http().bindAndHandle(r, address, port) bindingFuture.onComplete { case Success(bound) => log.info(s"AKKA HTTP Server online at http://${bound.localAddress.getHostString}:${bound.localAddress.getPort}/") shutdown.addTask(PhaseServiceUnbind, "api.unbind") { () => bound.terminate(5 seconds).map(_ => Done) } case Failure(e) => log.error(s"AKKA HTTP Server could not start! Shutting down... ${e.printableStackTrace}") shutdown.run(BindFailure) } case Failure(e) => log.error(s"Couldn't get dynamic HTTP routes from ServableRoutes actor! Shutting down... ${e.printableStackTrace}") shutdown.run(BindFailure) } }
Example 128
Source File: RestApiServer.scala From akka-blog-example with Apache License 2.0 | 5 votes |
package com.spr.akka import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.stream.Materializer import com.typesafe.config.ConfigFactory import scala.concurrent.Future class RestApiServer(api: RestApi)(implicit system: ActorSystem, materializer: Materializer) { def bind(): Future[ServerBinding] = { val config = ConfigFactory.load() val host = config.getString("http.host") val port = config.getInt("http.port") implicit val system = this.system implicit val materializer = this.materializer Http().bindAndHandle(api.route, host, port) } }
Example 129
Source File: Boot.scala From slick-akka-http-oauth2 with Apache License 2.0 | 5 votes |
import java.sql.Timestamp import akka.http.scaladsl.Http import akka.http.scaladsl.server.RouteConcatenation import akka.stream.ActorMaterializer import org.joda.time.DateTime import persistence.entities.{Account, OAuthClient} import rest.OAuthRoutes import utils._ object Main extends App with RouteConcatenation { // configuring modules for application, cake pattern for DI val modules = new ConfigurationModuleImpl with ActorModuleImpl with PersistenceModuleImpl implicit val system = modules.system implicit val materializer = ActorMaterializer() implicit val ec = modules.system.dispatcher modules.generateDDL() for { createAccounts <- modules.accountsDal.insert(Seq( Account(0, "[email protected]", "48181acd22b3edaebc8a447868a7df7ce629920a", new Timestamp(new DateTime().getMillis)) // password:bob )) createOauthClients <- modules.oauthClientsDal.insert(Seq( OAuthClient(0, 1, "client_credentials", "bob_client_id", "bob_client_secret", Some("redirectUrl"), new Timestamp(new DateTime().getMillis)))) } yield { println(s"Database initialized with default values for bob and alice") } val bindingFuture = Http().bindAndHandle( new OAuthRoutes(modules).routes, "localhost", 8080) println(s"Server online at http://localhost:8080/") }
Example 130
Source File: WebhookCaller.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.webhook import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.stream.ActorMaterializer import cool.graph.client.ClientInjector import cool.graph.cuid.Cuid import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future trait WebhookCaller { def call(url: String, payload: String): Future[Boolean] } class WebhookCallerMock extends WebhookCaller { private val _calls = scala.collection.parallel.mutable.ParTrieMap[String, (String, String)]() def calls: List[(String, String)] = _calls.values.toList var nextCallShouldFail = false def clearCalls: Unit = _calls.clear override def call(url: String, payload: String): Future[Boolean] = { _calls.put(Cuid.createCuid(), (url, payload)) Future.successful(!nextCallShouldFail) } } class WebhookCallerImplementation(implicit injector: ClientInjector) extends WebhookCaller { override def call(url: String, payload: String): Future[Boolean] = { implicit val system: ActorSystem = injector.system implicit val materializer: ActorMaterializer = injector.materializer println("calling " + url) Http() .singleRequest(HttpRequest(uri = url, method = HttpMethods.POST, entity = HttpEntity(contentType = ContentTypes.`application/json`, string = payload))) .map(x => { x.status.isSuccess() }) } }
Example 131
Source File: Version.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.singleserver import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import cool.graph.graphql.GraphQlClientImpl import spray.json._ import scala.concurrent.Future object Version { import DefaultJsonProtocol._ def check()(implicit system: ActorSystem, materializer: ActorMaterializer): Future[_] = { import system.dispatcher val client = GraphQlClientImpl("https://check-update.graph.cool", Map.empty, Http()(system)) client .sendQuery(""" |mutation { | checkUpdate(version: "1.0.0") { | newestVersion, | isUpToDate | } |} """.stripMargin) .flatMap { resp => if (resp.is200) { val json = resp.body.parseJson val updateStatus = json.asJsObject .fields("data") .asJsObject() .fields("checkUpdate") .asJsObject() .fields("isUpToDate") .convertTo[Boolean] if (updateStatus) println("Version is up to date.") else println("Update available.") } else { println("Unable to fetch version info.") } Future.successful(()) } .recoverWith { case _ => println("Unable to fetch version info.") Future.successful(()) } } }
Example 132
Source File: Auth0.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.system.externalServices import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.stream.ActorMaterializer import com.typesafe.config.Config import scaldi.{Injectable, Injector} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future case class Auth0ApiUpdateValues(email: Option[String]) trait Auth0Api { def updateClient(auth0Id: String, values: Auth0ApiUpdateValues): Future[Boolean] } class Auth0ApiMock extends Auth0Api { var lastUpdate: Option[(String, Auth0ApiUpdateValues)] = None override def updateClient(auth0Id: String, values: Auth0ApiUpdateValues): Future[Boolean] = { lastUpdate = Some((auth0Id, values)) Future.successful(true) } } class Auth0ApiImplementation(implicit inj: Injector) extends Auth0Api with Injectable { override def updateClient(auth0Id: String, values: Auth0ApiUpdateValues): Future[Boolean] = { implicit val system = inject[ActorSystem](identified by "actorSystem") implicit val materializer = inject[ActorMaterializer](identified by "actorMaterializer") val config = inject[Config](identified by "config") val auth0Domain = config.getString("auth0Domain") val auth0ApiToken = config.getString("auth0ApiToken") Http() .singleRequest( HttpRequest( uri = s"https://${auth0Domain}/api/v2/users/${auth0Id}", method = HttpMethods.PATCH, entity = HttpEntity(contentType = ContentTypes.`application/json`, string = s"""{"email":"${values.email.get}"}""") ).addCredentials(OAuth2BearerToken(auth0ApiToken))) .map(_.status.intValue match { case 200 => true case _ => false }) } }
Example 133
Source File: GraphQlClient.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.graphql import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import cool.graph.akkautil.SingleThreadedActorSystem import play.api.libs.json.{JsPath, JsValue, Json, Reads} import scala.concurrent.Future import scala.util.{Failure, Success, Try} trait GraphQlClient { def sendQuery(query: String): Future[GraphQlResponse] } object GraphQlClient { private implicit lazy val actorSystem = SingleThreadedActorSystem("graphql-client") private implicit lazy val actorMaterializer = ActorMaterializer()(actorSystem) private implicit lazy val akkaHttp = Http()(actorSystem) def apply(uri: String, headers: Map[String, String] = Map.empty): GraphQlClient = { GraphQlClientImpl(uri, headers, akkaHttp) } } case class GraphQlResponse(status: Int, body: String) { def bodyAs[T](path: String)(implicit reads: Reads[T]): Try[T] = { def jsPathForElements(pathElements: Seq[String], current: JsPath = JsPath()): JsPath = { if (pathElements.isEmpty) { current } else { jsPathForElements(pathElements.tail, current \ pathElements.head) } } val jsPath = jsPathForElements(path.split('.')) val actualReads = jsPath.read(reads) jsonBody.map(_.as(actualReads)) } val is2xx: Boolean = status >= 200 && status <= 299 val is200: Boolean = status == 200 val is404: Boolean = status == 404 def isSuccess: Boolean = deserializedBody match { case Success(x) => x.errors.isEmpty && is200 case Failure(e) => false } def isFailure: Boolean = !isSuccess def firstError: GraphQlError = deserializedBody.get.errors.head private lazy val deserializedBody: Try[GraphQlResponseJson] = { for { body <- jsonBody response <- Try { body.as(JsonReaders.graphqlResponseReads) } } yield response } lazy val jsonBody: Try[JsValue] = Try(Json.parse(body)) } case class GraphQlResponseJson(data: JsValue, errors: Seq[GraphQlError]) case class GraphQlError(message: String, code: Int) object JsonReaders { import play.api.libs.functional.syntax._ import play.api.libs.json._ implicit lazy val graphqlErrorReads = Json.reads[GraphQlError] implicit lazy val graphqlResponseReads = ( (JsPath \ "data").read[JsValue] and (JsPath \ "errors").readNullable[Seq[GraphQlError]].map(_.getOrElse(Seq.empty)) )(GraphQlResponseJson.apply _) }
Example 134
Source File: ServerExecutor.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.akkautil.http import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.stream.ActorMaterializer import ch.megard.akka.http.cors.scaladsl.CorsDirectives import ch.megard.akka.http.cors.scaladsl.CorsDirectives._ import scala.concurrent.duration.{Duration, _} import scala.concurrent.{Await, Future} case class ServerExecutor(port: Int, servers: Server*)(implicit system: ActorSystem, materializer: ActorMaterializer) { import system.dispatcher val routes: Route = { handleRejections(CorsDirectives.corsRejectionHandler) { cors() { val routes = servers.map(_.routes) :+ statusRoute routes.reduceLeft(_ ~ _) } } } def statusRoute: Route = (get & path("status")) { val checks = Future.sequence(servers.map(_.healthCheck)) onSuccess(checks) { _ => complete("OK") } } lazy val serverBinding: Future[ServerBinding] = { val binding = Http().bindAndHandle(Route.handlerFlow(routes), "0.0.0.0", port) binding.onSuccess { case b => println(s"Server running on :${b.localAddress.getPort}") } binding } def start: Future[_] = Future.sequence[Any, Seq](servers.map(_.onStart) :+ serverBinding) def stop: Future[_] = Future.sequence[Any, Seq](servers.map(_.onStop) :+ serverBinding.map(_.unbind)) // Starts the server and blocks the calling thread until the underlying actor system terminates. def startBlocking(duration: Duration = 15.seconds): Unit = { start Await.result(system.whenTerminated, Duration.Inf) } def stopBlocking(duration: Duration = 15.seconds): Unit = Await.result(stop, duration) }
Example 135
Source File: WebServer.scala From akka-http-scalajs.g8 with Apache License 2.0 | 5 votes |
package $package$ import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import com.typesafe.config.ConfigFactory object WebServer { def main(args: Array[String]) { implicit val system = ActorSystem("server-system") implicit val materializer = ActorMaterializer() val config = ConfigFactory.load() val interface = config.getString("http.interface") val port = config.getInt("http.port") val service = new WebService() Http().bindAndHandle(service.route, interface, port) println(s"Server online at http://\$interface:\$port") } }
Example 136
Source File: RequestRunner.scala From aws-spi-akka-http with Apache License 2.0 | 5 votes |
package com.github.matsluni.akkahttpspi import java.util.concurrent.CompletableFuture import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpRequest import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.stream.Materializer import akka.stream.scaladsl.{Keep, Sink} import org.slf4j.LoggerFactory import software.amazon.awssdk.http.SdkHttpFullResponse import software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler import scala.compat.java8.FutureConverters import scala.concurrent.ExecutionContext import scala.collection.JavaConverters._ class RequestRunner(connectionPoolSettings: ConnectionPoolSettings)(implicit sys: ActorSystem, ec: ExecutionContext, mat: Materializer) { val logger = LoggerFactory.getLogger(this.getClass) def run(httpRequest: HttpRequest, handler: SdkAsyncHttpResponseHandler): CompletableFuture[Void] = { val result = Http() .singleRequest(httpRequest, settings = connectionPoolSettings) .flatMap { response => val sdkResponse = SdkHttpFullResponse.builder() .headers(response.headers.groupBy(_.name()).map{ case (k, v) => k -> v.map(_.value()).asJava }.asJava) .statusCode(response.status.intValue()) .statusText(response.status.reason) .build handler.onHeaders(sdkResponse) val (complete, publisher) = response .entity .dataBytes .map(_.asByteBuffer) .alsoToMat(Sink.ignore)(Keep.right) .toMat(Sink.asPublisher(fanout = false))(Keep.both) .run() handler.onStream(publisher) complete } result.failed.foreach(handler.onError) FutureConverters.toJava(result.map(_ => null: Void)).toCompletableFuture } }
Example 137
Source File: AuthCodeCard.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.marshalling.ToResponseMarshallable import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.stream.ActorFlowMaterializer case class CodeCard(id: Long, codes: Seq[String], userIdentifier: String) case class RegisterResponse(identity: Identity, codesCard: CodeCard) case class LoginRequest(userIdentifier: String, cardIndex: Long, codeIndex: Long, code: String) case class ActivateCodeRequest(userIdentifier: String) case class ActivateCodeResponse(cardIndex: Long, codeIndex: Long) case class GetCodeCardRequest(userIdentifier: String) case class GetCodeCardResponse(userIdentifier: String, codesCard: CodeCard) case class Identity(id: Long) case class Token(value: String, validTo: Long, identityId: Long, authMethods: Set[String]) object AuthCodeCardCard extends App with JsonProtocols with Config { implicit val actorSystem = ActorSystem() implicit val materializer = ActorFlowMaterializer() implicit val dispatcher = actorSystem.dispatcher val repository = new Repository val gateway = new Gateway val service = new Service(gateway, repository) Http().bindAndHandle(interface = interface, port = port, handler = { logRequestResult("auth-codecard") { (path("register" / "codecard" ) & pathEndOrSingleSlash & post & optionalHeaderValueByName("Auth-Token")) { (tokenValue) => complete { service.register(tokenValue).map[ToResponseMarshallable] { case Right(response) => Created -> response case Left(errorMessage) => BadRequest -> errorMessage } } } ~ (path("login" / "codecard" / "activate") & pathEndOrSingleSlash & post & entity(as[ActivateCodeRequest])) { (request) => complete { service.activateCode(request).map[ToResponseMarshallable] { case Right(response) => OK -> response case Left(errorMessage) => BadRequest -> errorMessage } } } ~ (path("login" / "codecard") & pathEndOrSingleSlash & post & optionalHeaderValueByName("Auth-Token") & entity(as[LoginRequest])) { (tokenValue, request) => complete { service.login(request, tokenValue).map[ToResponseMarshallable] { case Right(response) => Created -> response case Left(errorMessage) => BadRequest -> errorMessage } } } ~ (path("generate" / "codecard") & pathEndOrSingleSlash & post & optionalHeaderValueByName("Auth-Token") & entity(as[GetCodeCardRequest])) { (tokenValue, request) => complete { service.getCodeCard(request, tokenValue).map[ToResponseMarshallable] { case Right(response) => OK -> response case Left(errorMessage) => BadRequest -> errorMessage } } } } }) }
Example 138
Source File: Gateway.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.FlowMaterializer import akka.stream.scaladsl.{Sink, Source} import java.io.IOException import scala.concurrent.{ExecutionContext, Future} case class InternalLoginRequest(identityId: Long, authMethod: String = "codecard") case class InternalReloginRequest(tokenValue: String, authMethod: String = "codecard") class Gateway(implicit actorSystem: ActorSystem, materializer: FlowMaterializer, ec: ExecutionContext) extends JsonProtocols with Config { private val identityManagerConnectionFlow = Http().outgoingConnection(identityManagerHost, identityManagerPort) private val tokenManagerConnectionFlow = Http().outgoingConnection(tokenManagerHost, tokenManagerPort) private def requestIdentityManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(identityManagerConnectionFlow).runWith(Sink.head) } private def requestTokenManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(tokenManagerConnectionFlow).runWith(Sink.head) } def requestToken(tokenValue: String): Future[Either[String, Token]] = { requestTokenManager(RequestBuilding.Get(s"/tokens/$tokenValue")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Right(_)) case NotFound => Future.successful(Left("Token expired or not found")) case _ => Future.failed(new IOException(s"Token request failed with status ${response.status} and error ${response.entity}")) } } } def requestNewIdentity(): Future[Identity] = { requestIdentityManager(RequestBuilding.Post("/identities")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Identity] case _ => Future.failed(new IOException(s"Identity request failed with status ${response.status} and error ${response.entity}")) } } } def requestLogin(identityId: Long): Future[Token] = { val loginRequest = InternalLoginRequest(identityId) requestTokenManager(RequestBuilding.Post("/tokens", loginRequest)).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token] case _ => Future.failed(new IOException(s"Login request failed with status ${response.status} and error ${response.entity}")) } } } def requestRelogin(tokenValue: String): Future[Option[Token]] = { requestTokenManager(RequestBuilding.Patch("/tokens", InternalReloginRequest(tokenValue))).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Option(_)) case NotFound => Future.successful(None) case _ => Future.failed(new IOException(s"Relogin request failed with status ${response.status} and error ${response.entity}")) } } } }
Example 139
Source File: IdentityManager.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.stream.ActorFlowMaterializer import com.typesafe.config.ConfigFactory import scala.concurrent.blocking import scala.slick.driver.PostgresDriver.simple._ import scala.slick.lifted.{ProvenShape, Tag} import spray.json.DefaultJsonProtocol case class Identity(id: Option[Long], createdAt: Long) class Identities(tag: Tag) extends Table[Identity](tag, "identity") { def id = column[Long]("id", O.PrimaryKey, O.AutoInc) def createdAt = column[Long]("created_at", O.NotNull) override def * : ProvenShape[Identity] = (id.?, createdAt) <> ((Identity.apply _).tupled, Identity.unapply) } object IdentityManager extends App with DefaultJsonProtocol { val config = ConfigFactory.load() val interface = config.getString("http.interface") val port = config.getInt("http.port") val dbUrl = config.getString("db.url") val dbUser = config.getString("db.user") val dbPassword = config.getString("db.password") implicit val actorSystem = ActorSystem() implicit val materializer = ActorFlowMaterializer() implicit val dispatcher = actorSystem.dispatcher implicit val identityFormat = jsonFormat2(Identity.apply) val db = Database.forURL(url = dbUrl, user = dbUser, password = dbPassword, driver = "org.postgresql.Driver") val identities = TableQuery[Identities] def getAllIdentities(): List[Identity] = { blocking { db.withSession { implicit s => identities.list } } } def saveIdentity(identity: Identity): Identity = { blocking { db.withSession { implicit s => identities returning identities.map(_.id) into ((_, id) => identity.copy(id = Option(id))) += identity } } } Http().bindAndHandle(interface = interface, port = port, handler = { logRequestResult("identity-manager") { path("identities") { pathEndOrSingleSlash { post { complete { val newIdentity = Identity(id = None, createdAt = System.currentTimeMillis()) Created -> saveIdentity(newIdentity) } } ~ get { complete { getAllIdentities() } } } } } }) }
Example 140
Source File: Gateway.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.FlowMaterializer import akka.stream.scaladsl.{Sink, Source} import java.io.IOException import scala.concurrent.{ExecutionContext, Future} case class InternalLoginRequest(identityId: Long, authMethod: String = "password") case class InternalReloginRequest(tokenValue: String, authMethod: String = "password") class Gateway(implicit actorSystem: ActorSystem, materializer: FlowMaterializer, ec: ExecutionContext) extends JsonProtocols with Config { private val identityManagerConnectionFlow = Http().outgoingConnection(identityManagerHost, identityManagerPort) private val tokenManagerConnectionFlow = Http().outgoingConnection(tokenManagerHost, tokenManagerPort) private def requestIdentityManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(identityManagerConnectionFlow).runWith(Sink.head) } private def requestTokenManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(tokenManagerConnectionFlow).runWith(Sink.head) } def requestToken(tokenValue: String): Future[Either[String, Token]] = { requestTokenManager(RequestBuilding.Get(s"/tokens/$tokenValue")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Right(_)) case NotFound => Future.successful(Left("Token expired or not found")) case _ => Future.failed(new IOException(s"Token request failed with status ${response.status} and error ${response.entity}")) } } } def requestNewIdentity(): Future[Identity] = { requestIdentityManager(RequestBuilding.Post("/identities")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Identity] case _ => Future.failed(new IOException(s"Identity request failed with status ${response.status} and error ${response.entity}")) } } } def requestLogin(identityId: Long): Future[Token] = { val loginRequest = InternalLoginRequest(identityId) requestTokenManager(RequestBuilding.Post("/tokens", loginRequest)).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token] case _ => Future.failed(new IOException(s"Login request failed with status ${response.status} and error ${response.entity}")) } } } def requestRelogin(tokenValue: String): Future[Option[Token]] = { requestTokenManager(RequestBuilding.Patch("/tokens", InternalReloginRequest(tokenValue))).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Option(_)) case NotFound => Future.successful(None) case _ => Future.failed(new IOException(s"Relogin request failed with status ${response.status} and error ${response.entity}")) } } } }
Example 141
Source File: AuthPassword.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.marshalling.ToResponseMarshallable import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.stream.ActorFlowMaterializer case class PasswordRegisterRequest(email: EmailAddress, password: String) case class PasswordLoginRequest(email: EmailAddress, password: String) case class PasswordResetRequest(email: EmailAddress, newPassword: String) case class Identity(id: Long) case class Token(value: String, validTo: Long, identityId: Long, authMethods: Set[String]) object AuthPassword extends App with JsonProtocols with Config { implicit val actorSystem = ActorSystem() implicit val materializer = ActorFlowMaterializer() implicit val dispatcher = actorSystem.dispatcher val repository = new Repository val gateway = new Gateway val service = new Service(repository, gateway) Http().bindAndHandle(interface = interface, port = port, handler = { logRequestResult("auth-password") { path("register" / "password") { (pathEndOrSingleSlash & post & entity(as[PasswordRegisterRequest]) & optionalHeaderValueByName("Auth-Token")) { (request, tokenValue) => complete { service.register(request, tokenValue).map[ToResponseMarshallable] { case Right(identity) => Created -> identity case Left(errorMessage) => BadRequest -> errorMessage } } } } ~ path("login" / "password") { (pathEndOrSingleSlash & post & entity(as[PasswordLoginRequest]) & optionalHeaderValueByName("Auth-Token")) { (request, tokenValue) => complete { service.login(request, tokenValue).map[ToResponseMarshallable] { case Right(token) => Created -> token case Left(errorMessage) => BadRequest -> errorMessage } } } } ~ path("reset" / "password") { (pathEndOrSingleSlash & post & entity(as[PasswordResetRequest]) & headerValueByName("Auth-Token")) { (request, tokenValue) => complete { service.reset(request, tokenValue).map[ToResponseMarshallable] { case Right(identity) => OK -> identity case Left(errorMessage) => BadRequest -> errorMessage } } } } } }) }
Example 142
Source File: SessionManager.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding import akka.http.scaladsl.model.{HttpResponse, HttpRequest} import akka.http.scaladsl.server.Directives._ import akka.stream.ActorFlowMaterializer import akka.stream.scaladsl.{Sink, Source} import com.typesafe.config.ConfigFactory import scala.concurrent.Future object SessionManager extends App { val config = ConfigFactory.load() val interface = config.getString("http.interface") val port = config.getInt("http.port") val tokenManagerHost = config.getString("services.token-manager.host") val tokenManagerPort = config.getInt("services.token-manager.port") implicit val actorSystem = ActorSystem() implicit val materializer = ActorFlowMaterializer() implicit val dispatcher = actorSystem.dispatcher val tokenManagerConnectionFlow = Http().outgoingConnection(tokenManagerHost, tokenManagerPort) def requestTokenManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(tokenManagerConnectionFlow).runWith(Sink.head) } Http().bindAndHandle(interface = interface, port = port, handler = { logRequestResult("session-manager") { path("session") { headerValueByName("Auth-Token") { tokenValue => pathEndOrSingleSlash { get { complete { requestTokenManager(RequestBuilding.Get(s"/tokens/$tokenValue")) } } ~ delete { complete { requestTokenManager(RequestBuilding.Delete(s"/tokens/$tokenValue")) } } } } } } }) }
Example 143
Source File: AuthFb.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.marshalling.ToResponseMarshallable import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.stream.ActorFlowMaterializer import com.restfb.exception.FacebookException import scala.util.{Failure => FailureT, Success => SuccessT} case class AuthResponse(accessToken: String) case class Identity(id: Long) case class Token(value: String, validTo: Long, identityId: Long, authMethods: Set[String]) object AuthFb extends App with JsonProtocols with Config { implicit val actorSystem = ActorSystem() implicit val materializer = ActorFlowMaterializer() implicit val dispatcher = actorSystem.dispatcher val gateway = new Gateway val service = new Service(gateway) Http().bindAndHandle(interface = interface, port = port, handler = { logRequestResult("auth-fb") { (path("register" / "fb") & pathEndOrSingleSlash & post & entity(as[AuthResponse]) & optionalHeaderValueByName("Auth-Token")) { (authResponse, tokenValue) => complete { service.register(authResponse, tokenValue) match { case SuccessT(f) => f.map[ToResponseMarshallable] { case Right(identity) => Created -> identity case Left(errorMessage) => BadRequest -> errorMessage } case FailureT(e: FacebookException) => Unauthorized -> e.getMessage case _ => InternalServerError } } } ~ (path("login" / "fb") & pathEndOrSingleSlash & post & entity(as[AuthResponse]) & optionalHeaderValueByName("Auth-Token")) { (authResponse, tokenValue) => complete { service.login(authResponse, tokenValue) match { case SuccessT(f) => f.map[ToResponseMarshallable] { case Right(token) => Created -> token case Left(errorMessage) => BadRequest -> errorMessage } case FailureT(e: FacebookException) => Unauthorized -> e.getMessage case _ => InternalServerError } } } } }) }
Example 144
Source File: Gateway.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.FlowMaterializer import akka.stream.scaladsl.{Sink, Source} import com.restfb.DefaultFacebookClient import com.restfb.types.User import java.io.IOException import scala.concurrent.{blocking, ExecutionContext, Future} import scala.util.Try case class InternalLoginRequest(identityId: Long, authMethod: String = "fb") case class InternalReloginRequest(tokenValue: String, authMethod: String = "fb") class Gateway(implicit actorSystem: ActorSystem, materializer: FlowMaterializer, ec: ExecutionContext) extends JsonProtocols with Config { private val identityManagerConnectionFlow = Http().outgoingConnection(identityManagerHost, identityManagerPort) private val tokenManagerConnectionFlow = Http().outgoingConnection(tokenManagerHost, tokenManagerPort) private def requestIdentityManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(identityManagerConnectionFlow).runWith(Sink.head) } private def requestTokenManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(tokenManagerConnectionFlow).runWith(Sink.head) } def requestToken(tokenValue: String): Future[Either[String, Token]] = { requestTokenManager(RequestBuilding.Get(s"/tokens/$tokenValue")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Right(_)) case NotFound => Future.successful(Left("Token expired or not found")) case _ => Future.failed(new IOException(s"Token request failed with status ${response.status} and error ${response.entity}")) } } } def requestNewIdentity(): Future[Identity] = { requestIdentityManager(RequestBuilding.Post("/identities")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Identity] case _ => Future.failed(new IOException(s"Identity request failed with status ${response.status} and error ${response.entity}")) } } } def requestLogin(identityId: Long): Future[Token] = { val loginRequest = InternalLoginRequest(identityId) requestTokenManager(RequestBuilding.Post("/tokens", loginRequest)).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token] case _ => Future.failed(new IOException(s"Login request failed with status ${response.status} and error ${response.entity}")) } } } def requestRelogin(tokenValue: String): Future[Option[Token]] = { requestTokenManager(RequestBuilding.Patch("/tokens", InternalReloginRequest(tokenValue))).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Option(_)) case NotFound => Future.successful(None) case _ => Future.failed(new IOException(s"Relogin request failed with status ${response.status} and error ${response.entity}")) } } } def getFbUserDetails(accessToken: String): Try[User] = { Try { blocking { val client = new DefaultFacebookClient(accessToken) client.fetchObject("me", classOf[User]) } } } }
Example 145
Source File: TokenManager.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.marshalling.ToResponseMarshallable import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.stream.ActorFlowMaterializer import metrics.common.{Value, RequestResponseStats, Counter, Metrics} import metrics.common.MetricsDirectives._ case class LoginRequest(identityId: Long, authMethod: String) case class ReloginRequest(tokenValue: String, authMethod: String) case class Token(value: String, validTo: Long, identityId: Long, authMethods: Set[String]) object TokenManager extends App with JsonProtocols with Config with Metrics { implicit val actorSystem = ActorSystem() implicit val materializer = ActorFlowMaterializer() implicit val dispatcher = actorSystem.dispatcher val repository = new Repository val service = new Service(repository) def putMetricForRequestResponse(requestStats: RequestResponseStats): Unit = { val method = requestStats.request.method.name.toLowerCase putMetric(Value(s"token-manager.$method.time", requestStats.time)) } Http().bindAndHandle(interface = interface, port = port, handler = { (measureRequestResponse(putMetricForRequestResponse) & logRequestResult("token-manager")) { pathPrefix("tokens") { (post & pathEndOrSingleSlash & entity(as[LoginRequest])) { loginRequest => complete { putMetric(Counter("token-manager.post", 1)) service.login(loginRequest).map(token => Created -> token) } } ~ (patch & pathEndOrSingleSlash & entity(as[ReloginRequest])) { reloginRequest => complete { service.relogin(reloginRequest).map[ToResponseMarshallable] { case Some(token) => putMetric(Counter("token-manager.patch", 1)) OK -> token case None => putMetric(Counter("token-manager.patch", -1)) NotFound -> "Token expired or not found" } } } ~ (path(Segment) & pathEndOrSingleSlash) { tokenValue => get { complete { service.findAndRefreshToken(tokenValue).map[ToResponseMarshallable] { case Some(token) => putMetric(Counter("token-manager.get", 1)) OK -> token case None => putMetric(Counter("token-manager.get", -1)) NotFound -> "Token expired or not found" } } } ~ delete { complete { service.logout(tokenValue) putMetric(Counter("token-manager.delete", 1)) OK } } } } } }) }
Example 146
Source File: HttpClientProvider.scala From reactive-nakadi with MIT License | 5 votes |
package org.zalando.react.nakadi.client.providers import java.security.SecureRandom import java.security.cert.X509Certificate import javax.net.ssl.{SSLContext, TrustManager, X509TrustManager} import akka.actor.ActorContext import akka.http.scaladsl.Http.OutgoingConnection import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.settings.ClientConnectionSettings import akka.http.scaladsl.{Http, HttpsConnectionContext} import akka.stream.scaladsl.Flow import scala.concurrent.Future import scala.concurrent.duration._ class HttpClientProvider(actorContext: ActorContext, server: String, port: Int, isConnectionSSL: Boolean = false, acceptAnyCertificate: Boolean = false, connectionTimeout: FiniteDuration) { val http = Http(actorContext.system) private val settings = { ClientConnectionSettings .apply(actorContext.system) .withConnectingTimeout(connectionTimeout) .withIdleTimeout(Duration.Inf) } val connection: Flow[HttpRequest, HttpResponse, Future[OutgoingConnection]] = { isConnectionSSL match { case true => val sslContext = if (!acceptAnyCertificate) SSLContext.getDefault else { val permissiveTrustManager: TrustManager = new X509TrustManager() { override def checkClientTrusted(chain: Array[X509Certificate], authType: String): Unit = {} override def checkServerTrusted(chain: Array[X509Certificate], authType: String): Unit = {} override def getAcceptedIssuers(): Array[X509Certificate] = Array.empty } val ctx = SSLContext.getInstance("TLS") ctx.init(Array.empty, Array(permissiveTrustManager), new SecureRandom()) ctx } http.outgoingConnectionHttps(server, port, new HttpsConnectionContext(sslContext), settings = settings) case false => http.outgoingConnection(server, port, settings = settings) } } }
Example 147
Source File: HttpUtil.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.analytics.util import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.RequestEntityAcceptance.Tolerated import akka.http.scaladsl.model.{HttpMethod, HttpRequest, HttpResponse} import akka.stream.ActorMaterializer import akka.stream.scaladsl.Sink import akka.util.ByteString import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper} import com.typesafe.config.ConfigFactory import scala.concurrent.duration.{MILLISECONDS, _} import scala.concurrent.{Await, ExecutionContextExecutor, Future} object HttpUtil { private val mapper = new ObjectMapper() private val config = ConfigFactory.load private val ReadTimeout = FiniteDuration(config.getDuration("extract-index-from-es.read-timeout").toMillis, MILLISECONDS) // Elasticsearch uses the POST verb in some places where the request is actually idempotent. // Requests that use POST, but are known to be idempotent can use this method. // The presence of any non-idempotent request in-flight causes Akka to not retry, and that will tend result in // entire downloads failing more often. val SAFE_POST = HttpMethod( value = "POST", isSafe = true, isIdempotent = true, requestEntityAcceptance = Tolerated) def resultAsync(request: HttpRequest, action: String) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): Future[ByteString] = Http().singleRequest(request).map { case HttpResponse(status, _, entity, _) if status.isSuccess => entity.dataBytes .fold(ByteString.empty)(_ ++ _) .runWith(Sink.head) case HttpResponse(status, _, entity, _) => val message = Await.result(entity.toStrict(10.seconds).map(_.data), 10.seconds).utf8String throw new RuntimeException(s"HTTP request for $action failed. Status code: $status, message:$message") } .flatMap(identity) def result(request: HttpRequest, action: String, timeout: FiniteDuration = ReadTimeout) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): ByteString = Await.result(resultAsync(request, action), timeout) def jsonResult(request: HttpRequest, action: String, timeout: FiniteDuration = ReadTimeout) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): JsonNode = mapper.readTree(result(request, action, timeout).utf8String) def jsonResultAsync(request: HttpRequest, action: String) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): Future[JsonNode] = resultAsync(request, action).map((bytes: ByteString) => mapper.readTree(bytes.utf8String)) }
Example 148
Source File: SimpleDowningSpec.scala From simple-akka-downing with Apache License 2.0 | 5 votes |
package com.ajjpj.simpleakkadowning.util import akka.actor.Props import akka.cluster.{Cluster, MemberStatus} import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, Uri} import akka.remote.testconductor.RoleName import akka.remote.testkit.MultiNodeSpec import akka.remote.transport.ThrottlerTransportAdapter.Direction import akka.stream.ActorMaterializer import akka.testkit.ImplicitSender import scala.concurrent.duration._ import scala.util.control.NonFatal abstract class SimpleDowningSpec(config: SimpleDowningConfig) extends MultiNodeSpec(config) with STMultiNodeSpec with ImplicitSender { def initialParticipants = roles.size private var portToNode = Map.empty[Int,RoleName] def init(): Unit = { if (roles.headOption contains myself) { enterBarrier("initialized") } else { val cluster = Cluster(system) cluster.joinSeedNodes(seedAddresses) system.actorOf(Props(new ClusterHttpInspector(httpPort(myself))), "http-server") while (cluster.state.members.count(_.status == MemberStatus.Up) < roles.tail.size) Thread.sleep(100) enterBarrier("initialized") } portToNode = roles.map(r => node(r).address.port.get -> r).toMap } def httpPort (node: RoleName) = { val nodeNo = roles.indexOf(node) require(nodeNo > 0) 8080 + nodeNo } def seedAddresses = roles.tail.map(node(_).root.address) private def httpGetNodes(node: RoleName, path: String): Set[RoleName] = { try { import system.dispatcher implicit val mat = ActorMaterializer() val uri = Uri (s"http://localhost:${httpPort (node)}$path") val response = Http (system).singleRequest (HttpRequest (uri = uri)).await val strict = response.entity.toStrict (10.seconds).await strict.data.decodeString ("utf-8") match { case s if s.isEmpty => Set.empty case s => s.split (' ').map (_.toInt).map (portToNode).toSet } } catch { case NonFatal(th) => th.printStackTrace() Set.empty } } def upNodesFor(node: RoleName) = httpGetNodes(node, "/cluster-members/up") def unreachableNodesFor (node: RoleName) = httpGetNodes(node, "/cluster-members/unreachable") def createPartition(nodes: RoleName*) = { val otherNodes = roles.tail.toSet -- nodes for (n1 <- nodes; n2 <- otherNodes) testConductor.blackhole(n1, n2, Direction.Both).await } def healPartition(): Unit = { for (n1 <- roles.tail; n2 <- roles.tail) testConductor.passThrough(n1, n2, Direction.Both).await } }
Example 149
Source File: ClusterHttpInspector.scala From simple-akka-downing with Apache License 2.0 | 5 votes |
package com.ajjpj.simpleakkadowning.util import akka.actor.Actor import akka.cluster.{Cluster, MemberStatus} import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives import akka.stream.ActorMaterializer import scala.concurrent.Await class ClusterHttpInspector(httpPort: Int) extends Actor { val cluster = Cluster.get(context.system) val routes = { import Directives._ pathPrefix("cluster-members") { path("up") { complete { cluster.state.members.filter(_.status == MemberStatus.Up).map(_.address.port.get).mkString(" ") }} ~ path("unreachable") { complete { cluster.state.unreachable.map(_.address.port.get).mkString(" ") }} } } import context.dispatcher implicit val mat = ActorMaterializer() val fServerBinding = Http(context.system) .bindAndHandle(routes, "localhost", httpPort) override def postStop () = { import scala.concurrent.duration._ super.postStop () fServerBinding.foreach(sb => Await.ready (sb.unbind(), 5.seconds)) } override def receive = Actor.emptyBehavior }
Example 150
Source File: PrometheusUtils.scala From kafka-lag-exporter with Apache License 2.0 | 5 votes |
package com.lightbend.kafkalagexporter.integration import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, HttpResponse, StatusCodes} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.Materializer import com.lightbend.kafkalagexporter.MetricsSink.GaugeDefinition import org.scalatest.Matchers import org.scalatest.concurrent.ScalaFutures import org.slf4j.{Logger, LoggerFactory} import scala.concurrent.{ExecutionContext, Future} import scala.util.matching.Regex val regex = s"""$name\\{$labels.*\\}\\s+(-?.+)""".r log.debug(s"Created regex: {}", regex.pattern.toString) Rule(regex, assertion) } } case class Rule(regex: Regex, assertion: String => _) case class Result(rule: Rule, groupResults: List[String]) { def assertDne(): Unit = { log.debug(s"Rule: ${rule.regex.toString}") groupResults.length shouldBe 0 } def assert(): Unit = { log.debug(s"Rule: ${rule.regex.toString}") groupResults.length shouldBe 1 log.debug(s"Actual value is ${groupResults.head}") rule.assertion(groupResults.head) } } }
Example 151
Source File: ClusterApp.scala From reactive-lib with Apache License 2.0 | 5 votes |
package foo import akka.actor.{ Actor, ActorLogging, ActorSystem, PoisonPill, Props } import akka.cluster.ClusterEvent.ClusterDomainEvent import akka.cluster.singleton.{ ClusterSingletonManager, ClusterSingletonManagerSettings } import akka.cluster.{ Cluster, ClusterEvent } import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.management.AkkaManagement import akka.management.cluster.bootstrap.ClusterBootstrap import akka.stream.ActorMaterializer object ClusterApp { def main(args: Array[String]): Unit = { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher val cluster = Cluster(system) system.log.info("Starting Akka Management") system.log.info("something2") // AkkaManagement(system).start() // ClusterBootstrap(system).start() system.actorOf( ClusterSingletonManager.props( Props[NoisySingleton], PoisonPill, ClusterSingletonManagerSettings(system))) Cluster(system).subscribe( system.actorOf(Props[ClusterWatcher]), ClusterEvent.InitialStateAsEvents, classOf[ClusterDomainEvent]) // add real app routes here val routes = path("hello") { get { complete( HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Hello</h1>")) } } Http().bindAndHandle(routes, "0.0.0.0", 8080) system.log.info( s"Server online at http://localhost:8080/\nPress RETURN to stop...") cluster.registerOnMemberUp(() => { system.log.info("Cluster member is up!") }) } class ClusterWatcher extends Actor with ActorLogging { val cluster = Cluster(context.system) override def receive = { case msg ⇒ log.info(s"Cluster ${cluster.selfAddress} >>> " + msg) } } }
Example 152
Source File: DemoApp.scala From reactive-lib with Apache License 2.0 | 5 votes |
package foo import akka.actor.{ Actor, ActorLogging, ActorSystem, Props } import akka.cluster.ClusterEvent.ClusterDomainEvent import akka.cluster.{ Cluster, ClusterEvent } import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.management.AkkaManagement import akka.management.cluster.bootstrap.ClusterBootstrap import akka.stream.ActorMaterializer object DemoApp extends App { implicit val system = ActorSystem("Appka") import system.log implicit val mat = ActorMaterializer() val cluster = Cluster(system) log.info(s"Started [$system], cluster.selfAddress = ${cluster.selfAddress}") log.info("something2") //#start-akka-management AkkaManagement(system).start() //#start-akka-management ClusterBootstrap(system).start() cluster.subscribe( system.actorOf(Props[ClusterWatcher]), ClusterEvent.InitialStateAsEvents, classOf[ClusterDomainEvent]) // add real app routes here val routes = path("hello") { get { complete( HttpEntity( ContentTypes.`text/html(UTF-8)`, "<h1>Hello</h1>")) } } Http().bindAndHandle(routes, "0.0.0.0", 8080) Cluster(system).registerOnMemberUp({ log.info("Cluster member is up!") }) } class ClusterWatcher extends Actor with ActorLogging { val cluster = Cluster(context.system) override def receive = { case msg ⇒ log.info(s"Cluster ${cluster.selfAddress} >>> " + msg) } }
Example 153
Source File: EventSource.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.client import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.stream.Materializer import akka.stream.alpakka.sse.scaladsl.{EventSource => SSESource} import akka.stream.scaladsl.Source import ch.epfl.bluebrain.nexus.iam.client.config.IamClientConfig import ch.epfl.bluebrain.nexus.iam.client.types.AuthToken import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import com.typesafe.scalalogging.Logger import io.circe.Decoder import io.circe.parser.decode import scala.concurrent.{ExecutionContext, Future} trait EventSource[A] { def apply[A: Decoder]( config: IamClientConfig )(implicit as: ActorSystem, mt: Materializer, ec: ExecutionContext): EventSource[A] = new EventSource[A] { private val logger = Logger[this.type] private val http = Http() private def addCredentials(request: HttpRequest)(implicit cred: Option[AuthToken]): HttpRequest = cred.map(token => request.addCredentials(OAuth2BearerToken(token.value))).getOrElse(request) private def send(request: HttpRequest)(implicit cred: Option[AuthToken]): Future[HttpResponse] = http.singleRequest(addCredentials(request)).map { resp => if (!resp.status.isSuccess()) logger.warn(s"HTTP response when performing SSE request: status = '${resp.status}'") resp } override def apply(iri: AbsoluteIri, offset: Option[String])( implicit cred: Option[AuthToken] ): Source[A, NotUsed] = SSESource(iri.asAkka, send, offset, config.sseRetryDelay).flatMapConcat { sse => decode[A](sse.data) match { case Right(ev) => Source.single(ev) case Left(err) => logger.error(s"Failed to decode admin event '$sse'", err) Source.empty } } } }
Example 154
Source File: HttpApi.scala From jwt-akka-http with MIT License | 5 votes |
package ba.codecentric import java.util.concurrent.TimeUnit import akka.actor.{ Actor, ActorLogging, Props } import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.headers.RawHeader import akka.http.scaladsl.server.{ Directive1, Route } import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import akka.pattern._ import scala.util.Failure object HttpApi { import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._ import io.circe.generic.auto._ import authentikat.jwt._ final val Name = "http-api" final val AccessTokenHeaderName = "X-Access-Token" final case class LoginRequest(username: String, password: String) private val tokenExpiryPeriodInDays = 1 private val secretKey = "super_secret_key" private val header = JwtHeader("HS256") private def login: Route = post { entity(as[LoginRequest]) { case lr @ LoginRequest("admin", "admin") => val claims = setClaims(lr.username, tokenExpiryPeriodInDays) respondWithHeader(RawHeader(AccessTokenHeaderName, JsonWebToken(header, claims, secretKey))) { complete(StatusCodes.OK) } case LoginRequest(_, _) => complete(StatusCodes.Unauthorized) } } private def securedContent: Route = get { authenticated { claims => complete(s"User: ${claims.getOrElse("user", "")} has accessed a secured content!") } } private def authenticated: Directive1[Map[String, Any]] = optionalHeaderValueByName("Authorization").flatMap { case Some(jwt) if isTokenExpired(jwt) => complete(StatusCodes.Unauthorized -> "Session expired.") case Some(jwt) if JsonWebToken.validate(jwt, secretKey) => provide(getClaims(jwt)) case _ => complete(StatusCodes.Unauthorized) } private def setClaims(username: String, expiryPeriodInDays: Long): JwtClaimsSetMap = JwtClaimsSet( Map("user" -> username, "expiredAt" -> (System.currentTimeMillis() + TimeUnit.DAYS .toMillis(expiryPeriodInDays))) ) private def getClaims(jwt: String): Map[String, String] = jwt match { case JsonWebToken(_, claims, _) => claims.asSimpleMap.getOrElse(Map.empty[String, String]) } private def isTokenExpired(jwt: String): Boolean = getClaims(jwt).get("expiredAt").exists(_.toLong < System.currentTimeMillis()) def routes: Route = login ~ securedContent def apply(host: String, port: Int) = Props(new HttpApi(host, port)) } final class HttpApi(host: String, port: Int) extends Actor with ActorLogging { import HttpApi._ import context.dispatcher private implicit val materializer: ActorMaterializer = ActorMaterializer() Http(context.system).bindAndHandle(routes, host, port).pipeTo(self) override def receive: Receive = { case ServerBinding(address) => log.info("Server successfully bound at {}:{}", address.getHostName, address.getPort) case Failure(cause) => log.error("Failed to bind server", cause) context.system.terminate() } }
Example 155
Source File: MainActor.scala From akka-http-extensions with Mozilla Public License 2.0 | 5 votes |
package org.denigma.preview import akka.actor._ import akka.http.scaladsl.Http.{IncomingConnection, ServerBinding} import akka.http.scaladsl.{Http, _} import akka.stream.ActorMaterializer import akka.stream.scaladsl.Source import org.denigma.preview.routes.Router import scala.concurrent.Future class MainActor extends Actor with ActorLogging // Routes { implicit val system = context.system implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher val server: HttpExt = Http(context.system) var serverSource: Source[IncomingConnection, Future[ServerBinding]] = null val router = new Router() override def receive: Receive = { case AppMessages.Start(config)=> val (host,port) = (config.getString("app.host") , config.getInt("app.port")) server.bindAndHandle(router.routes, host, port) log.info(s"starting server at $host:$port") case AppMessages.Stop=> onStop() } def onStop() = { log.info("Main actor has been stoped...") } override def postStop() = { onStop() } }
Example 156
Source File: Main.scala From distributed-cache-on-k8s-poc with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.event.Logging import akka.http.scaladsl.Http import akka.stream.{ ActorMaterializer, ActorMaterializerSettings } import cluster.ClusterStateInformer import com.typesafe.config.{ Config, ConfigFactory, ConfigValueFactory } import http.Route import scala.util.{ Failure, Success } import scala.concurrent.ExecutionContext.Implicits.global object Main { def main(args: Array[String]): Unit = { val config: Config = { import scala.collection.JavaConverters._ val seedNodes = ClusterSetup.seedNodes() ConfigFactory.empty() .withValue("akka.cluster.seed-nodes", ConfigValueFactory.fromIterable(seedNodes.map(seedNode => s"akka.tcp://${ClusterSetup.actorSystemName()}@$seedNode").asJava)) .withValue("akka.remote.netty.tcp.hostname", ConfigValueFactory.fromAnyRef(ClusterSetup.podName() + "." + ClusterSetup.domain())) .withValue("akka.remote.netty.tcp.port", ConfigValueFactory.fromAnyRef(ClusterSetup.remoteBindingPort())) .withFallback(ConfigFactory.load()) .resolve() } implicit val system: ActorSystem = ActorSystem(ClusterSetup.actorSystemName(), config) val logging = Logging(system, "main") implicit val mat = ActorMaterializer(materializerSettings = Some(ActorMaterializerSettings(system))) val routes = new Route(system) Http().bindAndHandle(routes.routes, "0.0.0.0", 9000).onComplete { case Success(s) => logging.info("Successfully started") case Failure(f) => logging.error(f, "Server cannot be started!!!!") } system.actorOf(ClusterStateInformer.props(), "cluster-informer") } } object ClusterSetup { def seedNodes(): Iterable[String] = sys.env.get("AKKA_SEED_NODES").map(_.split(",")).get.toIterable def domain(): String = sys.env.getOrElse("AKKA_REMOTING_BIND_DOMAIN", throw new RuntimeException("No domain found.")) def podName(): String = sys.env.getOrElse("POD_NAME", throw new RuntimeException("No podname found.")) def remoteBindingPort(): String = sys.env.getOrElse("AKKA_REMOTING_BIND_PORT", throw new RuntimeException("No port found.")) def actorSystemName(): String = sys.env.getOrElse("AKKA_ACTOR_SYSTEM_NAME", throw new RuntimeException("No actorsystem name found.")) }
Example 157
Source File: TestUtils.scala From mist with Apache License 2.0 | 5 votes |
package io.hydrosphere.mist.master import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.stream.scaladsl.Flow import com.typesafe.config.ConfigFactory import scala.concurrent.duration.{Duration, FiniteDuration} import scala.concurrent.{Await, Future, Promise} trait TestUtils { implicit class AwaitSyntax[A](f: => Future[A]) { def await: A = Await.result(f, Duration.Inf) def await(d: FiniteDuration): A = Await.result(f, d) } } object TestUtils extends TestUtils { val cfgStr = """ |context-defaults { | downtime = Inf | streaming-duration = 1 seconds | max-parallel-jobs = 20 | precreated = false | spark-conf = { } | worker-mode = "shared" | run-options = "--opt" | max-conn-failures = 5 |} | |context { | | foo { | spark-conf { | spark.master = "local[2]" | } | } |} """.stripMargin val contextSettings = { val cfg = ConfigFactory.parseString(cfgStr) ContextsSettings(cfg) } val FooContext = contextSettings.contexts.get("foo").get object MockHttpServer { import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import akka.util.Timeout import scala.concurrent.duration._ def onServer[A]( routes: Flow[HttpRequest, HttpResponse, _], f: (Http.ServerBinding) => A): Future[A] = { implicit val system = ActorSystem("mock-http-cli") implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher implicit val timeout = Timeout(1.seconds) val binding = Http().bindAndHandle(routes, "localhost", 0) val close = Promise[Http.ServerBinding] close.future .flatMap(binding => binding.unbind()) .onComplete(_ => { materializer.shutdown() Await.result(system.terminate(), Duration.Inf) }) val result = binding.flatMap(binding => { try { Future.successful(f(binding)) } catch { case e: Throwable => Future.failed(e) } finally { close.success(binding) } }) result } } }
Example 158
Source File: GreeterServer.scala From akka-grpc with Apache License 2.0 | 5 votes |
//#full-server package example.myapp.helloworld import akka.actor.ActorSystem import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.http.scaladsl.{ Http, HttpConnectionContext } import akka.stream.{ ActorMaterializer, Materializer } import com.typesafe.config.ConfigFactory import example.myapp.helloworld.grpc._ import scala.concurrent.{ ExecutionContext, Future } object GreeterServer { def main(args: Array[String]): Unit = { // Important: enable HTTP/2 in ActorSystem's config // We do it here programmatically, but you can also set it in the application.conf val conf = ConfigFactory .parseString("akka.http.server.preview.enable-http2 = on") .withFallback(ConfigFactory.defaultApplication()) val system = ActorSystem("HelloWorld", conf) new GreeterServer(system).run() // ActorSystem threads will keep the app alive until `system.terminate()` is called } } class GreeterServer(system: ActorSystem) { def run(): Future[Http.ServerBinding] = { // Akka boot up code implicit val sys: ActorSystem = system implicit val mat: Materializer = ActorMaterializer() implicit val ec: ExecutionContext = sys.dispatcher // Create service handlers val service: HttpRequest => Future[HttpResponse] = GreeterServiceHandler(new GreeterServiceImpl()) // Bind service handler servers to localhost:8080/8081 val binding = Http().bindAndHandleAsync( service, interface = "127.0.0.1", port = 8080, connectionContext = HttpConnectionContext()) // report successful binding binding.foreach { binding => println(s"gRPC server bound to: ${binding.localAddress}") } binding } } //#full-server
Example 159
Source File: PowerGreeterServer.scala From akka-grpc with Apache License 2.0 | 5 votes |
//#full-server package example.myapp.helloworld import akka.actor.ActorSystem import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.http.scaladsl.{ Http, HttpConnectionContext } import akka.stream.{ ActorMaterializer, Materializer } import example.myapp.helloworld.grpc._ import scala.concurrent.{ ExecutionContext, Future } class PowerGreeterServer(system: ActorSystem) { def run(): Future[Http.ServerBinding] = { // Akka boot up code implicit val sys: ActorSystem = system implicit val mat: Materializer = ActorMaterializer() implicit val ec: ExecutionContext = sys.dispatcher // Create service handlers val service: HttpRequest => Future[HttpResponse] = GreeterServicePowerApiHandler(new PowerGreeterServiceImpl(mat)) // Bind service handler servers to localhost:8080/8081 val binding = Http().bindAndHandleAsync( service, interface = "127.0.0.1", port = 8081, connectionContext = HttpConnectionContext()) // report successful binding binding.foreach { binding => println(s"gRPC server bound to: ${binding.localAddress}") } binding } } //#full-server
Example 160
Source File: CombinedServer.scala From akka-grpc with Apache License 2.0 | 5 votes |
package example.myapp import scala.concurrent.ExecutionContext import scala.concurrent.Future import akka.actor.ActorSystem import akka.http.scaladsl.{ Http, HttpConnectionContext } import akka.http.scaladsl.model.HttpRequest import akka.http.scaladsl.model.HttpResponse import akka.grpc.scaladsl.ServerReflection import akka.stream.ActorMaterializer import akka.stream.Materializer import com.typesafe.config.ConfigFactory import example.myapp.helloworld._ import example.myapp.echo._ import example.myapp.echo.grpc._ import example.myapp.helloworld.grpc.GreeterService //#concatOrNotFound import akka.grpc.scaladsl.ServiceHandler //#concatOrNotFound //#grpc-web import akka.grpc.scaladsl.WebHandler //#grpc-web object CombinedServer { def main(args: Array[String]): Unit = { // important to enable HTTP/2 in ActorSystem's config val conf = ConfigFactory .parseString("akka.http.server.preview.enable-http2 = on") .withFallback(ConfigFactory.defaultApplication()) implicit val sys: ActorSystem = ActorSystem("HelloWorld", conf) implicit val mat: Materializer = ActorMaterializer() implicit val ec: ExecutionContext = sys.dispatcher //#concatOrNotFound // explicit types not needed but included in example for clarity val greeterService: PartialFunction[HttpRequest, Future[HttpResponse]] = example.myapp.helloworld.grpc.GreeterServiceHandler.partial(new GreeterServiceImpl()) val echoService: PartialFunction[HttpRequest, Future[HttpResponse]] = EchoServiceHandler.partial(new EchoServiceImpl) val reflectionService = ServerReflection.partial(List(GreeterService, EchoService)) val serviceHandlers: HttpRequest => Future[HttpResponse] = ServiceHandler.concatOrNotFound(greeterService, echoService, reflectionService) Http() .bindAndHandleAsync( serviceHandlers, interface = "127.0.0.1", port = 8080, connectionContext = HttpConnectionContext()) //#concatOrNotFound .foreach { binding => println(s"gRPC server bound to: ${binding.localAddress}") } //#grpc-web val grpcWebServiceHandlers = WebHandler.grpcWebHandler(greeterService, echoService) Http() .bindAndHandleAsync( grpcWebServiceHandlers, interface = "127.0.0.1", port = 8081, connectionContext = HttpConnectionContext()) //#grpc-web .foreach { binding => println(s"gRPC-Web server bound to: ${binding.localAddress}") } } }
Example 161
Source File: ErrorReportingSpec.scala From akka-grpc with Apache License 2.0 | 5 votes |
package example.myapp.helloworld import akka.actor.{ ActorSystem, ClassicActorSystemProvider } import akka.grpc.internal.GrpcProtocolNative import akka.http.scaladsl.model.HttpEntity.{ Chunked, LastChunk } import akka.http.scaladsl.model._ import akka.http.scaladsl.model.headers.RawHeader import akka.http.scaladsl.{ Http, HttpConnectionContext } import akka.stream.ActorMaterializer import akka.stream.scaladsl.Sink import example.myapp.helloworld.grpc.{ GreeterService, GreeterServiceHandler } import io.grpc.Status import org.junit.runner.RunWith import org.scalatest.BeforeAndAfterAll import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.time.Span import org.scalatest.wordspec.AnyWordSpec import org.scalatestplus.junit.JUnitRunner import scala.concurrent.Await import scala.concurrent.duration._ @RunWith(classOf[JUnitRunner]) class ErrorReportingSpec extends AnyWordSpec with Matchers with ScalaFutures with BeforeAndAfterAll { override implicit val patienceConfig = PatienceConfig(5.seconds, Span(100, org.scalatest.time.Millis)) implicit val system: ActorSystem = ActorSystem() implicit val mat = ActorMaterializer() "A gRPC server" should { val binding = Http() .bindAndHandleAsync( GreeterServiceHandler(new GreeterServiceImpl())(system.asInstanceOf[ClassicActorSystemProvider]), interface = "127.0.0.1", port = 0, connectionContext = HttpConnectionContext()) .futureValue "respond with an 'unimplemented' gRPC error status when calling an unknown method" in { val request = HttpRequest( method = HttpMethods.POST, entity = HttpEntity.empty(GrpcProtocolNative.contentType), uri = s"http://localhost:${binding.localAddress.getPort}/${GreeterService.name}/UnknownMethod") val response = Http().singleRequest(request).futureValue response.status should be(StatusCodes.OK) allHeaders(response) should contain(RawHeader("grpc-status", Status.Code.UNIMPLEMENTED.value().toString)) } "respond with an 'invalid argument' gRPC error status when calling an method without a request body" in { val request = HttpRequest( method = HttpMethods.POST, entity = HttpEntity.empty(GrpcProtocolNative.contentType), uri = s"http://localhost:${binding.localAddress.getPort}/${GreeterService.name}/SayHello") val response = Http().singleRequest(request).futureValue response.status should be(StatusCodes.OK) allHeaders(response) should contain(RawHeader("grpc-status", Status.Code.INVALID_ARGUMENT.value().toString)) } def allHeaders(response: HttpResponse) = response.entity match { case Chunked(_, chunks) => chunks.runWith(Sink.last).futureValue match { case LastChunk(_, trailingHeaders) => response.headers ++ trailingHeaders case _ => response.headers } case _ => response.headers } } override def afterAll: Unit = Await.result(system.terminate(), 5.seconds) }
Example 162
Source File: ErrorReportingSpec.scala From akka-grpc with Apache License 2.0 | 5 votes |
package example.myapp.helloworld import akka.actor.ActorSystem import akka.grpc.internal.GrpcProtocolNative import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.model.HttpEntity.{ Chunked, LastChunk } import akka.http.scaladsl.model.headers.RawHeader import akka.stream.ActorMaterializer import akka.stream.scaladsl.Sink import example.myapp.helloworld.grpc.{ GreeterService, GreeterServiceHandlerFactory } import io.grpc.Status import org.scalatest.concurrent.ScalaFutures import org.scalatest.time.Span import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import scala.concurrent.Await import scala.concurrent.duration._ class ErrorReportingSpec extends AnyWordSpec with Matchers with ScalaFutures with BeforeAndAfterAll { implicit val sys = ActorSystem() override implicit val patienceConfig = PatienceConfig(5.seconds, Span(100, org.scalatest.time.Millis)) "A gRPC server" should { implicit val mat = ActorMaterializer() val handler = GreeterServiceHandlerFactory.create(new GreeterServiceImpl(mat), sys) val binding = { import akka.http.javadsl.{ ConnectHttp, Http } Http(sys).bindAndHandleAsync(handler, ConnectHttp.toHost("127.0.0.1", 0), mat).toCompletableFuture.get } "respond with an 'unimplemented' gRPC error status when calling an unknown method" in { val request = HttpRequest( method = HttpMethods.POST, entity = HttpEntity.empty(GrpcProtocolNative.contentType), uri = s"http://localhost:${binding.localAddress.getPort}/${GreeterService.name}/UnknownMethod") val response = Http().singleRequest(request).futureValue response.status should be(StatusCodes.OK) allHeaders(response) should contain(RawHeader("grpc-status", Status.Code.UNIMPLEMENTED.value().toString)) } "respond with an 'invalid argument' gRPC error status when calling an method without a request body" in { val request = HttpRequest( method = HttpMethods.POST, entity = HttpEntity.empty(GrpcProtocolNative.contentType), uri = s"http://localhost:${binding.localAddress.getPort}/${GreeterService.name}/SayHello") val response = Http().singleRequest(request).futureValue response.status should be(StatusCodes.OK) allHeaders(response) should contain(RawHeader("grpc-status", Status.Code.INVALID_ARGUMENT.value().toString)) } def allHeaders(response: HttpResponse) = response.entity match { case Chunked(_, chunks) => chunks.runWith(Sink.last).futureValue match { case LastChunk(_, trailingHeaders) => response.headers ++ trailingHeaders case _ => response.headers } case _ => response.headers } } override def afterAll: Unit = Await.result(sys.terminate(), 5.seconds) }
Example 163
Source File: MutableServiceDiscovery.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.scaladsl.tools import java.net.InetSocketAddress import akka.discovery.Lookup import akka.discovery.ServiceDiscovery import akka.discovery.ServiceDiscovery.Resolved import akka.discovery.ServiceDiscovery.ResolvedTarget import akka.http.scaladsl.Http import scala.concurrent.Future import scala.concurrent.duration.FiniteDuration final class MutableServiceDiscovery(targets: List[InetSocketAddress]) extends ServiceDiscovery { var services: Future[Resolved] = _ setServices(targets) def setServices(targets: List[InetSocketAddress]): Unit = services = Future.successful( Resolved( "greeter", targets.map(target => ResolvedTarget(target.getHostString, Some(target.getPort), Some(target.getAddress))))) override def lookup(query: Lookup, resolveTimeout: FiniteDuration): Future[Resolved] = { require(query.serviceName == "greeter") services } } object MutableServiceDiscovery { def apply(targets: List[Http.ServerBinding]) = new MutableServiceDiscovery(targets.map(_.localAddress)) }
Example 164
Source File: AkkaHttpClient.scala From graphql-gateway with Apache License 2.0 | 5 votes |
package sangria.gateway.http.client import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpHeader.ParsingResult import akka.http.scaladsl.model._ import akka.http.scaladsl.model.Uri.Query import akka.http.scaladsl.model.headers.Location import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.Materializer import akka.util.ByteString import sangria.gateway.util.Logging import scala.concurrent.{ExecutionContext, Future} class AkkaHttpClient(implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext) extends HttpClient with Logging { import AkkaHttpClient._ import HttpClient._ override def request(method: Method.Value, url: String, queryParams: Seq[(String, String)] = Seq.empty, headers: Seq[(String, String)] = Seq.empty, body: Option[(String, String)] = None) = { val m = mapMethod(method) val query = Query(queryParams: _*) val hs = headers.map(header) val uri = Uri(url).withQuery(query) val entity = body.fold(HttpEntity.Empty){case (tpe, content) ⇒ HttpEntity(contentType(tpe), ByteString(content))} val request = HttpRequest(m, uri, hs.toVector, entity) val client = Http().singleRequest(_: HttpRequest) val richClient = RichHttpClient.httpClientWithRedirect(client) logger.debug(s"Http request: ${m.value} $url") richClient(request).map(AkkaHttpResponse(m, url, _)) } override def oauthClientCredentials(url: String, clientId: String, clientSecret: String, scopes: Seq[String]): Future[HttpResponse] = throw new IllegalStateException("Not yet implemented, please use play implementation.") private def contentType(str: String) = ContentType.parse(str).fold( errors ⇒ throw ClientError(s"Invalid content type '$str'", errors.map(_.detail)), identity) private def header(nameValue: (String, String)) = HttpHeader.parse(nameValue._1, nameValue._2) match { case ParsingResult.Ok(_, errors) if errors.nonEmpty ⇒ throw ClientError(s"Invalid header '${nameValue._1}'", errors.map(_.detail)) case ParsingResult.Error(error) ⇒ throw ClientError(s"Invalid header '${nameValue._1}'", Seq(error.detail)) case ParsingResult.Ok(h, _) ⇒ h } def mapMethod(method: Method.Value) = method match { case Method.Get ⇒ HttpMethods.GET case Method.Post ⇒ HttpMethods.POST } object RichHttpClient { import akka.http.scaladsl.model.HttpResponse type HttpClient = HttpRequest ⇒ Future[HttpResponse] def redirectOrResult(client: HttpClient)(response: HttpResponse): Future[HttpResponse] = response.status match { case StatusCodes.Found | StatusCodes.MovedPermanently | StatusCodes.SeeOther ⇒ val newUri = response.header[Location].get.uri // Always make sure you consume the response entity streams (of type Source[ByteString,Unit]) by for example connecting it to a Sink (for example response.discardEntityBytes() if you don’t care about the response entity), since otherwise Akka HTTP (and the underlying Streams infrastructure) will understand the lack of entity consumption as a back-pressure signal and stop reading from the underlying TCP connection! response.discardEntityBytes() logger.debug(s"Http redirect: ${HttpMethods.GET.value} $newUri") client(HttpRequest(method = HttpMethods.GET, uri = newUri)) case _ ⇒ Future.successful(response) } def httpClientWithRedirect(client: HttpClient): HttpClient = { lazy val redirectingClient: HttpClient = req ⇒ client(req).flatMap(redirectOrResult(redirectingClient)) // recurse to support multiple redirects redirectingClient } } case class ClientError(message: String, errors: Seq[String]) extends Exception(message + ":\n" + errors.map(" * " + _).mkString("\n")) } object AkkaHttpClient { case class AkkaHttpResponse(method: HttpMethod, url: String, response: HttpResponse)(implicit mat: Materializer) extends HttpClient.HttpResponse { def asString = Unmarshal(response).to[String] def statusCode = response.status.intValue() def isSuccessful = response.status.isSuccess() def debugInfo = s"${method.value} $url" } }
Example 165
Source File: GatewayServer.scala From graphql-gateway with Apache License 2.0 | 5 votes |
package sangria.gateway.http import language.postfixOps import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import play.api.libs.ws.ahc.StandaloneAhcWSClient import play.shaded.ahc.org.asynchttpclient.DefaultAsyncHttpClient import sangria.gateway.AppConfig import sangria.gateway.http.client.PlayHttpClient import sangria.gateway.schema.materializer.GatewayMaterializer import sangria.gateway.schema.materializer.directive._ import sangria.gateway.schema.{ReloadableSchemaProvider, StaticSchemaProvider} import sangria.gateway.util.Logging import scala.util.{Failure, Success} import scala.util.control.NonFatal class GatewayServer extends Logging { implicit val system: ActorSystem = ActorSystem("sangria-server") implicit val materializer: ActorMaterializer = ActorMaterializer() import system.dispatcher //val client = new AkkaHttpClient val client = new PlayHttpClient(new StandaloneAhcWSClient(new DefaultAsyncHttpClient)) val directiveProviders = Map( "http" → new HttpDirectiveProvider(client), "graphql" → new GraphQLDirectiveProvider, "faker" → new FakerDirectiveProvider, "basic" → new BasicDirectiveProvider) def startup(config: AppConfig) = try { val gatewayMaterializer = new GatewayMaterializer(filterDirectives(config, directiveProviders)) val schemaProvider = if (config.watch.enabled) new ReloadableSchemaProvider(config, client, gatewayMaterializer) else new StaticSchemaProvider(config, client, gatewayMaterializer) schemaProvider.schemaInfo // trigger initial schema load at startup val routing = new GraphQLRouting(config, schemaProvider) Http().bindAndHandle(routing.route, config.bindHost, config.port).andThen { case Success(_) ⇒ logger.info(s"Server started on ${config.bindHost}:${config.port}") if (config.watch.enabled) logger.info(s"Watching files at following path: ${config.watch.allFiles.mkString(", ")}. Looking for files: ${config.watch.allGlobs.mkString(", ")}.") case Failure(_) ⇒ shutdown() } } catch { case NonFatal(error) ⇒ logger.error("Error during server startup", error) shutdown() } def shutdown(): Unit = { logger.info("Shutting down server") system.terminate() } private def filterDirectives(config: AppConfig, providers: Map[String, DirectiveProvider]) = { val includes = config.allIncludeDirectives.fold(Set.empty[String])(_.toSet) val excludes = config.allExcludeDirectives.fold(Set.empty[String])(_.toSet) val initial = providers.toVector val withIncludes = if (config.allIncludeDirectives.nonEmpty) initial.filter(dp ⇒ includes contains dp._1) else initial val withExcludes = if (config.allExcludeDirectives.nonEmpty) withIncludes.filterNot(dp ⇒ excludes contains dp._1) else withIncludes withExcludes.map(_._2) } }
Example 166
Source File: ChatClient.scala From akka-http-scala-js-websocket-chat with MIT License | 5 votes |
package example.akkawschat.cli import scala.concurrent.Future import akka.actor.ActorSystem import akka.stream.scaladsl.{ Keep, Source, Sink, Flow } import akka.http.scaladsl.Http import akka.http.scaladsl.model.Uri import akka.http.scaladsl.model.ws._ import upickle.default._ import shared.Protocol object ChatClient { def connect[T](endpoint: Uri, handler: Flow[Protocol.Message, String, T])(implicit system: ActorSystem): Future[T] = { val wsFlow: Flow[Message, Message, T] = Flow[Message] .collect { case TextMessage.Strict(msg) ⇒ read[Protocol.Message](msg) } .viaMat(handler)(Keep.right) .map(TextMessage(_)) val (fut, t) = Http().singleWebSocketRequest(WebSocketRequest(endpoint), wsFlow) fut.map { case v: ValidUpgrade ⇒ t case InvalidUpgradeResponse(response, cause) ⇒ throw new RuntimeException(s"Connection to chat at $endpoint failed with $cause") }(system.dispatcher) } def connect[T](endpoint: Uri, in: Sink[Protocol.Message, Any], out: Source[String, Any])(implicit system: ActorSystem): Future[Unit] = connect(endpoint, Flow.fromSinkAndSource(in, out)).map(_ ⇒ ())(system.dispatcher) def connect[T](endpoint: Uri, onMessage: Protocol.Message ⇒ Unit, out: Source[String, Any])(implicit system: ActorSystem): Future[Unit] = connect(endpoint, Sink.foreach(onMessage), out) }
Example 167
Source File: ChatBackendMain.scala From akka-http-scala-js-websocket-chat with MIT License | 5 votes |
package example.akkawschat import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import scala.util.{ Success, Failure } object ChatBackendMain extends App { implicit val system = ActorSystem() import system.dispatcher val config = system.settings.config val interface = config.getString("app.interface") val port = config.getInt("app.port") val service = new Webservice val binding = Http().bindAndHandle(service.route, interface, port) binding.onComplete { case Success(binding) ⇒ val localAddress = binding.localAddress println(s"Server is listening on ${localAddress.getHostName}:${localAddress.getPort}") case Failure(e) ⇒ println(s"Binding failed with ${e.getMessage}") system.terminate() } }
Example 168
Source File: WebResources.scala From NSDb with Apache License 2.0 | 5 votes |
package io.radicalbit.nsdb.web import java.util.concurrent.TimeUnit import akka.actor.ActorRef import akka.event.LoggingAdapter import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.stream.ActorMaterializer import akka.util.Timeout import com.typesafe.config.Config import io.radicalbit.nsdb.common.configuration.NSDbConfig.HighLevel._ import io.radicalbit.nsdb.security.NsdbSecurity import org.json4s.Formats import scala.concurrent.duration._ import scala.concurrent.{Await, Future} import scala.util.{Failure, Success} trait WebResources extends WsResources with SSLSupport { this: NsdbSecurity => import CORSSupport._ import VersionHeader._ implicit def formats: Formats def config: Config implicit lazy val materializer = ActorMaterializer() implicit lazy val dispatcher = system.dispatcher implicit lazy val httpTimeout: Timeout = Timeout(config.getDuration("nsdb.http-endpoint.timeout", TimeUnit.SECONDS), TimeUnit.SECONDS) def initWebEndpoint(writeCoordinator: ActorRef, readCoordinator: ActorRef, metadataCoordinator: ActorRef, publisher: ActorRef)(implicit logger: LoggingAdapter) = authProvider match { case Success(provider) => val api: Route = wsResources(publisher, provider) ~ new ApiResources(publisher, readCoordinator, writeCoordinator, metadataCoordinator, provider).apiResources(config) val httpExt = akka.http.scaladsl.Http() val http: Future[Http.ServerBinding] = if (isSSLEnabled) { val interface = config.getString(HttpInterface) val port = config.getInt(HttpsPort) logger.info(s"Cluster Apis started with https protocol at interface $interface on port $port") httpExt.bindAndHandle(withCors(withNSDbVersion(api)), interface, port, connectionContext = serverContext) } else { val interface = config.getString(HttpInterface) val port = config.getInt(HttpPort) logger.info(s"Cluster Apis started with http protocol at interface $interface and port $port") httpExt.bindAndHandle(withCors(withNSDbVersion(api)), interface, port) } scala.sys.addShutdownHook { http .flatMap(_.unbind()) .onComplete { _ => system.terminate() } Await.result(system.whenTerminated, 60 seconds) } case Failure(ex) => logger.error("error on loading authorization provider", ex) System.exit(1) } }
Example 169
Source File: WebSocketClient.scala From NSDb with Apache License 2.0 | 5 votes |
package io.radicalbit.nsdb.minicluster.ws import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.model.ws._ import akka.stream.scaladsl._ import akka.stream.{ActorMaterializer, OverflowStrategy} import akka.{Done, NotUsed} import com.typesafe.scalalogging.LazyLogging import scala.collection.mutable.ListBuffer import scala.concurrent.Future class WebSocketClient(host: String, port: Int) extends LazyLogging with SynchronizedBuffer[Message] { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() import system.dispatcher val req = WebSocketRequest(uri = s"ws://$host:$port/ws-stream") val webSocketFlow = Http().webSocketClientFlow(req) val messageSource: Source[Message, ActorRef] = Source.actorRef[TextMessage.Strict](bufferSize = 10, OverflowStrategy.fail) val messageSink: Sink[Message, NotUsed] = Flow[Message] .map { message => logger.debug(s"Received text message: [$message]") accumulate(message) } .to(Sink.ignore) val ((ws, upgradeResponse), closed) = messageSource .viaMat(webSocketFlow)(Keep.both) .toMat(messageSink)(Keep.both) .run() val connected = upgradeResponse.flatMap { upgrade => if (upgrade.response.status == StatusCodes.SwitchingProtocols) { Future.successful(Done) } else { throw new RuntimeException(s"Connection failed: ${upgrade.response.status}") } } def send(msg: String): Unit = { ws ! TextMessage.Strict(msg) } def receivedBuffer(): ListBuffer[Message] = buffer def subscribe(db: String, namespace: String, metric: String): Unit = ws ! TextMessage.Strict( s"""{"db":"$db","namespace":"$namespace","metric":"$metric","queryString":"select * from $metric limit 1"}""") }
Example 170
Source File: AkkaHttpServerTemplate.scala From akka-http-circe-json-template with Apache License 2.0 | 5 votes |
package com.vitorsvieira.http.server import akka.http.scaladsl.Http import akka.http.scaladsl.Http.{ IncomingConnection, ServerBinding } import akka.http.scaladsl.server.Route._ import akka.stream.scaladsl.{ Sink, Source } import com.vitorsvieira.http.config.ServerSettingsTemplate import com.vitorsvieira.http.routes.AkkaHttpRoutesTemplate import scala.concurrent.Future import scala.io.StdIn object AkkaHttpServerTemplate extends App { import ServerSettingsTemplate._ val server: Source[IncomingConnection, Future[ServerBinding]] = Http(actorSystem).bind(httpInterface, httpPort) log.info(s"\nAkka HTTP Server - Version ${actorSystem.settings.ConfigVersion} - running at http://$httpInterface:$httpPort/") val handler: Future[ServerBinding] = server .to( Sink.foreach { connection ⇒ connection.handleWithAsyncHandler(asyncHandler(AkkaHttpRoutesTemplate.availableRoutes)) } ) .run() handler.failed.foreach { case ex: Exception ⇒ log.error(ex, "Failed to bind to {}:{}", httpInterface, httpPort) } StdIn.readLine(s"\nPress RETURN to stop...") handler .flatMap(binding ⇒ binding.unbind()) .onComplete(_ ⇒ actorSystem.terminate()) }
Example 171
Source File: ClientFlowHttpsSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.httpclient import java.io.InputStream import java.security.{KeyStore, SecureRandom} import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory} import akka.actor.ActorSystem import akka.http.scaladsl.model._ import akka.http.scaladsl.{ConnectionContext, Http} import akka.stream.ActorMaterializer import akka.stream.scaladsl.{Sink, Source} import akka.util.ByteString import com.typesafe.config.ConfigFactory import org.scalatest.{AsyncFlatSpec, BeforeAndAfterAll, Matchers} import org.squbs.resolver.ResolverRegistry import org.squbs.testkit.Timeouts._ import scala.concurrent.{Await, Future} import scala.util.{Success, Try} object ClientFlowHttpsSpec { val config = ConfigFactory.parseString( """ |helloHttps { | type = squbs.httpclient | akka.ssl-config.loose.disableHostnameVerification = true |} """.stripMargin) implicit val system = ActorSystem("ClientFlowHttpsSpec", config) implicit val materializer = ActorMaterializer() ResolverRegistry(system).register[HttpEndpoint]("LocalhostHttpsEndpointResolver") { (name, _) => name match { case "helloHttps" => Some(HttpEndpoint(s"https://localhost:$port", Some(sslContext("exampletrust.jks", "changeit")), None)) case _ => None } } import akka.http.scaladsl.server.Directives._ import system.dispatcher val route = path("hello") { get { complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Hello World!")) } } val serverBinding = Await.result(Http().bindAndHandle(route, "localhost", 0, ConnectionContext.https(sslContext("example.com.jks", "changeit"))), awaitMax) val port = serverBinding.localAddress.getPort } class ClientFlowHttpsSpec extends AsyncFlatSpec with Matchers with BeforeAndAfterAll { import ClientFlowHttpsSpec._ override def afterAll: Unit = { serverBinding.unbind() map {_ => system.terminate()} } it should "make a call to Hello Service" in { val clientFlow = ClientFlow[Int]("helloHttps") val responseFuture: Future[(Try[HttpResponse], Int)] = Source.single(HttpRequest(uri = "/hello") -> 42) .via(clientFlow) .runWith(Sink.head) val (Success(response), _) = Await.result(responseFuture, awaitMax) response.status should be (StatusCodes.OK) val entity = response.entity.dataBytes.runFold(ByteString(""))(_ ++ _) map(_.utf8String) entity map { e => e shouldEqual "Hello World!" } } }
Example 172
Source File: ClientFlowIdleTimeoutSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.httpclient import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.stream.ActorMaterializer import akka.stream.scaladsl.{Sink, Source, TcpIdleTimeoutException} import com.typesafe.config.ConfigFactory import org.scalatest.{AsyncFlatSpec, BeforeAndAfterAll, Matchers} import org.squbs.resolver.ResolverRegistry import org.squbs.testkit.Timeouts.awaitMax import scala.concurrent.{Await, Promise} import scala.util.{Failure, Success} object ClientFlowIdleTimeoutSpec { val config = ConfigFactory.parseString( """ |akka { | loggers = [ | "akka.event.Logging$DefaultLogger" | ] | | loglevel = "DEBUG" | | http { | server { | idle-timeout = 240 s | request-timeout = 120 s | } | | client.idle-timeout = 1 s | | host-connection-pool.max-retries = 0 | } |} """.stripMargin) implicit val system = ActorSystem("ClientFlowIdleTimeoutSpec", config) implicit val materializer = ActorMaterializer() ResolverRegistry(system).register[HttpEndpoint]("LocalhostEndpointResolver") { (svcName, _) => svcName match { case "slow" => Some(HttpEndpoint(s"http://localhost:$port")) case _ => None }} import akka.http.scaladsl.server.Directives._ import system.dispatcher val route = path("slow") { get { val promise = Promise[String] // Never completing the promise onComplete(promise.future) { case Success(value) => complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Slow...!")) case Failure(ex) => complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Slow failed...!")) } } } val serverBinding = Await.result(Http().bindAndHandle(route, "localhost", 0), awaitMax) val port = serverBinding.localAddress.getPort } class ClientFlowIdleTimeoutSpec extends AsyncFlatSpec with Matchers with BeforeAndAfterAll { import ClientFlowIdleTimeoutSpec._ override def afterAll: Unit = { serverBinding.unbind() map {_ => system.terminate()} } it should "drop the connection after idle-timeout and resume the stream with new connections" in { val clientFlow = ClientFlow[Int]("slow") val result = Source(1 to 10) .map(HttpRequest(uri = "/slow") -> _) .via(clientFlow) .runWith(Sink.seq) result map { r => val failures = r.map(_._1).filter(_.isFailure).map(_.failed) failures should have size 10 failures.forall(_.get.isInstanceOf[TcpIdleTimeoutException]) shouldBe true r.map(_._2) should contain theSameElementsAs(1 to 10) } } }
Example 173
Source File: package.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, HttpResponse, Uri} import akka.stream.ActorMaterializer import akka.util.ByteString import scala.concurrent.Future package object testkit { case object TestPing case object TestPong def entityAsString(uri: String)(implicit am: ActorMaterializer, system: ActorSystem): Future[String] = { import system.dispatcher get(uri) flatMap extractEntityAsString } def get(uri: String)(implicit am: ActorMaterializer, system: ActorSystem): Future[HttpResponse] = { Http().singleRequest(HttpRequest(uri = Uri(uri))) } def extractEntityAsString(response: HttpResponse) (implicit am: ActorMaterializer, system: ActorSystem): Future[String] = { import system.dispatcher response.entity.dataBytes.runFold(ByteString(""))(_ ++ _) map(_.utf8String) } }
Example 174
Source File: PerpetualStreamMergeHubJSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.stream import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, Uri} import akka.pattern.ask import akka.stream.ActorMaterializer import akka.testkit.TestKit import com.typesafe.config.ConfigFactory import org.scalatest.{FlatSpecLike, Matchers} import org.squbs.unicomplex.Timeouts.{awaitMax, _} import org.squbs.unicomplex._ import scala.collection.mutable import scala.concurrent.Await object PerpetualStreamMergeHubJSpec { val dummyJarsDir = getClass.getClassLoader.getResource("classpaths").getPath val classPaths = Array("JavaPerpetualStreamMergeHubSpec") map (dummyJarsDir + "/" + _) val config = ConfigFactory.parseString( s""" |squbs { | actorsystem-name = JavaPerpetualStreamMergeHubSpec | ${JMX.prefixConfig} = true |} |default-listener.bind-port = 0 """.stripMargin ) val boot = UnicomplexBoot(config) .createUsing { (name, config) => ActorSystem(name, config) } .scanComponents(classPaths) .start() } class PerpetualStreamMergeHubJSpec extends TestKit(PerpetualStreamMergeHubJSpec.boot.actorSystem) with FlatSpecLike with Matchers { val portBindings = Await.result((Unicomplex(system).uniActor ? PortBindings).mapTo[Map[String, Int]], awaitMax) val psActorName = "/user/JavaPerpetualStreamMergeHubSpec/perpetualStreamWithMergeHub" val actorRef = Await.result((system.actorSelection(psActorName) ? RetrieveMyMessageStorageActorRef).mapTo[ActorRef], awaitMax) val port = portBindings("default-listener") it should "connect streams with mergehub" in { implicit val ac = ActorMaterializer() Http().singleRequest(HttpRequest(uri = Uri(s"http://127.0.0.1:$port/mergehub"), entity = "10")) Http().singleRequest(HttpRequest(uri = Uri(s"http://127.0.0.1:$port/mergehub"), entity = "11")) awaitAssert { val messages = Await.result((actorRef ? RetrieveMyMessages).mapTo[mutable.Set[MyMessage]], awaitMax) messages should have size 2 messages should contain(MyMessage(10)) messages should contain(MyMessage(11)) } } }
Example 175
Source File: CubeActorErrorStatesSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.unicomplex import javax.management.ObjectName import javax.management.openmbean.CompositeData import akka.actor.{Actor, ActorSystem} import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, Uri} import akka.pattern._ import akka.stream.ActorMaterializer import akka.testkit.{ImplicitSender, TestKit} import com.typesafe.config.ConfigFactory import org.scalatest.OptionValues._ import org.scalatest.{BeforeAndAfterAll, FlatSpecLike, Matchers} import org.squbs.lifecycle.GracefulStop import org.squbs.unicomplex.Timeouts._ import scala.concurrent.Await object CubeActorErrorStatesSpec{ val classPaths = Array(getClass.getClassLoader.getResource("classpaths/CubeActorErrorStates").getPath) val config = ConfigFactory.parseString( s""" |default-listener.bind-port = 0 |squbs { | actorsystem-name = cubeActorErrorStatesSpec | ${JMX.prefixConfig} = true |} """.stripMargin ) val boot = UnicomplexBoot(config) .createUsing {(name, config) => ActorSystem(name, config)} .scanComponents(classPaths) .initExtensions.start() } class CubeActorErrorStatesSpec extends TestKit(CubeActorErrorStatesSpec.boot.actorSystem) with FlatSpecLike with Matchers with ImplicitSender with BeforeAndAfterAll { val portBindings = Await.result((Unicomplex(system).uniActor ? PortBindings).mapTo[Map[String, Int]], awaitMax) val port = portBindings("default-listener") implicit val am = ActorMaterializer() override def afterAll(): Unit = { Unicomplex(system).uniActor ! GracefulStop } "Route" should "handle request with empty web-context" in { Http().singleRequest(HttpRequest(uri = Uri(s"http://127.0.0.1:$port/test2?msg=1"))) Thread.sleep(100) Http().singleRequest(HttpRequest(uri = Uri(s"http://127.0.0.1:$port/test1?msg=1"))) Thread.sleep(100) Http().singleRequest(HttpRequest(uri = Uri(s"http://127.0.0.1:$port/test1?msg=2"))) Thread.sleep(1000) // wait the agent get refreshed import org.squbs.unicomplex.JMX._ val errorStates = get(new ObjectName(prefix(system) + cubeStateName + "CubeActorErrorStates"), "ActorErrorStates") .asInstanceOf[Array[CompositeData]] errorStates should have length 2 val state1 = errorStates.find(_.get("actorPath") == "/user/CubeActorErrorStates/test1-CubeActorTest-handler").value state1.get("errorCount") shouldBe 2 state1.get("latestException").asInstanceOf[String] should include ("test1:2") val state2 = errorStates.find(_.get("actorPath") == "/user/CubeActorErrorStates/test2-CubeActorTest-handler").value state2.get("errorCount") shouldBe 1 state2.get("latestException").asInstanceOf[String] should include ("test2:1") } } class CubeActorTest extends Actor { override def receive: Receive = { case r: HttpRequest => val msg = r.uri.query().get("msg").getOrElse("") throw new RuntimeException(s"${r.uri.path}:$msg") } }
Example 176
Source File: StreamTestSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.unicomplex import akka.actor._ import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.pattern._ import akka.stream.ActorMaterializer import akka.stream.scaladsl.FileIO import akka.testkit.{ImplicitSender, TestKit} import com.typesafe.config.ConfigFactory import org.scalatest.concurrent.Waiters import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike} import org.squbs.lifecycle.GracefulStop import org.squbs.unicomplex.Timeouts._ import scala.concurrent.Await object StreamTestSpec { val dummyJarsDir = getClass.getClassLoader.getResource("classpaths").getPath val classPaths = Array( "StreamCube", "StreamSvc" ) map (dummyJarsDir + "/" + _) val config = ConfigFactory.parseString( s""" |squbs { | actorsystem-name = StreamTestSpec | ${JMX.prefixConfig} = true |} |default-listener.bind-port = 0 """.stripMargin ) val boot = UnicomplexBoot(config) .createUsing {(name, config) => ActorSystem(name, config)} .scanComponents(classPaths) .initExtensions .start() } class StreamTestSpec extends TestKit(StreamTestSpec.boot.actorSystem) with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfterAll with Waiters { implicit val am = ActorMaterializer() import system.dispatcher val portBindings = Await.result((Unicomplex(system).uniActor ? PortBindings).mapTo[Map[String, Int]], awaitMax) val port = portBindings("default-listener") override def afterAll(): Unit = { Unicomplex(system).uniActor ! GracefulStop } "UniComplex" must { "upload file with correct parts" in { val filePath = StreamTestSpec.getClass.getResource("/classpaths/StreamSvc/dummy.txt").getPath val file = new java.io.File(filePath) require(file.exists() && file.canRead) val chunkSize = 8192 val responseF = Http().singleRequest(HttpRequest(HttpMethods.POST, uri = s"http://127.0.0.1:$port/streamsvc/file-upload", entity = HttpEntity(MediaTypes.`application/octet-stream`, FileIO.fromPath(file.toPath, chunkSize)))) val actualResponseEntity = Await.result(responseF flatMap extractEntityAsString, awaitMax) val expectedNumberOfChunks = Math.ceil(file.length.toDouble / chunkSize).toInt val expectedResponseEntity = s"Chunk Count: $expectedNumberOfChunks ByteCount: ${file.length}" actualResponseEntity should be (expectedResponseEntity) } } }
Example 177
Source File: package.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs import java.net.InetSocketAddress import java.nio.channels.ServerSocketChannel import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.stream.ActorMaterializer import akka.util.ByteString import scala.concurrent.Future package object unicomplex { // Remove this once Akka-Http exposes this test utility. def temporaryServerAddress(interface: String = "127.0.0.1"): InetSocketAddress = { val serverSocket = ServerSocketChannel.open() try { serverSocket.socket.bind(new InetSocketAddress(interface, 0)) val port = serverSocket.socket.getLocalPort new InetSocketAddress(interface, port) } finally serverSocket.close() } def temporaryServerHostnameAndPort(interface: String = "127.0.0.1"): (InetSocketAddress, String, Int) = { val socketAddress = temporaryServerAddress(interface) (socketAddress, socketAddress.getHostName, socketAddress.getPort) } def extractEntityAsString(response: HttpResponse) (implicit am: ActorMaterializer, system: ActorSystem): Future[String] = { import system.dispatcher response.entity.dataBytes.runFold(ByteString(""))(_ ++ _) map(_.utf8String) } def entityAsString(uri: String)(implicit am: ActorMaterializer, system: ActorSystem): Future[String] = { import system.dispatcher get(uri) flatMap extractEntityAsString } def entityAsStringWithHeaders(uri: String)(implicit am: ActorMaterializer, system: ActorSystem): Future[(String, Seq[HttpHeader])] = { import system.dispatcher get(uri) flatMap( response => extractEntityAsString(response) map((_, response.headers))) } def entityAsInt(uri: String)(implicit am: ActorMaterializer, system: ActorSystem): Future[Int] = { import system.dispatcher entityAsString(uri) map (s => s.toInt) } def get(uri: String)(implicit am: ActorMaterializer, system: ActorSystem): Future[HttpResponse] = { Http().singleRequest(HttpRequest(uri = Uri(uri))) } def post(uri: String, e: RequestEntity)(implicit am: ActorMaterializer, system: ActorSystem): Future[HttpResponse] = { Http().singleRequest(HttpRequest(method = HttpMethods.POST, uri = Uri(uri), entity = e)) } def put(uri: String)(implicit am: ActorMaterializer, system: ActorSystem): Future[HttpResponse] = { Http().singleRequest(HttpRequest(method = HttpMethods.PUT, uri = Uri(uri))) } }
Example 178
Source File: MetronomeFacade.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome.integrationtest.utils import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding.{Get, Post} import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpRequest, HttpResponse} import akka.stream.Materializer import com.mesosphere.utils.http.RestResult import scala.concurrent.Await.result import scala.concurrent.Future import scala.concurrent.duration.{FiniteDuration, _} class MetronomeFacade(val url: String, implicit val waitTime: FiniteDuration = 30.seconds)(implicit val system: ActorSystem, mat: Materializer ) { import scala.concurrent.ExecutionContext.Implicits.global //info -------------------------------------------------- def info(): RestResult[HttpResponse] = { result(request(Get(s"$url/info")), waitTime) } def createJob(jobDef: String): RestResult[HttpResponse] = { val e = HttpEntity(ContentTypes.`application/json`, jobDef) result(request(Post(s"$url/v1/jobs", e)), waitTime) } def startRun(jobId: String): RestResult[HttpResponse] = { val e = HttpEntity(ContentTypes.`application/json`, "") result(request(Post(s"$url/v1/jobs/${jobId}/runs", e)), waitTime) } def getJob(jobId: String): RestResult[HttpResponse] = { result(request(Get(s"$url/v1/jobs/${jobId}")), waitTime) } def getJobs(): RestResult[HttpResponse] = { result(request(Get(s"$url/v1/jobs")), waitTime) } def getRuns(jobId: String): RestResult[HttpResponse] = { result(request(Get(s"$url/v1/jobs/${jobId}/runs")), waitTime) } private[this] def request(request: HttpRequest): Future[RestResult[HttpResponse]] = { Http(system).singleRequest(request).flatMap { response => response.entity.toStrict(waitTime).map(_.data.decodeString("utf-8")).map(RestResult(response, _)) } } }
Example 179
Source File: AkkaHttpClient.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.akkahttp import akka.actor.ActorSystem import akka.event.LoggingAdapter import akka.http.scaladsl.model.ws.{Message, WebSocketRequest, WebSocketUpgradeResponse} import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.server.{ExceptionHandler, RejectionHandler, Route, RoutingLog} import akka.http.scaladsl.settings.{ClientConnectionSettings, ConnectionPoolSettings, ParserSettings, RoutingSettings} import akka.http.scaladsl.{Http, HttpsConnectionContext} import akka.stream.Materializer import akka.stream.scaladsl.Flow import scala.concurrent.{ExecutionContext, ExecutionContextExecutor, Future} trait AkkaHttpClient { def singleRequest( request: HttpRequest, settings: ConnectionPoolSettings ): Future[HttpResponse] def singleWebsocketRequest[WS_RESULT]( request: WebSocketRequest, clientFlow: Flow[Message, Message, WS_RESULT], settings: ClientConnectionSettings )(implicit ec: ExecutionContext, mat: Materializer): Future[(WebSocketUpgradeResponse, WS_RESULT)] } object AkkaHttpClient { def default( system: ActorSystem, connectionContext: Option[HttpsConnectionContext], customLog: Option[LoggingAdapter] ): AkkaHttpClient = new AkkaHttpClient { private val http = Http()(system) override def singleRequest( request: HttpRequest, settings: ConnectionPoolSettings ): Future[HttpResponse] = { http.singleRequest( request, connectionContext.getOrElse(http.defaultClientHttpsContext), settings, customLog.getOrElse(system.log) ) } override def singleWebsocketRequest[WS_RESULT]( request: WebSocketRequest, clientFlow: Flow[Message, Message, WS_RESULT], settings: ClientConnectionSettings )(implicit ec: ExecutionContext, mat: Materializer): Future[(WebSocketUpgradeResponse, WS_RESULT)] = { val (wsResponse, wsResult) = http.singleWebSocketRequest( request, clientFlow, connectionContext.getOrElse(http.defaultClientHttpsContext), None, settings, customLog.getOrElse(system.log) ) wsResponse.map((_, wsResult)) } } def stubFromAsyncHandler(run: HttpRequest => Future[HttpResponse]): AkkaHttpClient = new AkkaHttpClient { def singleRequest(request: HttpRequest, settings: ConnectionPoolSettings): Future[HttpResponse] = run(request) override def singleWebsocketRequest[WS_RESULT]( request: WebSocketRequest, clientFlow: Flow[Message, Message, WS_RESULT], settings: ClientConnectionSettings )(implicit ec: ExecutionContext, mat: Materializer): Future[(WebSocketUpgradeResponse, WS_RESULT)] = Future.failed(new RuntimeException("Websockets are not supported")) } def stubFromRoute(route: Route)(implicit routingSettings: RoutingSettings, parserSettings: ParserSettings, materializer: Materializer, routingLog: RoutingLog, executionContext: ExecutionContextExecutor = null, rejectionHandler: RejectionHandler = RejectionHandler.default, exceptionHandler: ExceptionHandler = null ): AkkaHttpClient = stubFromAsyncHandler(Route.asyncHandler(route)) }
Example 180
Source File: CorsBenchmark.scala From akka-http-cors with Apache License 2.0 | 5 votes |
package ch.megard.akka.http.cors import java.util.concurrent.TimeUnit import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.model.headers.{Origin, `Access-Control-Request-Method`} import akka.http.scaladsl.model.{HttpMethods, HttpRequest} import akka.http.scaladsl.server.Directives import akka.http.scaladsl.unmarshalling.Unmarshal import ch.megard.akka.http.cors.scaladsl.CorsDirectives import ch.megard.akka.http.cors.scaladsl.settings.CorsSettings import com.typesafe.config.ConfigFactory import org.openjdk.jmh.annotations._ import scala.concurrent.duration._ import scala.concurrent.{Await, ExecutionContext} @State(Scope.Benchmark) @OutputTimeUnit(TimeUnit.SECONDS) @BenchmarkMode(Array(Mode.Throughput)) class CorsBenchmark extends Directives with CorsDirectives { private val config = ConfigFactory.parseString("akka.loglevel = ERROR").withFallback(ConfigFactory.load()) implicit private val system: ActorSystem = ActorSystem("CorsBenchmark", config) implicit private val ec: ExecutionContext = scala.concurrent.ExecutionContext.global private val http = Http() private val corsSettings = CorsSettings.default private var binding: ServerBinding = _ private var request: HttpRequest = _ private var requestCors: HttpRequest = _ private var requestPreflight: HttpRequest = _ @Setup def setup(): Unit = { val route = { path("baseline") { get { complete("ok") } } ~ path("cors") { cors(corsSettings) { get { complete("ok") } } } } val origin = Origin("http://example.com") binding = Await.result(http.bindAndHandle(route, "127.0.0.1", 0), 1.second) val base = s"http://${binding.localAddress.getHostString}:${binding.localAddress.getPort}" request = HttpRequest(uri = base + "/baseline") requestCors = HttpRequest( method = HttpMethods.GET, uri = base + "/cors", headers = List(origin) ) requestPreflight = HttpRequest( method = HttpMethods.OPTIONS, uri = base + "/cors", headers = List(origin, `Access-Control-Request-Method`(HttpMethods.GET)) ) } @TearDown def shutdown(): Unit = { val f = for { _ <- http.shutdownAllConnectionPools() _ <- binding.terminate(1.second) _ <- system.terminate() } yield () Await.ready(f, 5.seconds) } @Benchmark def baseline(): Unit = { val f = http.singleRequest(request).flatMap(r => Unmarshal(r.entity).to[String]) assert(Await.result(f, 1.second) == "ok") } @Benchmark def default_cors(): Unit = { val f = http.singleRequest(requestCors).flatMap(r => Unmarshal(r.entity).to[String]) assert(Await.result(f, 1.second) == "ok") } @Benchmark def default_preflight(): Unit = { val f = http.singleRequest(requestPreflight).flatMap(r => Unmarshal(r.entity).to[String]) assert(Await.result(f, 1.second) == "") } }
Example 181
Source File: AkkaKubernetes.scala From akka-kubernetes-tests with Apache License 2.0 | 5 votes |
package akka.kubernetes.sample import akka.actor.{Actor, ActorLogging, ActorSystem, PoisonPill, Props} import akka.cluster.ClusterEvent.ClusterDomainEvent import akka.cluster.sharding.{ClusterSharding, ClusterShardingSettings} import akka.cluster.singleton.{ ClusterSingletonManager, ClusterSingletonManagerSettings, ClusterSingletonProxy, ClusterSingletonProxySettings } import akka.cluster.{Cluster, ClusterEvent} import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.management.cluster.bootstrap.ClusterBootstrap import akka.management.scaladsl.AkkaManagement import akka.stream.ActorMaterializer object DemoApp extends App { implicit val system = ActorSystem("KubernetesTest") import system.{dispatcher, log} implicit val mat = ActorMaterializer() implicit val cluster = Cluster(system) log.info("Running with [{}]", new Resources()) log.info(s"Started [$system], cluster.selfAddress = ${cluster.selfAddress}") AkkaManagement(system).start() ClusterBootstrap(system).start() system.actorOf( ClusterSingletonManager.props(singletonProps = Props(new AkkaBoss("patriknw")), terminationMessage = PoisonPill, settings = ClusterSingletonManagerSettings(system)), "boss" ) val bossProxy = system.actorOf( ClusterSingletonProxy.props(singletonManagerPath = "/user/boss", settings = ClusterSingletonProxySettings(system)), name = "bossProxy" ) val teamMembers = ClusterSharding(system).start( "team-member", Props(new AkkaMember()), ClusterShardingSettings(system), AkkaMember.extractEntityId, AkkaMember.extractShardId ) cluster.subscribe(system.actorOf(Props[ClusterWatcher]), ClusterEvent.InitialStateAsEvents, classOf[ClusterDomainEvent]) val talkToTheBoss = new TalkToTheBossRouteRoute(bossProxy) val talkToATeamMember = new TalkToATeamMemberRoute(teamMembers) Http().bindAndHandle( concat(talkToTheBoss.route(), talkToATeamMember.route(), ClusterStateRoute.routeGetMembers(cluster), VersionRoute.versionRoute), "0.0.0.0", 8080 ) Cluster(system).registerOnMemberUp({ log.info("Cluster member is up!") }) } class ClusterWatcher extends Actor with ActorLogging { implicit val cluster = Cluster(context.system) override def receive = { case msg ⇒ log.info(s"Cluster ${cluster.selfAddress} >>> " + msg) } }
Example 182
Source File: AkkaKubernetesSpec.scala From akka-kubernetes-tests with Apache License 2.0 | 5 votes |
package akka.kubernetes.sample import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, StatusCodes} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.management.cluster.{ClusterHttpManagementJsonProtocol, ClusterMembers} import akka.stream.ActorMaterializer import org.scalatest.concurrent.{Eventually, ScalaFutures} import org.scalatest.time.{Seconds, Span} import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpec} class AkkaKubernetesSpec extends WordSpec with BeforeAndAfterAll with ScalaFutures with Matchers with ClusterHttpManagementJsonProtocol with Eventually { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() implicit override val patienceConfig = PatienceConfig(timeout = Span(60, Seconds), interval = Span(2, Seconds)) val target = system.settings.config.getString("akka.k8s.target") val clusterSize = system.settings.config.getInt("akka.k8s.cluster-size") val deployedVersion = system.settings.config.getString("akka.k8s.deployment-version") val log = system.log log.info("Running with target {} clusterSize {} version {}", target, clusterSize, deployedVersion) "Version deployed" should { "should have been updated" in { eventually { val response = Http().singleRequest(HttpRequest(uri = s"$target/version")).futureValue val reportedVersion = Unmarshal(response.entity).to[String].futureValue log.info("Reported version is: {}", reportedVersion) reportedVersion shouldEqual deployedVersion } } } "Cluster formation" should { "work" in { eventually { val response = Http().singleRequest(HttpRequest(uri = s"$target/cluster/members")).futureValue response.status shouldEqual StatusCodes.OK val clusterMembers: ClusterMembers = Unmarshal(response).to[ClusterMembers].futureValue withClue("Latest response: " + clusterMembers) { clusterMembers.members.size shouldEqual clusterSize clusterMembers.unreachable shouldEqual Seq.empty } log.info("Current cluster members: {}", clusterMembers) } } } "Akka Boss (singleton)" should { "say hello" in { val response = Http().singleRequest(HttpRequest(uri = s"$target/boss")).futureValue response.status shouldEqual StatusCodes.OK } } "Akka members (sharding)" should { "do some work" in { val response = Http().singleRequest(HttpRequest(uri = s"$target/team-member/johan")).futureValue response.status shouldEqual StatusCodes.OK } } override protected def afterAll(): Unit = { system.terminate() } }
Example 183
Source File: ChaosMain.scala From akka-kubernetes-tests with Apache License 2.0 | 5 votes |
package akka.kubernetes.chaos import akka.actor.ActorSystem import akka.cluster.Cluster import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.management.cluster.bootstrap.ClusterBootstrap import akka.management.scaladsl.AkkaManagement import akka.remote.RARP import akka.stream.ActorMaterializer object ChaosMain extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() val log = system.log val management = AkkaManagement(system).start() val bootstrap = ClusterBootstrap(system).start() val route = path("hello") { get { println("Time to say good bye") Runtime.getRuntime.halt(-1) complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>You won't get this</h1>")) } } val listeningOn = RARP(system).provider.getDefaultAddress.host.getOrElse("0.0.0.0") log.info("Listening on {}", listeningOn) Http().bindAndHandle(route, listeningOn, 8080) Cluster(system).registerOnMemberUp({ log.info("Cluster member is up!") }) }
Example 184
Source File: SoakMain.scala From akka-kubernetes-tests with Apache License 2.0 | 5 votes |
package akka.kubernetes.soak import akka.actor.ActorSystem import akka.cluster.Cluster import akka.http.scaladsl.Http import akka.management.cluster.bootstrap.ClusterBootstrap import akka.management.scaladsl.AkkaManagement import akka.remote.RARP import akka.stream.ActorMaterializer import com.sun.management.OperatingSystemMXBean import scala.concurrent.duration._ import akka.util.PrettyDuration._ object SoakMain extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() val log = system.log import java.lang.management.ManagementFactory log.info("Java version: {}", sys.props("java.version")) log.info("Cores: " + Runtime.getRuntime.availableProcessors) log.info("Total Memory: " + Runtime.getRuntime.totalMemory / 1000000 + "Mb") log.info("Max Memory: " + Runtime.getRuntime.maxMemory / 1000000 + "Mb") log.info("Free Memory: " + Runtime.getRuntime.freeMemory / 1000000 + "Mb") val memorySize = ManagementFactory.getOperatingSystemMXBean.asInstanceOf[OperatingSystemMXBean].getTotalPhysicalMemorySize log.info("RAM: " + memorySize / 1000000 + "Mb") log.info("JAVA env vars: {}", sys.env.filterKeys(_.contains("JAVA"))) log.info("JVM env vars: {}", sys.env.filterKeys(_.contains("JVM"))) val management = AkkaManagement(system).start() val bootstrapStart = System.nanoTime() val bootstrap = ClusterBootstrap(system).start() val listeningOn = RARP(system).provider.getDefaultAddress.host.getOrElse("0.0.0.0") log.info("Listening on {}", listeningOn) Cluster(system).registerOnMemberUp({ val joiningTime = (System.nanoTime() - bootstrapStart).nano system.actorOf(PingPong.serverProps(), "server") val client = system.actorOf(PingPong.clientProps(joiningTime), "client") val clusterStats = new StatsEndpoint(system, client) log.info("Cluster member is up! Starting tests and binding http server. Joining time: {}", joiningTime.pretty) Http().bindAndHandle(clusterStats.route, listeningOn, 8080) }) }
Example 185
Source File: ClusterSoakSpec.scala From akka-kubernetes-tests with Apache License 2.0 | 5 votes |
package akka.cluster.soak import akka.actor.ActorSystem import akka.discovery.ServiceDiscovery.Resolved import akka.event.Logging import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, StatusCodes} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.kubernetes.soak.Tests.{ResponseTimeNanos, Target} import akka.kubernetes.soak.{StatsJsonSupport, TestResults} import akka.stream.ActorMaterializer import akka.stream.scaladsl.{Sink, Source} import org.scalatest.concurrent.ScalaFutures import org.scalatest.time.{Seconds, Span} import org.scalatest.{Matchers, WordSpec} import akka.util.PrettyDuration._ import scala.collection.immutable import scala.concurrent.Future import scala.concurrent.duration._ class ClusterSoakSpec(endpoints: Resolved)(implicit system: ActorSystem) extends WordSpec with StatsJsonSupport with ScalaFutures with Matchers { import system.dispatcher implicit val mat = ActorMaterializer() implicit override val patienceConfig = PatienceConfig(timeout = Span(30, Seconds), interval = Span(2, Seconds)) val log = Logging(system, getClass) "The Clustered service" should { "not have had any failures" in { val responses: immutable.Seq[TestResults] = Source(endpoints.addresses) .mapAsyncUnordered(10) { rt => log.info("Hitting {}", rt.host) val request = HttpRequest(uri = s"http://${rt.host}:${rt.port.getOrElse(8080)}/stats") for { response <- Http().singleRequest(request) entity <- response.entity.toStrict(1.second) results <- response.status match { case StatusCodes.OK => Unmarshal(entity).to[TestResults] case unexpected => Future.failed( new RuntimeException(s"Unexpected response code: $unexpected body: ${entity.data.utf8String}") ) } } yield results } .runWith(Sink.seq) .futureValue log.info("{} nodes tested", responses.size) val maxJoinTimes = responses.map(_.joiningTime).sorted.reverse.take(5).map(_.nanos.pretty) log.info("Max join times: {}", maxJoinTimes) val maxResponseTimePerNode: immutable.Seq[(Target, ResponseTimeNanos)] = responses.map(_.lastResult.responses.maxBy(_._2)) val averageResponseTimesPerNode = responses .map((eachNode: TestResults) => { val total = eachNode.lastResult.responses.map(_._2).sum.nanos val count = eachNode.lastResult.responses.size total / count }) .sorted .reverse log.info("All response times: {}", responses) log.info("Slowest response times across all node pings: {}", maxResponseTimePerNode.sortBy(_._2).reverse.take(5).map(_._2.nanos.pretty)) log.info("Slowest average response times across all node pings: {}", averageResponseTimesPerNode.take(5).map(_.pretty)) responses.filter(_.testsFailed != 0) shouldEqual Nil withClue("Response took longer than 2 seconds. Do some investigation") { responses.filter(_.lastResult.responses.exists(_._2.nanos > 2.seconds)) shouldEqual Nil } withClue("Found unreachable events") { responses.filter(_.memberUnreachableEvents != 0) shouldEqual Nil } withClue("Found downed events") { responses.filter(_.memberDownedEvents != 0) shouldEqual Nil } } } }
Example 186
Source File: Main.scala From akka-ddd-cqrs-es-example with MIT License | 5 votes |
package com.github.j5ik2o.bank.apiServer import akka.actor.{ ActorRef, ActorSystem } import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import com.github.j5ik2o.bank.adaptor.aggregate.{ BankAccountAggregateFlowsImpl, ShardedBankAccountAggregates } import com.github.j5ik2o.bank.adaptor.controller.Routes import com.github.j5ik2o.bank.adaptor.dao.BankAccountReadModelFlowsImpl import com.github.j5ik2o.bank.adaptor.generator.IdGenerator import com.github.j5ik2o.bank.adaptor.readJournal.JournalReaderImpl import com.github.j5ik2o.bank.useCase.{ BankAccountAggregateUseCase, BankAccountReadModelUseCase } import com.typesafe.config.{ Config, ConfigFactory } import pureconfig._ import slick.basic.DatabaseConfig import slick.jdbc.JdbcProfile import scala.concurrent.ExecutionContextExecutor object Main extends App { val rootConfig: Config = ConfigFactory.load() val dbConfig: DatabaseConfig[JdbcProfile] = DatabaseConfig.forConfig[JdbcProfile](path = "slick", rootConfig) implicit val system: ActorSystem = ActorSystem("bank-system", config = rootConfig) implicit val materializer: ActorMaterializer = ActorMaterializer() implicit val executionContext: ExecutionContextExecutor = system.dispatcher val bankAccountIdGenerator = IdGenerator.ofBankAccountId(dbConfig.profile, dbConfig.db) val bankAccountAggregatesRef: ActorRef = system.actorOf(ShardedBankAccountAggregates.props, ShardedBankAccountAggregates.name) val bankAccountAggregateUseCase: BankAccountAggregateUseCase = new BankAccountAggregateUseCase( new BankAccountAggregateFlowsImpl(bankAccountAggregatesRef) ) val bankAccountReadModelUseCase: BankAccountReadModelUseCase = new BankAccountReadModelUseCase(new BankAccountReadModelFlowsImpl(dbConfig.profile, dbConfig.db), new JournalReaderImpl()) val routes: Routes = Routes(bankAccountIdGenerator, bankAccountAggregateUseCase, bankAccountReadModelUseCase) val ApiServerConfig(host, port) = loadConfigOrThrow[ApiServerConfig](system.settings.config.getConfig("bank.api-server")) val bindingFuture = Http().bindAndHandle(routes.root, host, port) sys.addShutdownHook { bindingFuture .flatMap(_.unbind()) .onComplete(_ => system.terminate()) } }
Example 187
Source File: LongPollingPool.scala From skuber with Apache License 2.0 | 5 votes |
package skuber.api.watch import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.settings.{ClientConnectionSettings, ConnectionPoolSettings} import akka.http.scaladsl.{Http, HttpsConnectionContext} import akka.stream.Materializer import skuber.api.client.Pool import scala.concurrent.duration._ private[api] object LongPollingPool { def apply[T](schema: String, host: String, port: Int, poolIdleTimeout: Duration, httpsConnectionContext: Option[HttpsConnectionContext], clientConnectionSettings: ClientConnectionSettings)(implicit mat: Materializer, system: ActorSystem): Pool[T] = { schema match { case "http" => Http().newHostConnectionPool[T]( host, port, buildHostConnectionPool(poolIdleTimeout, clientConnectionSettings, system) ).mapMaterializedValue(_ => NotUsed) case "https" => Http().newHostConnectionPoolHttps[T]( host, port, httpsConnectionContext.getOrElse(Http().defaultClientHttpsContext), buildHostConnectionPool(poolIdleTimeout, clientConnectionSettings, system) ).mapMaterializedValue(_ => NotUsed) case unsupported => throw new IllegalArgumentException(s"Schema $unsupported is not supported") } } private def buildHostConnectionPool[T](poolIdleTimeout: Duration, clientConnectionSettings: ClientConnectionSettings, system: ActorSystem) = { ConnectionPoolSettings(system) .withMaxConnections(1) // Limit number the of open connections to one .withPipeliningLimit(1) // Limit pipelining of requests to one .withMaxRetries(0) // Disables retries .withIdleTimeout(poolIdleTimeout) // Automatically shutdown connection pool after timeout .withConnectionSettings(clientConnectionSettings) } }
Example 188
Source File: Rest.scala From swagger-akka-http-sample with Apache License 2.0 | 5 votes |
package com.example.akka import akka.actor.{ActorSystem, Props} import akka.http.scaladsl.Http import akka.http.scaladsl.server.RouteConcatenation import ch.megard.akka.http.cors.scaladsl.CorsDirectives.cors import com.example.akka.add.{AddActor, AddService} import com.example.akka.addoption.{AddOptionActor, AddOptionService} import com.example.akka.echoenum.EchoEnumService import com.example.akka.echoenumeratum.EchoEnumeratumService import com.example.akka.echolist.EchoListService import com.example.akka.hello.{HelloActor, HelloService} import com.example.akka.swagger.SwaggerDocService import scala.concurrent.ExecutionContextExecutor object Rest extends App with RouteConcatenation { implicit val system: ActorSystem = ActorSystem("akka-http-sample") sys.addShutdownHook(system.terminate()) implicit val executionContext: ExecutionContextExecutor = system.dispatcher val add = system.actorOf(Props[AddActor]) val addOption = system.actorOf(Props[AddOptionActor]) val hello = system.actorOf(Props[HelloActor]) val routes = cors() (new AddService(add).route ~ new AddOptionService(addOption).route ~ new HelloService(hello).route ~ EchoEnumService.route ~ EchoEnumeratumService.route ~ EchoListService.route ~ SwaggerDocService.routes) Http().bindAndHandle(routes, "0.0.0.0", 12345) }
Example 189
Source File: SSLProvider.scala From service-container with Apache License 2.0 | 5 votes |
package com.github.vonnagy.service.container.http.security import akka.actor.Actor import akka.http.scaladsl.{ConnectionContext, Http} import com.github.vonnagy.service.container.security.SSLServerContextProvider private[http] trait SSLProvider extends SSLServerContextProvider { this: Actor => implicit def system = context.system def configNamespace = "container.ssl" def getContext(ssl: Boolean): ConnectionContext = ssl match { case true => isClient match { case true => Http().createClientHttpsContext(this.sslConfig) case false => ConnectionContext.https(sslContext, sslConfig.config.enabledCipherSuites, sslConfig.config.enabledProtocols, None, None) } case false => ConnectionContext.noEncryption() } }
Example 190
Source File: ContainerServiceSpec.scala From service-container with Apache License 2.0 | 5 votes |
package com.github.vonnagy.service.container.service import akka.actor.{ActorSystem, Terminated} import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, StatusCodes} import com.github.vonnagy.service.container.{AkkaTestkitSpecs2Support, TestUtils} import com.typesafe.config.ConfigFactory import org.specs2.concurrent.ExecutionEnv import org.specs2.matcher.FutureMatchers import org.specs2.mutable.SpecificationLike class ContainerServiceSpec extends AkkaTestkitSpecs2Support(ActorSystem("test", { val http = TestUtils.temporaryServerHostnameAndPort() ConfigFactory.parseString( s""" container.http.interface="${http._2}" container.http.port=${http._3} """)})) with SpecificationLike with FutureMatchers { sequential val cont = new ContainerService(Nil, Nil, name = "test") "The ContainerService" should { "create the appropriate parts during construction" in { cont.registeredHealthChecks must be equalTo Nil cont.registeredRoutes must be equalTo Nil cont.started must beFalse } "start properly and respond to a `/ping` request" in { cont.start() cont.started must beTrue val host = system.settings.config.getString("container.http.interface") val port = system.settings.config.getInt("container.http.port") val resp = Http().singleRequest(HttpRequest(uri = s"http://$host:$port/ping")) resp.value.get.get.status must eventually(be_==(StatusCodes.OK)) } "shut down properly when asked" in { cont.shutdown implicit val ec = ExecutionEnv.fromExecutionContext(system.dispatcher) cont.system.whenTerminated must beAnInstanceOf[Terminated].await } } }
Example 191
Source File: WSConnector.scala From scala-loci with Apache License 2.0 | 5 votes |
package loci package communicator package ws.akka import akka.http.scaladsl.{Http, HttpExt} import akka.http.scaladsl.model.ws.{InvalidUpgradeResponse, ValidUpgrade, WebSocketRequest} import akka.stream.{ConnectionException, Materializer} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.{Future, Promise} import scala.util.{Failure, Success, Try} import scala.util.control.NonFatal private object WSConnector { locally(WSConnector) def apply[P <: WS: WSProtocolFactory]( http: HttpExt, webSocketRequest: WebSocketRequest, properties: WS.Properties)(implicit materializer: Materializer) = new WSConnector[P]( http, properties, webSocketRequest, Function const { }) def apply[P <: WS: WSProtocolFactory]( webSocketRequest: WebSocketRequest, properties: WS.Properties) = { implicit val (actorSystem, actorMaterializer) = WSActorSystem.retrieve() new WSConnector[P]( Http(), properties, webSocketRequest, { case Success(connection) => connection.closed foreach { _ => WSActorSystem.release() } case _ => WSActorSystem.release() }) } class WSConnector[P <: WS: WSProtocolFactory]( http: HttpExt, properties: WS.Properties, webSocketRequest: WebSocketRequest, webSocketConnectionEstablished: Try[Connection[P]] => Unit)(implicit materializer: Materializer) extends Connector[P] { def connect(connectionEstablished: Connected[P]) = { val protocolPromise = Promise[P] def connected(connection: Try[Connection[P]]) = { webSocketConnectionEstablished(connection) connectionEstablished.set(connection) } val (future, _) = try http.singleWebSocketRequest( webSocketRequest, WSHandler.handleWebSocket(protocolPromise.future, properties, connected)) catch { case NonFatal(exception) => (Future failed exception, ()) } future onComplete { case Success(ValidUpgrade(response, _)) => val uri = webSocketRequest.uri val WSSecurityProperties(isAuthenticated, isProtected, isEncrypted, certificates) = WSSecurityProperties(webSocketRequest, response, authenticated = false) implicitly[WSProtocolFactory[P]] make ( uri.toString, Some(uri.authority.host.address), Some(uri.effectivePort), WSConnector.this, isAuthenticated, isEncrypted, isProtected, Some(Right(response)), Left(certificates)) match { case Failure(exception) => connected(Failure(exception)) case Success(ws) => protocolPromise.success(ws) } case Success(InvalidUpgradeResponse(_, cause)) => connected(Failure(new ConnectionException(cause))) case Failure(exception) => connected(Failure(exception)) } } } }
Example 192
Source File: AkkaHttpTest.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.console import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.ActorMaterializer import scala.concurrent.Future case class SpidFacebookData( AppId: String, Type: String, Application: String, ExpiresAt: Long, IsValid: Boolean, IssuedAt: Long, Scopes: Array[String], UserId: String) class AkkaHttpTest { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() import system.dispatcher val resultFuture: Future[HttpResponse] = Http().singleRequest( HttpRequest( HttpMethods.POST, "/api/signin", headers = List(headers.`Content-Type`(ContentTypes.`application/json`)), entity = """{"account":"[email protected]", "password": "yangbajing"}""")) resultFuture.flatMap(httpResponse => Unmarshal(httpResponse.entity).to[String]).foreach { str => println(str) } }
Example 193
package io.moia.streamee.demo import akka.Done import akka.actor.{ CoordinatedShutdown, ActorSystem => ClassicSystem } import akka.actor.CoordinatedShutdown.{ PhaseServiceUnbind, Reason } import akka.http.scaladsl.Http import akka.http.scaladsl.model.StatusCodes.{ BadRequest, InternalServerError, OK } import akka.http.scaladsl.server.Route import io.moia.streamee.FrontProcessor import org.slf4j.LoggerFactory import scala.concurrent.duration.FiniteDuration import scala.util.{ Failure, Success } object Api { type TextShufflerProcessor = FrontProcessor[TextShuffler.ShuffleText, Either[TextShuffler.Error, TextShuffler.TextShuffled]] final case class Config(interface: String, port: Int, terminationDeadline: FiniteDuration) private final object BindFailure extends Reason private val logger = LoggerFactory.getLogger(getClass) def apply(config: Config, textShufflerProcessor: TextShufflerProcessor)(implicit classicSystem: ClassicSystem ): Unit = { import FrontProcessor.processorUnavailableHandler import classicSystem.dispatcher import config._ val shutdown = CoordinatedShutdown(classicSystem) Http() .bindAndHandle(route(textShufflerProcessor), interface, port) .onComplete { case Failure(cause) => if (logger.isErrorEnabled) logger.error(s"Shutting down, because cannot bind to $interface:$port!", cause) shutdown.run(BindFailure) case Success(binding) => if (logger.isInfoEnabled) logger.info(s"Listening for HTTP connections on ${binding.localAddress}") shutdown.addTask(PhaseServiceUnbind, "api.unbind") { () => binding.terminate(terminationDeadline).map(_ => Done) } } } def route(textShufflerProcessor: TextShufflerProcessor): Route = { import akka.http.scaladsl.server.Directives._ import de.heikoseeberger.akkahttpcirce.ErrorAccumulatingCirceSupport._ import io.circe.generic.auto._ pathSingleSlash { get { complete { OK } } } ~ path("shuffle") { import TextShuffler._ post { entity(as[ShuffleText]) { shuffleText => onSuccess(textShufflerProcessor.offer(shuffleText)) { case Left(Error.EmptyText) => complete(BadRequest -> "Empty text!") case Left(Error.InvalidText) => complete(BadRequest -> "Invalid text!") case Left(Error.RandomError) => complete(InternalServerError -> "Random error!") case Left(Error.EmptyWordSeq) => complete(InternalServerError -> "Words empty!") case Right(TextShuffled(original, result)) => complete(s"$original -> $result") } } } } } }
Example 194
Source File: AkkaHttpServer.scala From learn-akka with Apache License 2.0 | 5 votes |
package com.allaboutscala.learn.akka.http import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server._ import akka.stream.ActorMaterializer import com.allaboutscala.learn.akka.http.routes.{DonutRoutes, ServerVersion} import com.typesafe.scalalogging.LazyLogging import scala.io.StdIn import scala.util.{Failure, Success} object AkkaHttpServer extends App with LazyLogging { implicit val system = ActorSystem("akka-http-rest-server") implicit val materializer = ActorMaterializer() implicit val ec = system.dispatcher // these should ideally be in some configuration file val host = "127.0.0.1" val port = 8080 implicit val globalRejectionHandler = RejectionHandler.newBuilder() .handle { case ValidationRejection(msg, route) => complete(StatusCodes.InternalServerError, s"The operation is not supported, error = $msg") } .handleNotFound { complete(StatusCodes.NotFound, "The path is not supported.") } .result() implicit val globalExceptionHandler = ExceptionHandler { case e: RuntimeException => complete(s"A runtime exception occurred with, msg = ${e.getMessage}") } // // routes // val serverUpRoute: Route = get { // complete("Akka HTTP Server is UP.") // } val serverVersion = new ServerVersion() val serverVersionRoute = serverVersion.route() val serverVersionRouteAsJson = serverVersion.routeAsJson() val serverVersionJsonEncoding = serverVersion.routeAsJsonEncoding() val donutRoutes = new DonutRoutes().route() val routes: Route = donutRoutes ~ serverVersionRoute ~ serverVersionRouteAsJson ~ serverVersionJsonEncoding// // ~ serverUpRoute val httpServerFuture = Http().bindAndHandle(routes, host, port) httpServerFuture.onComplete { case Success(binding) => logger.info(s"Akka Http Server is UP and is bound to ${binding.localAddress}") case Failure(e) => logger.error(s"Akka Http server failed to start", e) system.terminate() } StdIn.readLine() // let it run until user presses return httpServerFuture .flatMap(_.unbind()) // trigger unbinding from the port .onComplete(_ => system.terminate()) // and shutdown when done }
Example 195
Source File: AkkaHTTPClient.scala From learn-akka with Apache License 2.0 | 5 votes |
package com.allaboutscala.learn.akka.client import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{MediaTypes, HttpEntity, HttpMethods, HttpRequest} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.ActorMaterializer import akka.util.ByteString import com.allaboutscala.learn.akka.http.jsonsupport.{Donuts, JsonSupport} import scala.concurrent.{Await, Future} import scala.util.{Failure, Success} import scala.concurrent.duration._ object AkkaHttpClient extends App with JsonSupport { implicit val system = ActorSystem("akka-http-donuts-client") implicit val materializer = ActorMaterializer() implicit val ec = system.dispatcher // HTTP GET request val donutsUri = "http://localhost:8080/donuts" val donutsHttpRequest = HttpRequest( uri = donutsUri, method = HttpMethods.GET ) val donutsResponse = Http().singleRequest(donutsHttpRequest) donutsResponse .onComplete { case Success(donutsResponse) => println(s"Raw HttpResponse = $donutsResponse") // You obviously should not block using Await.result(...) and use flatMap or other similar future sequencing mechanics val donutsF: Future[Donuts] = Unmarshal(donutsResponse).to[Donuts] val donuts: Donuts = Await.result(donutsF, 5.second) println(s"Unmarshalled HttpResponse to Case Class = $donuts") case Failure(e) => println(s"Failed to HTTP GET $donutsUri, error = ${e.getMessage}") } Thread.sleep(3000) // HTTP POST request val jsonDonutInput = ByteString("""{"name":"plain donut", "price":1.50}""") val httpPostCreateDonut = HttpRequest( uri = "http://localhost:8080/create-donut", method = HttpMethods.POST, entity = HttpEntity(MediaTypes.`application/json`, jsonDonutInput)) val createDonutF = for { response <- Http().singleRequest(httpPostCreateDonut) _ = println(s"Akka HTTP request status = ${response.status}") if response.status.isSuccess() output <- Unmarshal(response).to[String] } yield println(s"HTTP POST request output = $output") Await.result(createDonutF, 5.second) system.terminate() }
Example 196
Source File: SauceLabsClient.scala From korolev with Apache License 2.0 | 5 votes |
package tools import java.util.Base64 import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.model.headers._ import scala.concurrent.Await import scala.concurrent.duration._ class SauceLabsClient(userName: String, accessKey: String, jobId: String)(implicit actorSystem: ActorSystem) { def setName(name: String): Unit = { putToJob(s"""{"name": "$name"}""") } def setPassed(passed: Boolean): Unit = { putToJob(s"""{"passed": $passed}""") } def putToJob(data: String): Unit = { val authorization = { val s = s"$userName:$accessKey" Base64.getEncoder.encodeToString(s.getBytes) } val request = HttpRequest( uri = s"https://saucelabs.com/rest/v1/$userName/jobs/$jobId", method = HttpMethods.PUT, headers = List(Authorization(BasicHttpCredentials(authorization))), entity = HttpEntity(ContentTypes.`application/json`, data.getBytes) ) val response = Http() .singleRequest(request) Await.result(response, 10 seconds) () } }
Example 197
Source File: AkkaHttpExample.scala From korolev with Apache License 2.0 | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import korolev._ import korolev.akka._ import korolev.server._ import korolev.state.javaSerialization._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future object AkkaHttpExample extends App { private implicit val actorSystem: ActorSystem = ActorSystem() val applicationContext = Context[Future, Boolean, Any] import applicationContext._ import levsha.dsl._ import html.{body, button, Html} private val config = KorolevServiceConfig[Future, Boolean, Any]( stateLoader = StateLoader.default(false), document = s => optimize { Html( body( s"Hello akka-http: $s", button("Click me!", event("click")(_.transition(!_)) ) ) ) } ) private val route = akkaHttpService(config).apply(AkkaHttpServerConfig()) Http().bindAndHandle(route, "0.0.0.0", 8080) }
Example 198
Source File: SimpleAkkaHttpKorolevApp.scala From korolev with Apache License 2.0 | 5 votes |
package korolev.akka import akka.actor.ActorSystem import akka.http.scaladsl.Http abstract class SimpleAkkaHttpKorolevApp(config: AkkaHttpServerConfig = null) { implicit val actorSystem: ActorSystem = ActorSystem() def service: AkkaHttpService def main(args: Array[String]): Unit = { val escapedConfig = if (config == null) AkkaHttpServerConfig() else config val route = service(escapedConfig) Http().bindAndHandle(route, "0.0.0.0", 8080) () } }
Example 199
Source File: EventSource.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.client import java.util.UUID import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.persistence.query.{NoOffset, Offset, Sequence, TimeBasedUUID} import akka.stream.Materializer import akka.stream.alpakka.sse.scaladsl.{EventSource => SSESource} import akka.stream.scaladsl.Source import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.iam.client.types.AuthToken import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import com.typesafe.scalalogging.Logger import io.circe.Decoder import io.circe.parser.decode import scala.concurrent.{ExecutionContext, Future} import scala.util.Try trait EventSource[A] { def apply[A: Decoder]( config: KgClientConfig )(implicit as: ActorSystem, mt: Materializer, ec: ExecutionContext): EventSource[A] = new EventSource[A] { private val logger = Logger[this.type] private val http = Http() private def addCredentials(request: HttpRequest)(implicit cred: Option[AuthToken]): HttpRequest = cred.map(token => request.addCredentials(OAuth2BearerToken(token.value))).getOrElse(request) private def send(request: HttpRequest)(implicit cred: Option[AuthToken]): Future[HttpResponse] = http.singleRequest(addCredentials(request)).map { resp => if (!resp.status.isSuccess()) logger.warn(s"HTTP response when performing SSE request: status = '${resp.status}'") resp } private def toOffset(id: String): Offset = Try(TimeBasedUUID(UUID.fromString(id))).orElse(Try(Sequence(id.toLong))).getOrElse(NoOffset) override def apply(iri: AbsoluteIri, offset: Option[String])( implicit cred: Option[AuthToken] ): Source[(Offset, A), NotUsed] = SSESource(iri.asAkka, send, offset, config.sseRetryDelay).flatMapConcat { sse => val offset = sse.id.map(toOffset).getOrElse(NoOffset) decode[A](sse.data) match { case Right(ev) => Source.single(offset -> ev) case Left(err) => logger.error(s"Failed to decode admin event '$sse'", err) Source.empty } } } }
Example 200
Source File: WebServer.scala From sbt-web-scalajs with Apache License 2.0 | 5 votes |
package com.example.akkahttpscalajs import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import com.typesafe.config.ConfigFactory object WebServer { def main(args: Array[String]) { implicit val system = ActorSystem("server-system") implicit val materializer = ActorMaterializer() val config = ConfigFactory.load() val interface = config.getString("http.interface") val port = config.getInt("http.port") val service = new WebService() Http().bindAndHandle(service.route, interface, port) println(s"Server online at http://$interface:$port") } }