SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
Play Framework
Understanding using Scala
K. Harinath
PRESENTED BY:
ClariTrics Technologies
Agenda
· Introduction to Scala Play
· Key Features
· Anatomy of Scala Play Application
· Architecture of Scala play
· MVC Architecture in Scala play
· Developing Rest API in Play Framework
· Adding Dependency In Play Framework
· Request URL Mapping in play
· Reading Configuration File
· Concurrent Programming in Scala
· Packing for Production – Commonly used Commands
Introduction to Play Framework
· Is based on a lightweight, stateless, web-friendly, non blocking architecture.
· Built on Akka, provides predictable and minimal resources consumption (CPU, memory, threads)
for highly-scalable applications.
· Lots of built in features for fast development.
· Follows MVC architecture
Features of Play Framework
· Strong focus on productivity. Fast turnaround.
· Hot reloading: Fix the bug and hit reload!
· Type safe all the way, even templates and route files!
· Use Scala or Java: Your choice. You can even mix them!
· Predefined module for heavy lifting. Mail Service, Authentication
· Play Provides full stack
· Websocket support
· Template Engine for views
· Testing engine
Installation of Play Framework
· Dependencies
· Java 1.8
· Sbt 1.1.0
· Intellji or Eclipse
· Creating New Application
· Using Play Starter Projects
· Download Play Starter
· Create a new application using SBT
· Requires giter8
Steps to Initialize Play
Framework
· Unzip the package and place in the desired location
· Open the package in the IntelliJ editor File->Open
· Open the terminal inside the IntelliJ editor
· Run $sbt run - Process will take more time, please wait patiently
· Application will be opened at port 9000
Steps – Cont 1
· Download the File
· Unzip
Steps – Cont 2
· Open the Folder in Intellji
Steps – Cont 3
· Wait till it sync
· Open the terminal
Steps – Cont 4
· Type sbt run
· Application will be started at localhost:9000
Anatomy of Play Framework
MVC Architecture in Play
Playing Around Play Framework
· Accessing the End Point
· GET http://localhost:9000 - HTML View Page
· GET http://localhost:9000/count - Count gets incremented for each page refresh or request
· GET http://localhost:9000/message - Displays Hi! Message
· Important File to Remember in Play Framework
· build.sbt – Adding third party dependency from Maven
· Conf/applicaton.conf – Contains the Play framework settings
· Conf/routes – Contains the Url Mapping
· App folder holds the controllers, views, filters etc
Creating Rest API using Play!
Hello World using Play
· Go to conf/routes
· GET /greetings controllers.HelloWorldController.index
· HelloWorldController – name of the controller class file
· Index is the function name inside the controller class file
· Create a controller called HelloWorldController.scala in app/controller
package controllers
import javax.inject._
import play.api.mvc._
/**
* This controller creates an `Action` to handle HTTP requests to the
* application's home page.
*/
@Singleton
class HelloWorldController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
/**
Returns hello world
*/
def index = Action {
Ok("Hello World")
}
}
Output of Hello World
Adding Dependency in Play
· Using build.sbt
· // https://mvnrepository.com/artifact/org.apache.opennlp/opennlp-tools
libraryDependencies += "org.apache.opennlp" % "opennlp-tools" % "1.8.3"
· Using custom build jar
· Placing the custom build jar or jar which are not available in the Maven repository. We
should place those files inside lib in root location
Play Terminology
· Actions
· A Action is basically a function that handles a request and generates a result to be sent to
the client.
· Controllers
· A controller in Play is nothing more than an object that generates Action values. Controllers
are typically defined as classes to take advantage of Dependency Injection.
· Modules
· Play uses public modules to augment built-in functionality.
Returning Different Result Type
· val textResult = Ok("Hello World!")
· By default returns text/plain
· val xmlResult = Ok(<message>Hello World!</message>)
· Returns the result application/xml
· val htmlResult2 = Ok(<h1>Hello World!</h1>).as(HTML)
· Renders the HTML page
HTTP Routing
· Question ?
· Which file is responsible for URL mapping?
HTTP routing
· The built-in HTTP router
· The router is the component in charge of translating each incoming HTTP request to an
Action.
· An HTTP request is seen as an event by the MVC framework. This event contains two major
pieces of information:
· The request path (e.g. /clients/1542, /photos/list), including the query string
· The HTTP method (e.g. GET, POST)
· URI Pattern
· Static Path
· GET /clients/all controllers.Clients.list()
· Dynamic Parts
· GET /clients/:id controllers.Clients.show(id: Long)
· Router Feature
· What if I say you can do regex validation in route file itself !
Workflow of the Request from
Route file
Configuration File
· Play uses the Typesafe config library, but Play also provides a nice Scala wrapper called
Configuration with more advanced Scala features.
class MyController @Inject() (config: Configuration, c: ControllerComponents) extends
AbstractController(c) {
def getFoo = Action {
Ok(config.get[String]("name"))
}
}
Scala Concepts
· Writing Future Method
· A Future gives you a simple way to run an algorithm concurrently. A future starts running
concurrently when you create it and returns a result at some point, well, in the future.
val f = Future {
ComplexOperation // takes 100 sec approx
42
}
· Case class
· Case classes are like regular classes with a few key differences which we will go over. Case
classes are good for modeling immutable data
case class Book(isbn: String)
val frankenstein = Book("978-0486282114")
Creating Part of Speech Tagger
def convertPos(data:String) = Future {
var result:String = ""
if(data.isEmpty)
Future.successful(BadRequest("Data is empty"))
try {
// converting option[x] to x
// val sentence = data.get
// tokenize the sentence
val projectPath = Play.application().path();
var tokenModelIn = new FileInputStream(projectPath+"/conf/resource/en-token.bin")
val tokenModel = new TokenizerModel(tokenModelIn)
val tokenizer = new TokenizerME(tokenModel)
val tokens = tokenizer.tokenize(data)
// Parts-Of-Speech Tagging
// reading parts-of-speech model to a stream
var posModelIn = new FileInputStream(projectPath+ "/conf/resource/en-pos-maxent.bin")
// loading the parts-of-speech model from stream
val posModel = new POSModel(posModelIn)
// initializing the parts-of-speech tagger with model
val posTagger = new POSTaggerME(posModel)
// Tagger tagging the tokens
val tags = posTagger.tag(tokens)
// Getting the probabilities of the tags given to the tokens
val probs = posTagger.probs
// Iterates the token, tag and probability score
// Flattening to list makes into string
result++= (0 until tokens.length).map(i => tokens(i) + "t:t" + tags(i) + "t:t" + probs(i) + "n").flatten.toList
} catch {
case e: IOException =>
// Model loading failed, handle the error
e.printStackTrace()
}
result.toString
}
Commonly used Command
· Sbt run
· Sbt compile
· Sbt update
· Sbt dist
References
· Scala Programming Language (Wikipedia Article)
· Scala official site
· Typesafe
· Programming in Scala Book on Amazon
· Functional Programming Principles in Scala on Coursera
· Principles of Reactive Programming on Coursera
· Play Framework Official Website
· Play Framework (Wikipedia Article)
· Typesafe Activator
Questions?
harinath@claritrics.com
CONTACT:

Weitere ähnliche Inhalte

Ähnlich wie Play Framework

Basic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsBasic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsGary Yeh
 
Solving anything in VCL
Solving anything in VCLSolving anything in VCL
Solving anything in VCLFastly
 
nodejs_at_a_glance.ppt
nodejs_at_a_glance.pptnodejs_at_a_glance.ppt
nodejs_at_a_glance.pptWalaSidhom1
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015Nir Noy
 
Copper: A high performance workflow engine
Copper: A high performance workflow engineCopper: A high performance workflow engine
Copper: A high performance workflow enginedmoebius
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Hugo Hamon
 
Distributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and ScalaDistributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and ScalaMax Alexejev
 
Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#caohansnnuedu
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsRichard Lee
 
Module design pattern i.e. express js
Module design pattern i.e. express jsModule design pattern i.e. express js
Module design pattern i.e. express jsAhmed Assaf
 
WebTalk - Implementing Web Services with a dedicated Java daemon
WebTalk - Implementing Web Services with a dedicated Java daemonWebTalk - Implementing Web Services with a dedicated Java daemon
WebTalk - Implementing Web Services with a dedicated Java daemonGeert Van Pamel
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Baruch Sadogursky
 
What's New in .Net 4.5
What's New in .Net 4.5What's New in .Net 4.5
What's New in .Net 4.5Malam Team
 
Apache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextApache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextPrateek Maheshwari
 
Introducing Node.js in an Oracle technology environment (including hands-on)
Introducing Node.js in an Oracle technology environment (including hands-on)Introducing Node.js in an Oracle technology environment (including hands-on)
Introducing Node.js in an Oracle technology environment (including hands-on)Lucas Jellema
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - TalkMatthias Noback
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing Techglyphs
 
Concurrency at the Database Layer
Concurrency at the Database Layer Concurrency at the Database Layer
Concurrency at the Database Layer mcwilson1
 

Ähnlich wie Play Framework (20)

Basic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsBasic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.js
 
Solving anything in VCL
Solving anything in VCLSolving anything in VCL
Solving anything in VCL
 
nodejs_at_a_glance.ppt
nodejs_at_a_glance.pptnodejs_at_a_glance.ppt
nodejs_at_a_glance.ppt
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
Copper: A high performance workflow engine
Copper: A high performance workflow engineCopper: A high performance workflow engine
Copper: A high performance workflow engine
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2
 
Distributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and ScalaDistributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and Scala
 
Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Module design pattern i.e. express js
Module design pattern i.e. express jsModule design pattern i.e. express js
Module design pattern i.e. express js
 
WebTalk - Implementing Web Services with a dedicated Java daemon
WebTalk - Implementing Web Services with a dedicated Java daemonWebTalk - Implementing Web Services with a dedicated Java daemon
WebTalk - Implementing Web Services with a dedicated Java daemon
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java
 
What's New in .Net 4.5
What's New in .Net 4.5What's New in .Net 4.5
What's New in .Net 4.5
 
Apache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextApache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's Next
 
Intro to Laravel 4
Intro to Laravel 4Intro to Laravel 4
Intro to Laravel 4
 
Introducing Node.js in an Oracle technology environment (including hands-on)
Introducing Node.js in an Oracle technology environment (including hands-on)Introducing Node.js in an Oracle technology environment (including hands-on)
Introducing Node.js in an Oracle technology environment (including hands-on)
 
Apache Flume (NG)
Apache Flume (NG)Apache Flume (NG)
Apache Flume (NG)
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - Talk
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
 
Concurrency at the Database Layer
Concurrency at the Database Layer Concurrency at the Database Layer
Concurrency at the Database Layer
 

Kürzlich hochgeladen

WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 

Kürzlich hochgeladen (20)

WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 

Play Framework

  • 1. Play Framework Understanding using Scala K. Harinath PRESENTED BY: ClariTrics Technologies
  • 2. Agenda · Introduction to Scala Play · Key Features · Anatomy of Scala Play Application · Architecture of Scala play · MVC Architecture in Scala play · Developing Rest API in Play Framework · Adding Dependency In Play Framework · Request URL Mapping in play · Reading Configuration File · Concurrent Programming in Scala · Packing for Production – Commonly used Commands
  • 3. Introduction to Play Framework · Is based on a lightweight, stateless, web-friendly, non blocking architecture. · Built on Akka, provides predictable and minimal resources consumption (CPU, memory, threads) for highly-scalable applications. · Lots of built in features for fast development. · Follows MVC architecture
  • 4. Features of Play Framework · Strong focus on productivity. Fast turnaround. · Hot reloading: Fix the bug and hit reload! · Type safe all the way, even templates and route files! · Use Scala or Java: Your choice. You can even mix them! · Predefined module for heavy lifting. Mail Service, Authentication · Play Provides full stack · Websocket support · Template Engine for views · Testing engine
  • 5. Installation of Play Framework · Dependencies · Java 1.8 · Sbt 1.1.0 · Intellji or Eclipse · Creating New Application · Using Play Starter Projects · Download Play Starter · Create a new application using SBT · Requires giter8
  • 6. Steps to Initialize Play Framework · Unzip the package and place in the desired location · Open the package in the IntelliJ editor File->Open · Open the terminal inside the IntelliJ editor · Run $sbt run - Process will take more time, please wait patiently · Application will be opened at port 9000
  • 7. Steps – Cont 1 · Download the File · Unzip
  • 8. Steps – Cont 2 · Open the Folder in Intellji
  • 9. Steps – Cont 3 · Wait till it sync · Open the terminal
  • 10. Steps – Cont 4 · Type sbt run · Application will be started at localhost:9000
  • 11. Anatomy of Play Framework
  • 13. Playing Around Play Framework · Accessing the End Point · GET http://localhost:9000 - HTML View Page · GET http://localhost:9000/count - Count gets incremented for each page refresh or request · GET http://localhost:9000/message - Displays Hi! Message · Important File to Remember in Play Framework · build.sbt – Adding third party dependency from Maven · Conf/applicaton.conf – Contains the Play framework settings · Conf/routes – Contains the Url Mapping · App folder holds the controllers, views, filters etc
  • 14. Creating Rest API using Play!
  • 15. Hello World using Play · Go to conf/routes · GET /greetings controllers.HelloWorldController.index · HelloWorldController – name of the controller class file · Index is the function name inside the controller class file · Create a controller called HelloWorldController.scala in app/controller package controllers import javax.inject._ import play.api.mvc._ /** * This controller creates an `Action` to handle HTTP requests to the * application's home page. */ @Singleton class HelloWorldController @Inject()(cc: ControllerComponents) extends AbstractController(cc) { /** Returns hello world */ def index = Action { Ok("Hello World") } }
  • 17. Adding Dependency in Play · Using build.sbt · // https://mvnrepository.com/artifact/org.apache.opennlp/opennlp-tools libraryDependencies += "org.apache.opennlp" % "opennlp-tools" % "1.8.3" · Using custom build jar · Placing the custom build jar or jar which are not available in the Maven repository. We should place those files inside lib in root location
  • 18. Play Terminology · Actions · A Action is basically a function that handles a request and generates a result to be sent to the client. · Controllers · A controller in Play is nothing more than an object that generates Action values. Controllers are typically defined as classes to take advantage of Dependency Injection. · Modules · Play uses public modules to augment built-in functionality.
  • 19. Returning Different Result Type · val textResult = Ok("Hello World!") · By default returns text/plain · val xmlResult = Ok(<message>Hello World!</message>) · Returns the result application/xml · val htmlResult2 = Ok(<h1>Hello World!</h1>).as(HTML) · Renders the HTML page
  • 20. HTTP Routing · Question ? · Which file is responsible for URL mapping?
  • 21. HTTP routing · The built-in HTTP router · The router is the component in charge of translating each incoming HTTP request to an Action. · An HTTP request is seen as an event by the MVC framework. This event contains two major pieces of information: · The request path (e.g. /clients/1542, /photos/list), including the query string · The HTTP method (e.g. GET, POST) · URI Pattern · Static Path · GET /clients/all controllers.Clients.list() · Dynamic Parts · GET /clients/:id controllers.Clients.show(id: Long) · Router Feature · What if I say you can do regex validation in route file itself !
  • 22. Workflow of the Request from Route file
  • 23. Configuration File · Play uses the Typesafe config library, but Play also provides a nice Scala wrapper called Configuration with more advanced Scala features. class MyController @Inject() (config: Configuration, c: ControllerComponents) extends AbstractController(c) { def getFoo = Action { Ok(config.get[String]("name")) } }
  • 24. Scala Concepts · Writing Future Method · A Future gives you a simple way to run an algorithm concurrently. A future starts running concurrently when you create it and returns a result at some point, well, in the future. val f = Future { ComplexOperation // takes 100 sec approx 42 } · Case class · Case classes are like regular classes with a few key differences which we will go over. Case classes are good for modeling immutable data case class Book(isbn: String) val frankenstein = Book("978-0486282114")
  • 25. Creating Part of Speech Tagger def convertPos(data:String) = Future { var result:String = "" if(data.isEmpty) Future.successful(BadRequest("Data is empty")) try { // converting option[x] to x // val sentence = data.get // tokenize the sentence val projectPath = Play.application().path(); var tokenModelIn = new FileInputStream(projectPath+"/conf/resource/en-token.bin") val tokenModel = new TokenizerModel(tokenModelIn) val tokenizer = new TokenizerME(tokenModel) val tokens = tokenizer.tokenize(data) // Parts-Of-Speech Tagging // reading parts-of-speech model to a stream var posModelIn = new FileInputStream(projectPath+ "/conf/resource/en-pos-maxent.bin") // loading the parts-of-speech model from stream val posModel = new POSModel(posModelIn) // initializing the parts-of-speech tagger with model val posTagger = new POSTaggerME(posModel) // Tagger tagging the tokens val tags = posTagger.tag(tokens) // Getting the probabilities of the tags given to the tokens val probs = posTagger.probs // Iterates the token, tag and probability score // Flattening to list makes into string result++= (0 until tokens.length).map(i => tokens(i) + "t:t" + tags(i) + "t:t" + probs(i) + "n").flatten.toList } catch { case e: IOException => // Model loading failed, handle the error e.printStackTrace() } result.toString }
  • 26. Commonly used Command · Sbt run · Sbt compile · Sbt update · Sbt dist
  • 27. References · Scala Programming Language (Wikipedia Article) · Scala official site · Typesafe · Programming in Scala Book on Amazon · Functional Programming Principles in Scala on Coursera · Principles of Reactive Programming on Coursera · Play Framework Official Website · Play Framework (Wikipedia Article) · Typesafe Activator