Play in practice
Upcoming SlideShare
Loading in...5
×
 

Play in practice

on

  • 13,606 views

Slides from the SF Scala talk on "Play in Practice", 4/18/2013 Will Sargent

Slides from the SF Scala talk on "Play in Practice", 4/18/2013 Will Sargent

Statistics

Views

Total Views
13,606
Views on SlideShare
1,642
Embed Views
11,964

Actions

Likes
4
Downloads
40
Comments
0

29 Embeds 11,964

http://tersesystems.com 10168
http://marakana.com 1192
https://thenewcircle.com 184
http://feeds.feedburner.com 81
http://localhost 65
http://www.tersesystems.com 62
http://tersesystems.heroku.com 35
https://twitter.com 33
http://newsblur.com 29
http://www.directrss.co.il 16
http://plus.url.google.com 14
http://tersesystems.herokuapp.com 13
http://webcache.googleusercontent.com 11
http://www.google.pl 11
http://digg.com 9
http://prlog.ru 7
http://www.newsblur.com 7
http://abtasty.com 5
http://www.mybestcv.co.il 4
http://feedly.com 4
http://translate.googleusercontent.com 3
http://cloud.feedly.com 3
http://protected-fjord-7788.herokuapp.com 2
http://reader.aol.com 1
http://yoleoreader.com 1
http://www.google.ch 1
http://www.themusicage.com 1
http://dev.newsblur.com 1
http://www.google.com.my 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Play in practice Play in practice Presentation Transcript

  • PLAY IN PRACTICE/Will Sargent @will_sargent
  • PREAMBLEThis is going to be a broad talk.
  • USAGEThis talk does not attempt to be universal to all situations.YMMV. View slide
  • WHAT IS PLAY?Play is a web application framework. View slide
  • THESIS STATEMENTPlay is awesome.
  • WHY PLAY IS AWESOME?Because Play is Simple.
  • HERES WHAT PLAY DOES:Takes in an HTTP request.Returns (or streams) a response.
  • HERES WHAT PLAY DOESNT DO:It doesnt tie an HTTP request to a thread.It doesnt tie an HTTP session to memory.It doesnt assume you want convenient but unsafepractices.It doesnt mandate a particular object relationalframework.It doesnt mandate a particular template library.It doesnt mandate a particular routing format.
  • HERES WHAT PLAY PROVIDES AS OPTIONS:"Always-on" Build SystemHTTP APITemplatesForm HandlingJSON support
  • BUILD SYSTEMModified SBT, recompiles pages on the fly in developmentmode.Type "play idea" or "play eclipse" to generate the files foryour IDE.
  • FRONT CONTROLLERRouting API (with LangPathBindable andQueryStringBindable)Action: asynchronous and stateless by default.Immutable HTTP: cookies, request, response, headers, etc.Backed by Akka and Netty.
  • PRESENTATIONCompiled template language (Twirl, Scaml, Jade).Built-in helpers for rendering forms.Support for Twitter Bootstrap, LESS, jQuery, etc.
  • JSONFormatting to JSONParsing of JSON objectsJsPath, automatic validation, read and write combinators
  • SO, WHY DOES THIS MATTER?
  • THE BUSINESS REQUESTMake a website!
  • MAKE TWO WEBSITES!ProductAdmin
  • MAKE MORE WEBSITES!ProductAdminCustomer ServiceBusiness AnalyticsBusiness ContentJobs Queue
  • THE PROBLEMIn a complex web application, many different front ends needto look at the same data, from different angles.Touching the database directly is dangerous and messy.
  • THE SOLUTIONIn complex applications, use loosely coupled, stateless,composable services.
  • HOW?Create a Play HTTP front end, backed by Akka.Create a business domain services, backed by Akka.Use Akka to pass messages between Play and the domainservices.Present information using query services.
  • ALAN KAY ON OOP“ I thought of objects being like biological cellsand/or individual computers on a network,only able to communicate with messages (somessaging came at the very beginning -- ittook a while to see how to do messaging in aprogramming language efficiently enough tobe useful). ”
  • COMMAND / QUERY RESPONSIBILITYSEGREGATION (CQRS)Command: a message intending a change of state.Event: a message indicating that state has changed.Fault: a message indicating failure to execute a command.
  • CQRS MAPPED ONTO PLAYProcessing Commands using POSTQuerying for Data with GETBroadcasting Events with Streaming
  • PROCESSING COMMANDSValidate all input.where possible.Be very careful when handling passwords and credit cardnumbers.Use value classes
  • COMMANDSdef sendCommand(command : Command) : Future[Either[Fault,Event]]
  • CQRS
  • COMMANDtrait Command extends Productsealed abstract class AuthenticationCommand extends Commandcase class AuthenticateWithPasswordCommand(email:Email,password:String,rememberMe:Boolean)extends AuthenticationCommand {override def toString = "AuthenticateWithPasswordCommand("+ email + "," + rememberMe + ")"}
  • EVENTcase class UserAuthenticatedEvent(userId: UserID,series: Option[Long] = None,token: Option[Long] = None)extends AuthenticationEvent
  • FAULTtrait AuthenticationFaultcase class InvalidSessionCookieFault() extends AuthenticationFault
  • QUERYcase class UserInfo(id: UUID, email: Email, fullName: String)class UserInfoService {def lookup(uuid: UUID)(implicit c:Credentials): Option[UserInfo]def findByName(name: String)(implicit c:Credentials) : TraversableOnce[UserIn}
  • QUERY PRACTICES:Objects should be immutableReturn ranges or streams over unbounded lists.Dont expose repository logic (i.e. HQL, CQL, etc)Making a good query builder is hard; avoid yak shaving.
  • CONTEXT AND AUTHENTICATIONUse or to set up aWrappedRequest context.Pass the request context around implicitly in everytemplate.The request context will contain all the state you need (i.e.Credentials).Make helpers take the context as an implicit as needed.Sidenote: helpers are great for keeping logic out oftemplates.play2-rememberme SecureSocial
  • EXAMPLE TEMPLATE@(u: UserInfo)(implicit ctx: Context)@layout(title = "User Page") {@ctx.me.map { u =><p>This is your user page.</p>}<p>User page of @{u.fullName}.</p>}
  • STREAMINGYou send an event.Event goes through an Iteratee.Iteratee broadcasts event using Socket and Hub model.Play uses Server Sent Events to stream JSON to clientsJavascript picks up that JSON from SSE, handles display.is the best reference.Sidenote: also check outlilavert.x
  • EVERYTHING ELSELoggingMetricsTesting
  • LOGGINGPlays internal logger isnt the greatest.Fortunately, its not hardcoded and you can ignore it.Make your own wrapper over SF4LJ & Logback (orgrizzled, or typesafe logging).
  • METRICShas a Scala option.Map it through Globals onRequest method.Metrics
  • TESTINGUnit testing a stateless app is very easy.Swapping out services with mocks/stubs may be easierwith DI (I use Subcut).Functional and integration testing with FakeRequest andFluentlenium almost painless.
  • LOAD TESTINGUse jconsole and .Dont know how useful Typesafe Console is.Apache JMeter
  • QUESTIONS?Theres an award!