javascalaakkasprayxsbt-web-plugin

spray akka deployment on webserver


I have an application built on spray + akka. using this guide:

http://sysgears.com/articles/building-rest-service-with-scala/

It explains this example: https://github.com/oermolaev/simple-scala-rest-example

The application is working just fine. But when trying to deploy on a webServer I didn't find a way to do that.

I've tried to use xsbt-web-plugin to deploy on Tomcat, got the following input:

 ~container:start

[info] starting server ... Adding Context for target/webapp ...

Starting service Tomcat Starting Servlet Engine:

Apache Tomcat/7.0.34 org.apache.catalina.startup.ContextConfig

getDefaultWebXmlFragment INFO: No global web.xml found

org.apache.coyote.AbstractProtocol start INFO: Starting

ProtocolHandler ["http-nio-8080"]

But Tomcat is returning 404 for all the requests.

Does someone know how can I deploy a spray akka application on Tomcat?


Solution

  • Solved the problem.

    This is what you need to make xsbt-plugin work with the spray application:

    1. Set the root-path in application.conf

    As @jrudolph pointed: spray servlet doesn't know to figure it out automaticly on tomcat:

    spray.servlet {
       boot-class = "com.sysgears.example.boot.Boot"
       root-path = "/rest"
       request-timeout = 10s
     } 
    
    1. Change class boot to extend webBoot:

    boot.scala

    class Boot extends WebBoot {
      // create an actor system for application
    
      val system = ActorSystem("rest-service-example")
    
      // create and start rest service actor
    
      val serviceActor = system.actorOf(Props[RestServiceActor], "rest-endpoint")
    }
    
    1. add the web.xml as explained on xsbt-web-plugin:

      src/main/webapp/WEB-INF/web.xml:

      <listener>
          <listener-class>spray.servlet.Initializer</listener-class>
      </listener>
      
      <servlet>
          <servlet-name>SprayConnectorServlet</servlet-name>
          <servlet-class>spray.servlet.Servlet30ConnectorServlet</servlet-class>
          <async-supported>true</async-supported>
      </servlet>
      
      <servlet-mapping>
          <servlet-name>SprayConnectorServlet</servlet-name>
          <url-pattern>/*</url-pattern>
      </servlet-mapping>
      

    For the full change see the comparison on github (The example writer has generously generate this branch for tomcat users)

    https://github.com/oermolaev/simple-scala-rest-example/compare/spray-tomcat