Search code examples
scalaservicerequestakkaakka-http

Allow all requests to the AKKA-HTTP service


Comrades! I have a small service from the AKKA-HTTP example.

import ch.megard.akka.http.cors.scaladsl.CorsDirectives._

object Server extends App{
    
    val route = cors() {
      path("hello") {
        get {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Привет ёпта</h1>"))
        }
      }
    }

    val routes = cors() {
      concat(route, getUser, createUser, addMessage, getQueue, test, test2)
    }
    
    val bindingFuture = Http().newServerAt("localhost", 8080).bind(routes)
}

For CORS i create file resourses/application.conf:

akka-http-cors {

  allowed-origins = "*"
  allowed-methods = ["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS"]
}

When I run a project in intellij idea, the route works fine:

enter image description here

But if I run the project in Docker, the route doesn't want to work. Error in chrome: enter image description here

Error in Postman:

enter image description here

Below are the errors when the project is turned off everywhere: enter image description hereenter image description here

How do I properly configure the application.conf file so that the application accepts third-party requests? Or maybe the error is hidden in something else? Please tell me! I've been thinking for two days.

UPD: File DockerFile:

FROM openjdk:8-jre-alpine
WORKDIR /opt/docker
ADD --chown=daemon:daemon opt /opt
USER daemon
ENTRYPOINT ["/opt/docker/bin/servertelesupp"]
CMD []

Project on GitHub: https://github.com/MinorityMeaning/ServerTeleSupp


Solution

  • Change

    Http().newServerAt("localhost", 8080).bind(routes)

    to

    Http().newServerAt("0.0.0.0", 8080).bind(routes)

    By binding to localhost inside docker, you will not be able route traffic from outside to it.