-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ff67bb
commit f9a08d5
Showing
23 changed files
with
184 additions
and
380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package controller | ||
|
||
import javax.inject._ | ||
|
||
import akka.actor.ActorSystem | ||
import play.api.mvc._ | ||
|
||
import scala.concurrent.duration._ | ||
import scala.concurrent.{ExecutionContext, Future, Promise} | ||
|
||
/** | ||
* This controller creates an `Action` that demonstrates how to write | ||
* simple asynchronous code in a controller. It uses a timer to | ||
* asynchronously delay sending a response for 1 second. | ||
* | ||
* @param cc standard controller components | ||
* @param actorSystem We need the `ActorSystem`'s `Scheduler` to | ||
* run code after a delay. | ||
* @param exec We need an `ExecutionContext` to execute our | ||
* asynchronous code. When rendering content, you should use Play's | ||
* default execution context, which is dependency injected. If you are | ||
* using blocking operations, such as database or network access, then you should | ||
* use a different custom execution context that has a thread pool configured for | ||
* a blocking API. | ||
*/ | ||
@Singleton | ||
class AsyncController @Inject()( | ||
cc: ControllerComponents, | ||
actorSystem: ActorSystem)(implicit exec: ExecutionContext) | ||
extends AbstractController(cc) { | ||
|
||
/** | ||
* Creates an Action that returns a plain text message after a delay | ||
* of 1 second. | ||
* | ||
* The configuration in the `routes` file means that this method | ||
* will be called when the application receives a `GET` request with | ||
* a path of `/message`. | ||
*/ | ||
def message = Action.async { | ||
getFutureMessage(1.second).map { msg => | ||
Ok(msg) | ||
} | ||
} | ||
|
||
private def getFutureMessage(delayTime: FiniteDuration): Future[String] = { | ||
val promise: Promise[String] = Promise[String]() | ||
actorSystem.scheduler.scheduleOnce(delayTime) { | ||
promise.success("Hi!") | ||
}(actorSystem.dispatcher) // run scheduled tasks using the actor system's dispatcher | ||
promise.future | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package controller | ||
|
||
import javax.inject._ | ||
|
||
import play.api.mvc._ | ||
import service.Counter | ||
|
||
/** | ||
* This controller demonstrates how to use dependency injection to | ||
* bind a component into a controller class. The class creates an | ||
* `Action` that shows an incrementing count to users. The [[Counter]] | ||
* object is injected by the Guice dependency injection system. | ||
*/ | ||
@Singleton | ||
class CountController @Inject()(cc: ControllerComponents, counter: Counter) | ||
extends AbstractController(cc) { | ||
|
||
/** | ||
* Create an action that responds with the [[Counter]]'s current | ||
* count. The result is plain text. This `Action` is mapped to | ||
* `GET /count` requests by an entry in the `routes` config file. | ||
*/ | ||
def count = Action { Ok(counter.nextCount().toString) } | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
app/controllers/HomeController.scala → app/controller/HomeController.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package controllers | ||
package controller | ||
|
||
import javax.inject._ | ||
import play.api.mvc._ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.