akka-http

Akka-http process HttpRequests from different connections in one flow

你说的曾经没有我的故事 提交于 2019-12-05 12:22:04
Akka-http documentation says: Apart from regarding a socket bound on the server-side as a Source[IncomingConnection] and each connection as a Source[HttpRequest] with a Sink[HttpResponse] Assume we get the merged source containing incoming connections from many Source[IncomingConnection]. Then, assume, we get Source[HttpRequest] from Source[IncomingConnection] (see the code below). Then, no problem, we can provide a flow to convert HttpRequest to HttpResponse. And here is the problem - how can we properly Sink the responses ? How can we join the responses to connections? The whole idea behind

Akka Http Client :Custom headers

寵の児 提交于 2019-12-05 11:43:19
I am trying to use Akka-Http for invoking REST url. I am following this example from the akka documentation. Using this I am able to make the rest call. But I am not able to find out how to add custom request headers. I tried using ModeledCustomHeader, but still request is not having header. Here is my example. final class ApiTokenHeader(token: String) extends ModeledCustomHeader[ApiTokenHeader] { override def renderInRequests = false override def renderInResponses = false override val companion = ApiTokenHeader override def value: String = token } object ApiTokenHeader extends

Obtaining the client IP in Akka-http

女生的网名这么多〃 提交于 2019-12-05 10:50:53
I am trying to write an Akka HTTP microservice (akka version 2.4.11, Scala version 2.11.8, both latest versions at time of writing) which is aware of the client service's IP (i.e., remote address), and I cannot get this to work. I can create and run a service which says 'Hello!' using a route like this: val routeHello: Route = path("SayHello") { get { entity(as[String]) { body => complete { HttpResponse(entity = HttpEntity("Hello!")) } } } } I have constructed a similar route to the one above, which is extended so that it is aware of the client's IP address. I noted that I need to edit the

Streaming file from server to client using Akka

試著忘記壹切 提交于 2019-12-05 01:30:20
问题 Basically I want to allow a user to download a csv file from the server. Assume the CSV file already exists on the server. A API endpoint is exposed via GET /export. How do I stream the file from Akka HTTP server to client? This is what I have so far... Service: def export(): Future[IOResult] = { FileIO.fromPath(Paths.get("file.csv")) .to(Sink.ignore) .run() } Route: pathPrefix("export") { pathEndOrSingleSlash { get { complete(HttpEntity(ContentTypes.`text/csv`, export()) } } } 回答1: The Akka

Akka-http: How to get custom header from a request?

梦想的初衷 提交于 2019-12-05 00:53:11
问题 I send following headers in a request to my akka-http api: "Content-type": "application/json" , "Accept": "application/json" , "AppId": "some_id" . How do I get "AppId" custom header in my akka-http route? (get & parameters("id")) { (id) => complete { val appId = ?? // I want to get custom header here. } } Thanks. 回答1: You need to use one of the HeaderDirectives (HeaderDirectives docs) to extract the header. For example, if it's a custom one you can use headerValueByName which yields the

akka-stream + akka-http lifecycle

浪尽此生 提交于 2019-12-05 00:42:27
TLDR: is it better to materialize a stream per request (i.e. use short-lived streams) or to use a single stream materialization across requests, when I have an outgoing http request as a part of the stream? Details: I have a typical service that takes an HTTP request, scatters it to several 3rd party downstream services (not controlled by me) and aggregates the results before sending them back. I'm using akka-http for client implementation and spray for server (legacy, will move to akka-http over time). Schematically: request -> map -1-*-> map -> 3rd party http -> map -*-1> aggregation ->

AKKA-http deployment

孤街浪徒 提交于 2019-12-04 23:00:00
问题 This is the first time I'm working with akka-http. I wrote the following main class which starts the application: object Main extends App with Routes with Config with Protocols { implicit val system: ActorSystem = ActorSystem("slickboard-system") implicit val executor: ExecutionContext = system.dispatcher implicit val materializer: ActorMaterializer = ActorMaterializer() override val employeeActor: ActorRef = system.actorOf(EmployeeActor.props, "employees") val server = Http().bindAndHandle

How to properly call a single server from multiple actors / web handlers using Akka HTTP?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 20:47:44
问题 I have a service (let's call it Service A) which uses Akka Server HTTP to handle incoming requests. Also I have 3rd party application (Service B) which provides several web services. The purpose of service A is to transform client requests, call one or multiple web services of service B, merge/transform results and serve it back to a client. I am using Actors for some parts, and just Future for other. To make a call to Service B, I use Akka HTTP client. Http.get(actorSystem).singleRequest

akka-http: How to set response headers

陌路散爱 提交于 2019-12-04 20:11:49
问题 I've a route as follows: val route = { logRequestResult("user-service") { pathPrefix("user") { get { respondWithHeader(RawHeader("Content-Type", "application/json")) { parameters("firstName".?, "lastName".?).as(Name) { name => findUserByName(name) match { case Left(users) => complete(users) case Right(error) => complete(error) } } } } ~ (put & entity(as[User])) { user => complete(Created -> s"Hello ${user.firstName} ${user.lastName}") } ~ (post & entity(as[User])) { user => complete(s"Hello $

Functions / methods in Scala. How does this work?

只愿长相守 提交于 2019-12-04 18:20:15
问题 I am new to Scala and have a hard time understanding all the ways of declaring and using functions. Can someone please explain, step by step, what is going on here? I am following a course that introduces Akka HTTP. The code works, but I do not understand the route method: import akka.http.scaladsl.server.Directives._ def route = path("hello") { get { complete("Hello, World!") } } We are defining a method route that is being declared the value of path (imported from the line above), but then