1. Trang chủ
  2. » Công Nghệ Thông Tin

Play framework Tien Nguyen

31 277 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 31
Dung lượng 3,81 MB

Nội dung

Play is a highproductivity Java and Scala web application framework that integrates the components and APIs you need for modern web application development. Play is based on a lightweight, stateless, webfriendly architecture and features predictable and minimal resource consumption (CPU, memory, threads) for highlyscalable applications thanks to its reactive model, based on Akka Streams.

Play framework VinaSaver – Tien Nguyen 2017 Play framework - Overview - Play is a high-productivity Java and Scala web application framework that integrates the components and APIs you need for modern web application development - Play is based on a lightweight, stateless, web-friendly architecture and features predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications thanks to its reactive model, based on Akka Streams Play Framework ADVANtages Play Framework  Like many frameworks, Play is based on the MVC model (model, view, controller)  Runs on the JVM, which has years of experience and optimizations  It's auto compiled, so you may expect better performance  It's really fullstack: can develop a web app, compiler, web server,…  It uses the asynchronous mechanism  … Play Framework Play framework and laravel Laravel Play Framework • Using MVC architecture (Makes code much more manageable and easier to navigate) • Using MVC architecture (Makes code much more manageable and easier to navigate) • Auto-compiling (No wait time to see the changes that are made being implemented) • Auto-compiling (Java doesn’t normally this but Play has built it in) • • Fullstack: can develop a web app, compiler, web server Fullstack: can develop a web app, compiler, web server • • Evolutions Migration • • Java/Scala language PHP language • • Open-source (Doesn’t seem to be quite as extensive as Laravels but still big) Open Source (Loads of great addons and libraries to enhance the functionality of the system without having to custom code) Play Framework Environment Setup On local server(1) Create virtual host to run your app by adding following rows to file /etc/httpd/conf/httpd.conf ProxyPreserveHost On ServerName www.demo-scala.com ProxyPass /excluded ! ProxyPass / http://127.0.0.1:9500/ ProxyPassReverse / http://127.0.0.1:9500/ Install Java Software Development Kit (SDK) and set your java environment Sbt SBT is an open source build tool for Scala and Java projects, similar to Java’s Maven or Ant To install sbt please refer to http://www.scala-sbt.org/ Play Framework Environment Setup On local server(2) Typesafe-activator Activator is build upon sbt and can everything that sbt can do, plus more Activator New is a fast way to create new Scala projects from a range of different templates and start building them immediately It replaces the Play command in Play Framework  Install activator Download activator: wget http://downloads.typesafe.com/typesafe-activator/1.3.2/typesafe-activator-1.3.2-minimal.zip And add the activator script to your PATH Ex: export PATH=$PATH:/usr/bin/activator-1.3.12/bin/  Create new project with exsiting template play-scala with command >activator new [project-name] play-scala >cd [project-name]  Run project or run with port 9500 Play Framework >activator run >activator “run 9500” Deploy On Product Server Using the dist task builds a binary version of your app that you can deploy to a server having Java installation >sbt dist This produces a ZIP file containing all JAR files in target/universal folder of your app To run the app, unzip the file on target server, then run the script in the bin directory The name of the script is your app name >unzip demo-scala-1.0.zip >demo-scala-1.0/bin/demo-scala –Dplay.scrypto.secret=abcdefghijk You can also specify a different configuration file for production environment >demo-scala-1.0/bin/demo-scala –Dconfig.file=/full/path/to/conf/application-prod.conf Play Framework Structure play framework project Source code includes those important folders and files “app” folder: All scala main code of project is put at here “build.sbt” file: In this file you declare plugins library that using in project “conf” folder: In this folder has important files - “application.conf”: Set config of all plugins that you declare in build.sbt - “routes”: Define all application routes “public” folder: Include using css, js, image files in project Play Framework Mvc in play framework Play Framework View (1)  Most of the application views are generated using an efficient templating system provided by play  The Controller gets some interesting data from the model layer, and then applies a template to decorate these objects  View are defined in the app/views folder  Following a simple naming convention (e.g user.scala.html)  Template parameters : A template is like a function, so it needs parameters, which must be declared at the top of the template file  View can using template available Play Framework 10 Database Use the ScalikeJDBC library to connect database(MySQL) Add the MySQL JDBC dependency to the build.sbt file libraryDependencies ++= Seq( jdbc) libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.24" Configuration mysql in conf/application.conf file db { default.driver = com.mysql.jdbc.Driver default.url = "jdbc:mysql://localhost/tien_scala“ default.username = root default.password = "vinasaver“ } Install ScalikeJDBC to run query, add to build.sbt libraryDependencies ++= Seq( "org.scalikejdbc" %% "scalikejdbc" % "2.4.1", "org.scalikejdbc" %% "scalikejdbc-config" % "2.4.1", "org.scalikejdbc" %% "scalikejdbc-play-initializer" % "2.5.1") Play Framework 17 evolutions   Evolutions is used to create relational databases for easy tracking and organization CREATE TABLE users ( Add evolutions into your dependencies list (in build.sbt): libraryDependencies += evolutions   # - !Ups Create evolutions script: The first script is named “1.sql”, the second script “2.sql”, and so on… and should be located in “conf/evolutions/default” If you agree with the SQL script, you can apply it directly by clicking on the ‘Apply evolutions’ button id int(10) NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, email varchar(255) NOT NULL, password varchar(255) NOT NULL, api_token varchar(60) NOT NULL, created_at timestamp, updated_at timestamp, PRIMARY KEY (id) ); Play Framework 18 REST API(1) Method : GET Step 1: Add url routes GET /all controllers.UsersController.all() Step 2: Create function all() def all() = Action { implicit request => implicit val userFormat = Json.format[Users] val list_user = Users.getAllUsers() Ok(Json.obj("success"-> true, "data" -> list_user, "error" -> "" ) ) } Play Framework 17 REST API(2) Method : POST Step 1: Add url routes POST /test controllers.UsersController.test() Step 2: Create function test() def test() = Action.async(parse.urlFormEncoded) { implicit request => implicit val userFormat = Json.format[Users] val email = request.body("email").head val password = request.body("password").head val list_user = Users.authenication(email,password) Future(Ok(Json.obj("success"-> true, "error" -> "" ) "data" -> list_user, ) ) } Play Framework 18 Demo App(1) Step 1: Set application routes In conf/routes set routes like GET /login controllers.AuthenicationController.login POST /login controllers.AuthenicationController.auth GET /logout controllers.AuthenicationController.logout GET / controllers.UsersController.index GET /user controllers.UsersController.user GET /edit/:id controllers.UsersController.editpage(id: Int) POST /edit/:id controllers.UsersController.edit(id: Int) GET controllers.UsersController.addpage() /add POST /add GET /delete/:id controllers.UsersController.add() controllers.UsersController.delete(id: Int) After that run command line > activator run To see how does it look like For more functionalities in this app please refer to source code Play Framework 21 Demo App(2) Create screen login (login.scala.html) @(message:String) @page("Login page") { @message ID

パパパパパ

パパパパパパ

  • ※ パパパパパパパパパパパパパパパパパパパパパパパパパ パパパパパパパパパパパパパパパパパパパパパパパパパパパパ
  • ※ パパパパパパパパパパパパパパパパパパパパパパ
  • ※ パパパパパパパパパパパパパパパパパパパパ パパパパパパパパパパパパパパパパパパパパパパパパパパパパパパ
} Play Framework 22 Demo App(3) Create Model Users (function authentication, updateApiToken) case class Users(id: Int, name: String, email: String, password: String, api_token: String) object Users extends SQLSyntaxSupport[Users]{ def md5(s: String) = { MessageDigest.getInstance("MD5").digest(s.getBytes).map("%02x".format(_)).mkString } def authenication(email: String, password: String)(implicit s: DBSession = AutoSession): Option[Users] = { sql"select id,name,email,password,api_token,created_at,updated_at from users where email = ${email} and password = ${md5(password)}" map { rs => Users(rs.int("id"),rs.string("name"),rs.string("email"),rs.string("password"),rs.string("api_token")) }.single.apply() } def updateApiToken(id: Int)(implicit s: DBSession = AutoSession):Boolean = { val api_token:String = System.currentTimeMillis().toString() val result_query = withSQL { update(Users).set(Users.column.api_token -> md5(api_token)).where.eq(Users.column.id, id) }.update.apply() if(result_query > 0) return true else return false } } Play Framework 23 Demo App(4) Create AuthenicationController class AuthenicationController extends Controller { def loginForm = Form(mapping("email" -> text, "password" -> text) (LoginRequest.apply)(LoginRequest.unapply)) case class LoginRequest(email:String, password:String) def login() = Action { request => val api_token = request.session.get("api_token").getOrElse("") val id_user = request.session.get("user_id").getOrElse("0") if (api_token.isEmpty || id_user.isEmpty){ Ok(views.html.login("")) }else{ val info_user = Users.checkLogin(id_user.toInt,api_token) if(info_user != None){ Redirect(routes.UsersController.user) }else{ Ok(views.html.login("")) } } } def auth = Action { implicit request => val loginRequest = loginForm.bindFromRequest.get; handleRespone(loginRequest) } def handleRespone(request: LoginRequest): Result = { val result = Users.authenication(request.email,request.password) Framework if(result != Play None){ val update_token = Users.updateApiToken(result.get.id.toInt) 24 Demo App(5) Screen Login : Login success -> List Users Play Framework 25 Demo App(6) Create Screen List Users @(user:List[Users], name: String) @main("Users",name) { Table of Users Create Id Name Email Action @for(t sql"select * from users" val api_token = request.session.get("api_token").getOrElse("") val id_user = request.session.get("user_id").getOrElse("0") val email_user = request.session.get("email").getOrElse("") map { rs => Users(rs.int("id"), rs.string("name"), rs.string("email"), rs.string("password"), rs.string("api_token")) }.list.apply() } if (api_token.isEmpty || id_user.isEmpty){ Ok(views.html.login("")) }else{ val info_user_login = Users.checkLogin(id_user.toInt,api_token) if(info_user_login != None){ val user = Users.getAllUsers() Ok(views.html.user(user,email_user)) }else{ Ok(views.html.login("")) } } } } Play Framework 27 Demo App(8) Model Update/Add/Delete Users def update_user(id: Int, name:String,email:String, password:String)(implicit s: DBSession = AutoSession):Boolean = { val result_query = withSQL { update(Users).set(Users.column.name -> name,Users.column.email -> email,Users.column.password -> md5(password)) where.eq(Users.column.id, id) }.update.apply() if(result_query > 0) return true else return false } def create(name: String, email: String, password : String)(implicit s: DBSession = AutoSession): Long = { sql"insert into users(name,email,password) values (${name}, ${email}, ${md5(password)})" updateAndReturnGeneratedKey.apply() // returns auto-incremeneted id } def deleteUser(id: Int)(implicit s: DBSession = AutoSession):Boolean = { val result_query = withSQL { delete.from(Users).where.eq(Users.column.id, id) }.update.apply() if(result_query > 0) return true else return false } Play Framework 28 Demo App(9) Create Screen Edit Users @(infoUser : Option[Users], message : String, name:String) @main("Edit page",name) { @for(t Framework //Check Login val info_user = Users.findById(id) if(info_user != None){ Ok(views.html.edituser(info_user,"",email_user)) }else{ Redirect(routes.UsersController.user) } } def userForm = Form(mapping("email" -> text, "username" -> text, "pwd" -> text, "rpwd" -> text)(UserRequest.apply)(UserRequest.unapply)) case class UserRequest(email:String, username:String, password:String, repassword:String) def edit(id : Int) = Action { implicit request => val id_user = request.session.get("user_id").getOrElse("0") val email_user = request.session.get("email").getOrElse("") val editRequest = userForm.bindFromRequest.get; handleEdit(editRequest,id,email_user) } def handleEdit(request: UserRequest,id: Int, email_user: String): Result = { if(request.password != request.repassword) { val info_user = Users.findById(id) if(info_user != None){ Ok(views.html.edituser(info_user,"Password and Re-Password fail",email_user)) }else{ Redirect(routes.UsersController.user) } }else{ Play Framework 30 Demo App(11) Function process add/delete user def addpage() = Action { request => //Check Login Ok(views.html.adduser("",email_user)) } def add() = Action { implicit request => val id_user = request.session.get("user_id").getOrElse("0") val email_user = request.session.get("email").getOrElse("") val addRequest = userForm.bindFromRequest.get; handleAdd(addRequest,id,email_user) } def handleAdd(request: UserRequest,id: Int, email_user: String): Result = { if(request.password != request.repassword) { Ok(views.html.adduser("Password and Re-Password fail",email_user)) }else{ val result = Users.create(request.username,request.email,request.password) if(result.toString.isEmpty){ Ok(views.html.adduser("Add user fail",email_user)) }else{ Redirect(routes.UsersController.user) } } } def delete(id: Int) = Action {request => val api_token = request.session.get("api_token").getOrElse("") val id_user = request.session.get("user_id").getOrElse("0") Play Framework if (api_token.isEmpty || id_user.isEmpty){ Ok(views.html.login("")) 31 ... files in project Play Framework Mvc in play framework Play Framework View (1)  Most of the application views are generated using an efficient templating system provided by play  The Controller... app, compiler, web server,…  It uses the asynchronous mechanism  … Play Framework Play framework and laravel Laravel Play Framework • Using MVC architecture (Makes code much more manageable... applications thanks to its reactive model, based on Akka Streams Play Framework ADVANtages Play Framework  Like many frameworks, Play is based on the MVC model (model, view, controller)  Runs

Ngày đăng: 17/08/2017, 09:21

TỪ KHÓA LIÊN QUAN

w