Share
Embed
Transcript
50 slides
-
Scala on Rails Yet Another Web Framework as a Scala
Gateway Scala by the Bay 2016 Kazuhiro Sera (@seratch) SmartNews, Inc. -
I came from Tokyo, Japan! My second visit to SF!
From Japan @seratch Kazuhiro Sera -
[PR] ScalaMatsuri 2017 • Matsuri := Festival • The largest
international conference in Asia • 500+ attendees this year (2017 will be held at the same venue) • Travel support for speakers! • Niseko season! • We’ll welcome you :) -
Who Am I • Back-end engineer at SmartNews, Inc. •
Working on back-end services on the JVM • Scala Enthusiast for over 5 years • Active maintainer of Scalatra, json4s, Scalate • ScalikeJDBC, a tidy RDB library • Skinny, a full-stack web framework -
SmartNews on the JVM ɾHeavily using Java/Scala to build our
backend ɾApache Spark for large data analysis ɾFinagle/Play/Skinny are used in Ads systems -
Who Am I • Back-end engineer at SmartNews, Inc. •
Working on back-end services on the JVM • Scala Enthusiast for over 5 years • Active maintainer of Scalatra, json4s, Scalate • ScalikeJDBC, a tidy RDB library • Skinny, a full-stack web framework -
2015/01/01 - 2016/11/05 GitHub contributions except merge commits I think
I’m the most active developer on Scalatra, json4s and Scalate now. (Scalate was resumed recently!) -
Who Am I • Back-end engineer at SmartNews, Inc. •
Working on back-end services on the JVM • Scala Enthusiast for over 5 years • Active maintainer of Scalatra, json4s, Scalate • ScalikeJDBC, a tidy RDB library • Skinny, a full-stack web framework -
We’re 2.12 Ready • try to always publish libs ASAP
not to be a blocker for upgrading Scala • json4s 3.4.2, 3.5.0 • Scalate 1.8.0 • Scalatra 2.5.0 • ScalikeJDBC 2.5.0 • Skinny 2.3.0 -
Skinny Web Framework • The design is highly inspired by
Ruby on Rails • Full-stack: used very often -> out-of-the-box • validator, i18n, ORM, database migration, template engine, mail sender, job workers, task runner, built- in OAuth, fronend support, testing, fixture tool, etc. • Has most of the things Rails + major gems cover -
Getting Started brew install skinny skinny new hello-world cd hello-world/
skinny run skinny command: a thin wrapper of sbt It simplifies the commands. Most of commands internally runs sbt tasks. Homebrew formura is available! -
Scaffolding skinny g scaffold \ tasks task \ title:String \
description:Option[String] \ deadline:Option[LocalDate] *** Skinny Generator Task *** "src/main/scala/controller/ApplicationController.scala" skipped. "src/main/scala/controller/TasksController.scala" skipped. "src/test/scala/controller/TasksControllerSpec.scala" skipped. "src/test/scala/integrationtest/TasksController_IntegrationTestSpec.scala" skipped. "src/main/scala/model/Task.scala" skipped. "src/test/scala/model/TaskSpec.scala" skipped. "src/main/webapp/WEB-INF/views/tasks/_form.html.ssp" skipped. "src/main/webapp/WEB-INF/views/tasks/new.html.ssp" skipped. "src/main/webapp/WEB-INF/views/tasks/edit.html.ssp" skipped. "src/main/webapp/WEB-INF/views/tasks/index.html.ssp" skipped. "src/main/webapp/WEB-INF/views/tasks/show.html.ssp" skipped. "src/main/resources/db/migration/V20161107001151__Create_tasks_table.sql" created. skinny g(generate) \ plural name, singular name \ fields… (name:type) Running skinny db:migrate is needed to apply the generated database migration. -
Scaffolding Bootstrap3-based CRUD pages (You can customize the layout) Generated
form with the validation rules corresponding to the table columns -
Reverse Scaffoding • Rails doesn’t have this feature :) •
Generating CRUD pages from existing database tables by fetching metadata and translating them to the corresponding Scala code • reverse-scaffold-all command scans all tables in the schema! • Seeing is believing… -
Redmine CRUD • Let’s generate CRUD for all tables for
Redmine • Watch http://showterm.io/07c7af9acdc87c50ee955 git clone [email protected]:redmine/redmine.git cd redmine bin/bundle install cp -p config/database.yml.example config/database.yml mysql -uroot -e "create database redmine_development" bin/rake db:migrate skinny new redmine-reverse-scaffold cd redmine-reverse-scaffold # build.sbt: add MySQL JDBC driver # src/main/resources/application.conf: fix development db settings ./skinny g reverse-scaffold-all -
Web App • Built upon Skinny Micro (a forked Scalatra)
• Routing DSLs similar to Sinatra • Smooth Form Validation • Recommended Security Headers (X-***) • CRUD Controller like Rails Resource Routing http://skinny-framework.org/documentation/controller-and-routes.html -
Skinny Micro skinny-framework/skinny-micro-heroku-example Tiny Web Application Toolkit with embedded Jetty
server HTTP server is running! curl -XPOST localhost:4567/prettify -d’{“name”:”Alice”}’ -
Routing DSL skinny-framework/skinny-framework-example Sinatra DSL and action to run (#as
can name a routing) Mount these definitiions to ServletContext -
Web Controller skinny-framework/skinny-framework-example Action method which lists items with pagination
#render returns response body (JSON string) Also possible to return ActionResult like other Scala frameworks -
Form Validation render the view template with errors accept request
params came from Skinny Micro layer overriden #validate() method does putting errorMessages into the view template if validation errors are found -
SkinnyResource If you’re OK with the default behavior, You don’t
need to maintain controllers except the definition of params and validation rules. Also possible to customize the default implementation by overriding methods -
Skinny ORM • Highly inspired by Rails ActiveRecord • Auto
timestamps, Soft Deletion, Optimistic Lock • ScalikeJDBC: Lower layer which enables handling JDBC stuff in a more Scala way • Typical Quries: Skinny ORM for labor saving • Special Cases: ScalikeJDBC for more flexibility http://skinny-framework.org/documentation/view-templates.html -
Helpful Logging Print the issued query and its spent time
Print the stacktrace to help find where the query issued in Scala code -
Template Engine • The default choice is Scalate Template Engine
• SSP: similar to Velocity, JSP, ERB • Jade, Scaml(Haml in Scala), Mustache • FreeMaker, Thymeleaf are also available • If you prefer twirl, it’s also possible http://skinny-framework.org/documentation/view-templates.html -
Skinny Mailer #deliver sends an email (highly insired by ActionMailer)
Example to use GMail STMP http://skinny-framework.org/documentation/mail.html -
FactoryGirl FactoryGirl: fixture tool FactoryGirl reuses Skinny ORM DAO to
prepare fixture data for testing. http://skinny-framework.org/documentation/factory-girl.html -
Scala.js Ready re-generates application-fastopt.js refers the generated JS file skinny
comamnd which detects Scala.js code changes and compiles them -
Reusable Libraries Components are independent from Web framework. You can
reuse them in any kind of app. -
Play Integration • GitHub: skinny-framework/skinny-orm-in-play • Lightbend Activator Template •
How to use Skinny ORM in Play apps • database migration with Flyway (optional) • Resuable model classes -
Spray Integration • GitHub: skinny-framework/skinny-splash • Enable using skinny-validator, skinny-json4s
• skinny-validator: readable DSL to define validation rules, i18n error messages in HOCON syntax • skinny-json4s: (if you need to use json4s rather than spray-json) -
Why not akka-http in 2016? • Work In Progress… •
I’ll publish the initial version soon :)