Reputation: 63
I've been working on building an application with Akka actors, and now that I've completed the actor-based business logic I'd like to give it a RESTful + websocket front-end. I'm trying to find instructions for how to setup Play within the context of an existing application. The only instructions I could find are how to create new Play applications. Is there any documentation on how to do this?
UPDATE: This question has more to do with the SBT setup than connecting the controllers to my actor based business logic. I tried to modify build.sbt
and plugins.sbt
to include the things that activator built when I did activator new
but IDEA is complaining about Cannot resolve symbol PlayScala
. Also I am wondering about moving my actors from the SBT-standard src/main/scala
to app/
-- should it be in app/actors
(as I saw in one of the templates) or in app/models
?
Here's my build.sbt
:
name := "test"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(play.PlayScala)
scalaVersion := "2.11.6"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
specs2 % Test
)
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
scalaVersion := "2.11.6"
resolvers += "repo.novus rels" at "http://repo.novus.com/releases/"
resolvers += "repo.novus snaps" at "http://repo.novus.com/snapshots/"
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.1" % "test"
libraryDependencies += "com.github.nscala-time" %% "nscala-time" % "1.8.0"
libraryDependencies += "org.slf4j" % "slf4j-simple" % "1.6.4"
libraryDependencies += "org.reactivemongo" %% "reactivemongo" % "0.10.5.0.akka23"
routesGenerator := InjectedRoutesGenerator
and here is my plugins.sbt
:
logLevel := Level.Warn
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.0")
// web plugins
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.0.6")
addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0")
Upvotes: 5
Views: 1166
Reputation: 2103
The below changes to the project helped me to adopt PlayFramework (version:2.8.1) into the existing project:
In plugins.sbt
add
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.1")
In build.sbt
add the below to libraryDependencies
:
"com.typesafe.play" %% "play" % playVersion
2.1: Add guice
support in the libraryDependencies ++= Seq(guice)
2.2: Add the below to include /src
to classpath:
unmanagedSourceDirectories in Compile += baseDirectory.value / "src"
Add the /src
directory to add it to classpath and verify:
[info] Loading global plugins from /home/training/.sbt/1.0/plugins
[info] Loading settings for project coursera-build from plugins.sbt ...
[info] Loading project definition from /home/training/IdeaProjects/coursera/project
[info] Loading settings for project root from build.sbt ...
[info] Set current project to coursera (in build file:/home/training/IdeaProjects/coursera/)
[info] * /home/training/IdeaProjects/coursera/app-2.12
[info] * /home/training/IdeaProjects/coursera/app
[info] * /home/training/IdeaProjects/coursera/app
[info] * /home/training/IdeaProjects/coursera/src
PlayFramework expects the following:
controllers
in app/controllers
- Add your controller.scala here
application.conf
in conf
routes
in conf
- Add a route that refers to controller on step-1
Execute: sbt run
Reference: The official repo
Upvotes: 0
Reputation: 382
UPDATE Answer:
Start in your Project File the activator
Then start the web App with run
and open the browser with http://localhost:9000
This loads all the dependencies and compiles the Scala Play Application.
This should correct your IDEA Ide Problems about missing Dependencies.
In Scala Play 2.4 you can choose between to Project Layouts.
app/
src/main/scala
used by SBT and Maven Project is new and experimental and may have issues.Before (Play 2.3 and smaller)
there where only the Project layout app/
The three Packages
app/controllers -> Application controllers
app/models -> Application business layer
app/views -> Templates
are predefined.
You can of course add your own packages, for example an app/actors
package.
Upvotes: 1
Reputation: 382
One Part is to connect your Businesslayer (actor-based business logic - a ActorSystem) with the Controller (play.api.mvc.Controller) in the Play MVC.
The following Sample show how to do that:
import play.api.mvc._
import akka.actor._
import javax.inject._
import actors.HelloActor
@Singleton
class Application @Inject() (system: ActorSystem) extends Controller {
val helloActor = system.actorOf(HelloActor.props, "hello-actor")
//...
}
then you need to know some about the Play Framework:
Now define some Route Request Paths:
- GET /clients/all controllers. ... .list()
- GET /clients/:id controllers. ... .show(id: Long)
And implement the Action in your Controller:
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.duration._
import akka.pattern.ask
implicit val timeout = 5.seconds
def show(id: Long) = Action.async {
// This ist a ask pattern returns a Future
// Here ist the Connection between the Action from Play to your
// Actor System - your Business Layer
// map it to your own result type
(helloActor ? SayHello(id)).mapTo[String].map { message =>
Ok(message)
}
}
Upvotes: 0