akka-http

Akka HTTP 2.0 to use SSL (HTTPS)

ⅰ亾dé卋堺 提交于 2019-11-29 15:05:23
问题 I am trying to add a self signed certificate to my configuration in order to make my web service to serve content over SSL but I had no luck. Here is my current configuration ssl { jksKeystore = "localhost.p12" password = "changeit" } Any ideas on what I'm misisng to be able to spin up an HTTPS server ? My project is using akka 2.0 and scala 回答1: There are some tests in the akka code base that test the https functionality. They use the predefined http contexts defined in ExampleHttpContexts.

akka-http: complete request with flow

拜拜、爱过 提交于 2019-11-29 09:52:42
问题 Assume I have set up an arbitrarily complex Flow[HttpRequest, HttpResponse, Unit] . I can already use said flow to handle incoming requests with Http().bindAndHandle(flow, "0.0.0.0", 8080) Now I would like to add logging, leveraging some existing directive, like logRequestResult("my-service"){...} Is there a way to combine this directive with my flow? I guess I am looking for another directive, something along the lines of def completeWithFlow(flow: Flow): Route Is this possible at all? N.B.:

Akka-http: Accept and Content-type handling

风流意气都作罢 提交于 2019-11-29 03:44:31
I'm trying out Akka-http and hopefully someone can shed light on a the following questions: How does one create different routes based on the accept: header in the request? For example, i want one code path to handle "json" and one to handle "xml" requests (with default to "json" if header is missing) In cases where I don't want the contentType to be inferred, how do i specify it? For example, in the code below I try to run the json through compactPrint() but this changes it to a string, hence "text/plain". I want to override that and tell the client it's still json. My code is something like

akka HttpResponse read body as String scala

三世轮回 提交于 2019-11-28 23:50:31
问题 So I have a function with this signature (akka.http.model.HttpResponse): def apply(query: Seq[(String, String)], accept: String): HttpResponse I simply get a value in a test like: val resp = TagAPI(Seq.empty[(String, String)], api.acceptHeader) I want to check its body in a test something like: resp.entity.asString == "tags" My question is how I can get the response body as string? 回答1: import akka.http.scaladsl.unmarshalling.Unmarshal implicit val system = ActorSystem("System") implicit val

How does one log Akka HTTP client requests

巧了我就是萌 提交于 2019-11-28 17:28:44
问题 I need to log akka http client requests as well as their responses. While there seems to be a hint of API for logging these requests, there is no clear documentation on how it should be done. My approach has been to create a logged request which transparently wraps Http().singleRequest(req) as follows: def loggedRequest(req: HttpRequest) (implicit system: ActorSystem, ctx: ExecutionContext, m: Materializer): Future[HttpResponse] = { Http().singleRequest(req).map { resp ⇒ Unmarshal(resp.entity

How to properly call Akka HTTP client for multiple (10k - 100k) requests?

一笑奈何 提交于 2019-11-28 05:32:16
I'm trying to write a tool for batch data upload using Akka HTTP 2.0-M2. But I'm facing akka.stream.OverflowStrategy$Fail$BufferOverflowException: Exceeded configured max-open-requests value of [32] error. I tried to isolate a problem and here is the sample code which also fails: public class TestMaxRequests { private static final class Router extends HttpApp { @Override public Route createRoute() { return route( path("test").route( get(handleWith(ctx -> ctx.complete("OK"))) ) ); } } public static void main(String[] args) { ActorSystem actorSystem = ActorSystem.create(); Materializer

Akka实战:构建REST风格的微服务

こ雲淡風輕ζ 提交于 2019-11-27 19:55:49
使用Akka-Http构建REST风格的微服务,服务API应尽量遵循REST语义,数据使用JSON格式交互。在有错误发生时应返回: {"errcode":409,"errmsg":"aa is invalid,the ID is expected to be bb"} 类似的JSON错误消息。 代码: https://github.com/yangbajing/akka-action http://git.oschina.net/yangbajing/akka-action 代码 首先来看看代码文件结构: ├── ApiRoute.scala ├── App.scala ├── ContextProps.scala ├── book │ ├── Book.scala │ ├── BookContextProps.scala │ ├── BookRoute.scala │ └── BookService.scala └── news ├── News.scala ├── NewsContextProps.scala ├── NewsRoute.scala └── NewsService.scala 通过名字可以看出, App.scala 是启动程序,以 Route 结尾的是API路由定义文件, Service 结尾的就是服务实现代码了。 ContextProps

How to properly call Akka HTTP client for multiple (10k - 100k) requests?

微笑、不失礼 提交于 2019-11-27 05:34:03
问题 I'm trying to write a tool for batch data upload using Akka HTTP 2.0-M2. But I'm facing akka.stream.OverflowStrategy$Fail$BufferOverflowException: Exceeded configured max-open-requests value of [32] error. I tried to isolate a problem and here is the sample code which also fails: public class TestMaxRequests { private static final class Router extends HttpApp { @Override public Route createRoute() { return route( path("test").route( get(handleWith(ctx -> ctx.complete("OK"))) ) ); } } public

How to create a Source that can receive elements later via a method call?

为君一笑 提交于 2019-11-26 23:59:19
I would like to create a Source and later push elements on it, like in: val src = ... // create the Source here // and then, do something like this pushElement(x1, src) pushElement(x2, src) What is the recommended way to do this? Thanks! There are three ways this can be achieved: 1. Post Materialization with SourceQueue You can use Source.queue that materializes the Flow into a SourceQueue : case class Weather(zipCode : String, temperature : Double, raining : Boolean) val bufferSize = 100 //if the buffer fills up then this strategy drops the oldest elements //upon the arrival of a new element.

Akka HTTP: Blocking in a future blocks the server

天大地大妈咪最大 提交于 2019-11-26 23:53:50
问题 I am trying to use Akka HTTP to basic authenticate my request. It so happens that I have an external resource to authenticate through, so I have to make a rest call to this resource. This takes some time, and while it's processing, it seems the rest of my API is blocked, waiting for this call. I have reproduced this with a very simple example: // used dispatcher: implicit val system = ActorSystem() implicit val executor = system.dispatcher implicit val materializer = ActorMaterializer() val