how to play at work - a play framework tutorial

16
A Play Framework Tutorial Assist-Software Romania- Marius Carp

Upload: assistsoftware

Post on 28-Jul-2015

48 views

Category:

Software


0 download

TRANSCRIPT

Page 1: How to Play at Work - A Play Framework Tutorial

A Play Framework Tutorial

Assist-Software Romania- Marius Carp

Page 2: How to Play at Work - A Play Framework Tutorial

What is Play Framework

• An open-source modern web framework

• Based on a lightweight, stateless, web-friendly architecture.

• Built on Akka, Play provides predictable and minimal resource consumption

• Developer friendly

• Has great support for Eclipse and Intellij IDEA.

• Provides RESTful by default

• Has a built-in JSON module and extensive NoSQL & Big Data Support.

• There are libraries for everything - most of which can be used in Play.

Page 3: How to Play at Work - A Play Framework Tutorial

System Requirements

• Install JDK 1.6 or later.

• Install Scala (general-purpose language designed to support clear, concise and type-safe programming). Optional, but good to have, you may also use Java.

• Install SBT is an open source build tool for Scala and Java projects, similar to Java's Maven or Ant.

• Install Play Framework through a tool called Typesafe Activator.

• Install PostgreSQL.

• Install pgAdmin is a comprehensive PostgreSQL database design and management system.

Page 4: How to Play at Work - A Play Framework Tutorial

Game Play Tutorial

• Let's create a new vanilla Play Scala application and name it scala-game. Here is the command you need to run it in command-line:

• To start your game from command-line run from your project root folder:

• or (recommanded)

• To view your work go to: http://localhost:9000/

Page 5: How to Play at Work - A Play Framework Tutorial

Game Configuration Tutorial

• To add more experience to your Play skills add the following dependencies to your build.sbt file:

• Slick - a modern database query and access library for Scala.

-It allows you to work with stored data and gives you full control

• PostgreSQL JDBC is necessary to connect to PostgreSQL database

Page 6: How to Play at Work - A Play Framework Tutorial

Game Configuration Tutorial

• Then, create a database and add its credentials to application.config file.

• Add a package named dao to your app folder

• Define database tables and set it in configuration

• Uncomment evolution plugin and set it enabled

• Play Framework's Evolutions will create tables and relations between them (primary keys, indexes, sequences etc...) for you.

Page 7: How to Play at Work - A Play Framework Tutorial

Game Configuration Tutorial

• We have to specify to which database to connect creating a trait named PostgresSupport and define an implicit database

Page 8: How to Play at Work - A Play Framework Tutorial

Loading Textures in Play Framework

• Create a package models and in it a case class named Player

• Play tracks your database evolutions using several evolution scripts located in the db/evolutions directory.

• The first script is named 1.sql.

• We just need to map the models to tabels.

• PostgreSQL will generate a PrimaryKey for the player using AutoInc:

Page 9: How to Play at Work - A Play Framework Tutorial

Loading Textures in Play Framework

• A GET HTTP request will be made to this route "/„ when a user first time accesses our application .

• The entire list of routes can be found in the conf/routes file

• Each route consists of an HTTP method and URI pattern, both associated with a call to an Action generator.

• An Action is basically a request => result function that handles a request and generates a result to be sent to the client.

• A Controller is nothing more than a singleton object that generates Action values.

Page 10: How to Play at Work - A Play Framework Tutorial

Play Framework Game modes

• You can chose to Play in two ways:

• Single player using Play Scala template

• Multi player using one of the following JavaScript frameworks: AngularJS, BackboneJS, EmberJS, ExtJS, DustJS .

• Adding a new player and get players list using Slick.• players list (players is the mapped table)

• add new player

Page 11: How to Play at Work - A Play Framework Tutorial

Single Player A Play Scala template

• Is a simple text file, that contains small blocks of Scala code.

• Can generate any text-based format, such as HTML, XML or CSV.

• Allow web designers to easily work with the templates.

• Templates are compiled as standard Scala functions.

• If you create a views/Application/index.scala.html template file, it will generate a views.html.Application.indexfunction.

Page 12: How to Play at Work - A Play Framework Tutorial

Single Player A Play Scala template

• In index.scala.html we have the player list

• Play Framework will render the page server-side.

• Adding players to the game is as simple as that: submit a form using POST to this route /player/single

Page 13: How to Play at Work - A Play Framework Tutorial

Multi Player

• You will have two separated applications in one project: • API Backend (presistent data) • Frontend side (making AJAX calls to the server)- in public folder.

• To test your calls use Postman.

• Make a GET call to /player/multi

• This will execute controllers.Application.findAll and it will return a list of players in JSON format.

Page 14: How to Play at Work - A Play Framework Tutorial

Multi Player

• Example:

• To add a player use POST method to this route /player/multi with the following JSON body:

{ "name": "Newbie", "level": 1

}

Page 15: How to Play at Work - A Play Framework Tutorial

Game Features

• Amazon S3 module (Scala) - allows you to list, get, add and remove items from a S3 bucket.

• Authentication and Authorization module (Scala) - offers Authentication and Authorization features to Play2.x applications.

• Deadbolt 2 Plugin -powerful authorisation mechanism for defining access rights to certain controller methods

• Dust Plugin – provides support for the dust client side template language (DustJS).

• Memcached Plugin - provides a memcached based cache implementation.

• MongoDB Salat, Casbah Plugin (Scala) - provides managed MongoDB access and object mapping using Salat and Casbah

• Redis Plugin (Java and Scala) - provides a redis based cache implementation

• SecureSocial (Java and Scala) - an authentication module supporting OAuth, OAuth2, OpenID, Username/Password and custom authentication schemes.

Page 16: How to Play at Work - A Play Framework Tutorial

Most popular Players

• LinkedIn

• The Guardian

• Twitter

• Foursquare

• Coursera

• Klout

• Walmart

More details here: http://assist-software.net/blog/how-play-work-play-framework-tutorial