Presented on Nov 9th, 2013 at the Desert Code Camp in Phoenix, AZ. Covers the basics of Scala, the Play Framework and builds a quick and simple RESTful example application.
by web developers for web developers • Not JEE (zero reliance on JEE APIs/libs) • Focus on Developer Happiness/Productivity • Fully Asynchronous ('reactive') • Written in Scala • Backed by TypeSafe • Scala is a hybrid functional/OO language on the JVM • Originated in 2004 by Martin Odersky • Backed by TypeSafe
• Written in Scala • Implements the 'actor' concurrency model (very similar to Erlang's OTP) • Newly added ‘agent’ pattern inspired by agents from Clojure • Developers don't work with Threads directly • Highly scalable - 48 core machine can process up to 50 million messages per second • Backed by TypeSafe • MongoDB/ReactiveMongo • MongoDB is a document database that stores records in BSON (binary JSON) • ReactiveMongo is a fully asynchronous driver for MongoDB, built with Akka
and Object Oriented • Created by Martin Odersky • Invented Java Generics and other Java Features • TypeSafe has James Gosling on board as an advisor • Used by many big companies and startups • Excellent Introduction: A Neophyte's Guide to Scala • Current Version 2.10.x • Install via zip file @ http://www.scala-lang.org • Homebrew on Mac: brew install scala • Requires JDK 1.6 or JDK 1.7 (JDK 8 may have some incompatibilities - use at your own risk)
start ups in production today • Increased Developer Productivity • Scales Extremely Well • Twitter is best example • Number of Wall Street firms using Akka/Scala for stock trading • Easily Build Concurrent/Scalable Code • Case Studies: http://typesafe.com/ company/casestudies
- Scala is a very deep language with a steep learning curve. This overview is to simply demonstrate some basics; a lot of features/concepts are not touched on and there for you to explore on your own. :) • What You Need to Create a Foundation for Rest of Session • val vs var • working with collections • pattern matching • and much more… • To open REPL, from command-line simply type: scala • Follow samples via a gist: https://gist.github.com/tdrozdowski/7299177
by Web Developers for Web Developers on the JVM platform • Was originally a Java Framework • Meant to enable productivity in building modern, ‘reactive’ web applications • Brings best ideas from various other frameworks such as Rails, Django
Design • Asset pipeline for compiling CoffeeScript, running LESS/Compass, etc. • Type-safe template system for generating HTML • Embraces HTML 5 (i.e. WebSockets) • Live code changes when you reload your browser page (compiles on the fly); compile/route errors shown in detail in your browser • Includes frameworks for: persistence, caching, security, internationalization, JSON handling, working with Web Services, WebSockets
Build System HTTP Interface RESTful Web Services API Akka Datastore-agnostic Persistence Layer Template Engine Asset Pipeline HTML Form Validation Cache (EHCache) Asynchronou s I/O
SBT console • sbt commands work • ~ compile - compiles code as you change files • ~ run - compile while server is running • ~ test - continually run tests as files change • Key commands • development day to day: • run, debug, clean, compile, test, console • deployment • dist, package, publish, publish-local
Homebrew: brew install play • Does not require Scala - will bring its own version (2.10.2) • Does require JDK (1.6 or 1.7) • IDE Optional: • IntelliJ EAP 13 Ultimate Edition • http://confluence.jetbrains.com/display/IDEADEV/IDEA+13+EAP • Build 132.947 • Scala Plugin • Play 2 Plugin
- contains your Scala code • build.sbt - your build file • conf - contains your configuration files • project - sbt related folder (don’t touch this for now…) • public - your HTTP files (JS/CSS, etc) • test - your unit tests • lib - un-managed JARs • logs - application logs
• A Scala object that is a subclass of play.api.mvc.Controller • Contains a series of methods for producing Actions • Action is a controller method that returns an instance of play.api.mvc.Action • Actions handle HTTP requests and provide a response • Defines a Request => Result function to handle the request • Actions can be composed of other Actions via the ActionBuilder • Let’s create a ping Action… User Controller all action get action create action update action GET /users/[email protected] HTTP Status 200 - { "email" : "[email protected]", ....}
presentation architecture (legacy architecture) • Type-safe Scala Templates • Actions pass data into them for use via the Result • Templates can include other templates • Build links to your assets using @routes.Assets.at
JSON : play.api.libs.json._ • Construct your own JSON objects • Does mapping from JSON -> case class • by hand with Readers/Writers • ‘JSON Inception’ using macros • Manipulate JSON w/o marshaling to a case class • add/remote attributes/branches • change values of specific nodes • transform attributes into new structures • Gist: https://gist.github.com/tdrozdowski/ 7348647
with the WS class • SOAP • REST • XMLRPC • play.api.libs.ws.WS • Can add in your own request signing • provides OAuth v1 signer • Results are asynchronous; will receive a Future with a Promise of the results of the HTTP call
a logger.xml is defined • can set log levels in applicaiton.conf • can override to use your own logger.xml via JVM arguments • -Dlogger.file=<path to logger.xml> • play.api.Logger
Provides helpers so that you can mock certain aspects of the framework • Fairly straight forward way of writing tests (should match up to user stories fairly well) • play test
REST API for Complaints • User Story • “As a self-entitled user of the internet, I want to be able to post my complaints in a public forum so that I can stroke my ego.” • Service must be able to create, update, delete a complaint • Service will not have authentication (bad in real life - right?)
complaints • Create new MongoDB @MongoHQ • http://mongohq.com • Choose Free 512MB Sandbox • Name database whatever you choose • Create a database user for your API
the Sonatype Repository • Update dependencies to include play- reactivemongo • Update application.conf with mongodb.uri • Add play.plugins file to conf folder
• This will load all the dependencies for Play and the app - could take a few moments • Browser will load @ http://localhost:9000 when completed • Optional: play idea • Generates files for use with IntelliJ • Optional: Import Project into IntelliJ • File -> Import Project -> <directory> • May need to run play idea a second time
how to add these actions? • list • update(id :String) • CORS Support • https://gist.github.com/tdrozdowski/7379656 • Basic Auth • https://gist.github.com/tdrozdowski/7379847
; Release Target: Feb 2014 • Improve Performance (reflection, compiler times, distribution size) • Modularize • Remove 22 parameter limit on case classes • Play • 2.3 ; Theme: Extending into the Client ; Q1 2014 • Update to Scala 2.11.0 • Bring Slick into Play Project • Improve Mailer Plugin • JDK 8 Readiness • Client Side Support Improvements (WebJars support, JS Unit Test Runner, Update Asset Pipeline)
• http://www.scala-lang.org • http://danielwestheide.com/scala/neophytes.html • Scala in Depth (covers 2.9 though!) - Manning Press • StackOverflow! • Use the source: https://github.com/scala/scala • Play Framework Resources • http://www.playframework.com/documentation/2.2.x/ScalaHome • Google Group • Play for Scala (covers 2.1) - Manning Press • StackOverflow! • Use the source: https://github.com/playframework/playframework