Ammonite icon indicating copy to clipboard operation
Ammonite copied to clipboard

simple akka-http work fine in REPL but not in script mode

Open dacr opened this issue 7 years ago • 4 comments

example code

import $ivy.`com.typesafe.akka::akka-http:10.1.1`
import $ivy.`com.typesafe.akka::akka-stream:2.5.12`
import akka.http.scaladsl._
import akka.http.scaladsl.server.Directives._

implicit val system = akka.actor.ActorSystem("MySystem")
implicit val materializer = akka.stream.ActorMaterializer()
implicit val executionContext = system.dispatcher

val route1 = pathPrefix("") { getFromDirectory("example") }
val bindingFuture = Http().bindAndHandle(route1, "0.0.0.0", 8080)
def shutdown = bindingFuture.flatMap(_.unbind()).onComplete{_ => system.terminate() }
/*
 Remember that everything is done asynchronously, so don't exit...
 Following lines allow to wait for the actor system to be terminated
 */
import scala.concurrent.Await
import scala.concurrent.duration.Duration
Await.result(system.whenTerminated, Duration.Inf)
  • just create an example directory with a dummy index.html file in it
  • starts amm, copy and paste in ammonite REPL, everything works fine
    • curl http://localhost:8080/index.html
  • launch it using amm example.sc the http server doesn't work anymore (timeout...)
20:32 $ amm example.sc 
Compiling (synthetic)/ammonite/predef/interpBridge.sc
Compiling (synthetic)/ammonite/predef/DefaultPredef.sc
Compiling /home/dcr/dev-new/oneline-httpd/httpd/example.sc
[INFO] [05/07/2018 20:34:16.479] [MySystem-akka.actor.default-dispatcher-8] [akka.actor.ActorSystemImpl(MySystem)] Request timeout encountered for request [GET / Empty]

dacr avatar May 07 '18 18:05 dacr

Running your example here is what I get:

$ http :8080/example/
HTTP/1.1 404 Not Found
Content-Length: 42
Content-Type: text/plain; charset=UTF-8
Date: Sun, 16 Sep 2018 08:40:15 GMT
Server: akka-http/10.1.5

The requested resource could not be found.

Akka HTTP runs fine but return 404 code. It looks like there is a problem on path. The following route works fine :

import akka.http.scaladsl.model.{HttpEntity, ContentTypes}
val route =
  path("hello") {
    get {
      complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
    }
  }
$ http :8080/hello
HTTP/1.1 200 OK
Content-Length: 31
Content-Type: text/html; charset=UTF-8
Date: Sun, 16 Sep 2018 08:42:22 GMT
Server: akka-http/10.1.5

<h1>Say hello to akka-http</h1>

mycaule avatar Sep 16 '18 08:09 mycaule

No bug in Ammonite 0.7 Same bug as @dacr in Ammonite 1.2

mycaule avatar Sep 16 '18 11:09 mycaule

I got the same issue with doodle, see : https://github.com/creativescala/doodle/issues/78

There is a simple a workaround, put your code into an object, and make some interaction with this object in order "activate" it.

Then everything will work fine with akka-http, doodle and probably some other libraries...

For example this akka-http static file web servers will work perfectly in REPL or script mode :

import $ivy.`com.typesafe.akka::akka-http:10.1.7`
import $ivy.`com.typesafe.akka::akka-stream:2.5.21`

import akka.http.scaladsl._
import akka.http.scaladsl.server.Directives._
import scala.concurrent.Await
import scala.concurrent.duration.Duration

object StaticServer {
  implicit val system = akka.actor.ActorSystem("MySystem")
  implicit val materializer = akka.stream.ActorMaterializer()
  implicit val executionContext = system.dispatcher

  val routes = pathPrefix("browse") { getFromBrowseableDirectory(".") }
  Http().bindAndHandle(routes, "0.0.0.0", 8080).andThen{case _ => println("Ready.")}
}
Await.ready(StaticServer.system.whenTerminated, Duration.Inf)

@lihaoyi what do you think about this ? May be this is something to add in your documentation ? Or do you think it can be fixed ?

dacr avatar Jul 10 '19 08:07 dacr

I can confirm the same behaviour even with Ammonite 1.8.1 - ending script with Await.result to wait for a future to resolve ends with timeout exception every time, doing the same in REPL works well.

The trick with the object still works, thanks for sharing.

mprihoda avatar Nov 06 '19 08:11 mprihoda