Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes for Play REST samples #48

Merged
merged 2 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion play-scala-rest-api-example/app/RequestHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import play.core.WebCommands
/**
* Handles all requests.
*
* https://www.playframework.com/documentation/2.5.x/ScalaHttpRequestHandlers#extending-the-default-request-handler
* https://www.playframework.com/documentation/latest/ScalaHttpRequestHandlers#extending-the-default-request-handler
*/
class RequestHandler @Inject()(webCommands: WebCommands,
optDevContext: OptionalDevContext,
Expand Down
6 changes: 3 additions & 3 deletions play-scala-rest-api-example/docs/src/main/paradox/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ We’ll demonstrate with a "best practices" REST API. You can get source code f

## From Lightbend Tech Hub

Download a pre-packaged bundle with this link: <https://example.lightbend.com/v1/download/play-scala-rest-api-example>.
Download a pre-packaged bundle with this link: <https://example.lightbend.com/v1/download/play-samples-play-scala-rest-api-example>.

**Linux/Mac:**

Expand All @@ -25,7 +25,7 @@ cd play-scala-rest-api-example
sbt.bat
```

## [From GitHub](https://github.com/playframework/play-samples):
## [From GitHub](https://github.com/playframework/play-samples)

```bash
git clone https://github.com/playframework/play-samples.git
Expand All @@ -46,7 +46,7 @@ The fun answer is that [Play is **fast**](https://www.lightbend.com/blog/why-is-

In fact, Play is so fast that you have to turn off machines so that the rest of your architecture can keep up. The Hootsuite team was able to **reduce the number of servers by 80%** by [switching to Play](https://www.lightbend.com/resources/case-studies-and-stories/how-hootsuite-modernized-its-url-shortener). if you deploy Play with the same infrastructure that you were using for other web frameworks, you are effectively staging a denial of service attack against your own database.

Play is fast because Play is **built on reactive bedrock**. Play starts from a reactive core, and builds on reactive principles all the way from the ground. Play breaks network packets into a stream of small chunks of bytes. It keeps a small pool of work stealing threads, mapped to the number of cores in the machine, and keeps those threads fed with those chunks. Play exposes those byte chunks to the application for body parsing, Server Sent Events and WebSockets through [Akka Streams](http://doc.akka.io/docs/akka/2.4/scala/stream/stream-introduction.html) -- the Reactive Streams implementation designed by the people who invented [Reactive Streams](http://www.reactive-streams.org/) and wrote the [Reactive Manifesto](http://www.reactivemanifesto.org/).
Play is fast because Play is **built on reactive bedrock**. Play starts from a reactive core, and builds on reactive principles all the way from the ground. Play breaks network packets into a stream of small chunks of bytes. It keeps a small pool of work stealing threads, mapped to the number of cores in the machine, and keeps those threads fed with those chunks. Play exposes those byte chunks to the application for body parsing, Server Sent Events and WebSockets through [Akka Streams](http://doc.akka.io/docs/akka/2.5/scala/stream/stream-introduction.html) -- the Reactive Streams implementation designed by the people who invented [Reactive Streams](http://www.reactive-streams.org/) and wrote the [Reactive Manifesto](http://www.reactivemanifesto.org/).

Linkedin uses Play throughout its infrastructure. It wins on all [four quadrants of scalability](http://www.slideshare.net/brikis98/the-play-framework-at-linkedin/128-Outline1_Getting_started_with_Play2) ([video](https://youtu.be/8z3h4Uv9YbE)). Play's average "request per second" comes in around [tens of k on a basic quad core w/o any intentional tuning](https://twitter.com/kevinbowling1/status/764188720140398592) -- and it only gets better.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ class PostResourceHandler @Inject()(

}
```

Here, it's a straight conversion in `createPostResource`, with the only hook being that the router provides the resource's URL, since it's something that `PostData` does not have itself.

## Rendering Content as JSON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ class PostRouterSpec extends PlaySpec with GuiceOneAppPerTest {

val posts: Seq[PostResource] = Json.fromJson[Seq[PostResource]](contentAsJson(home)).get
posts.filter(_.id == "1").head mustBe (PostResource("1","/v1/posts/1", "title 1", "blog post 1" ))

}

"render the list of posts when url ends with a trailing slash" in {
val request = FakeRequest(GET, "/v1/posts/").withHeaders(HOST -> "localhost:9000").withCSRFToken
val home:Future[Result] = route(app, request).get

val posts: Seq[PostResource] = Json.fromJson[Seq[PostResource]](contentAsJson(home)).get
posts.filter(_.id == "1").head mustBe (PostResource("1","/v1/posts/1", "title 1", "blog post 1" ))
}
}

}