Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scala on Rails @ Scalae by the Bay 2016 #scalae

Scala on Rails @ Scalae by the Bay 2016 #scalae

Kazuhiro Sera

November 11, 2016
Tweet

More Decks by Kazuhiro Sera

Other Decks in Programming

Transcript

  1. Scala on Rails Yet Another Web Framework as a Scala

    Gateway Scala by the Bay 2016 Kazuhiro Sera (@seratch) SmartNews, Inc.
  2. I came from Tokyo, Japan! My second visit to SF!

    From Japan @seratch Kazuhiro Sera
  3. [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 :)
  4. 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
  5. 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
  6. 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
  7. 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!)
  8. 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
  9. 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
  10. 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
  11. 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!
  12. 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.
  13. Scaffolding Bootstrap3-based CRUD pages (You can customize the layout) Generated

    form with the validation rules corresponding to the table columns
  14. 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…
  15. 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
  16. 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
  17. 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”}’
  18. Routing DSL skinny-framework/skinny-framework-example Sinatra DSL and action to run (#as

    can name a routing) Mount these definitiions to ServletContext
  19. 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
  20. 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
  21. 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
  22. 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
  23. Helpful Logging Print the issued query and its spent time

    Print the stacktrace to help find where the query issued in Scala code
  24. 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
  25. Skinny Mailer #deliver sends an email (highly insired by ActionMailer)

    Example to use GMail STMP http://skinny-framework.org/documentation/mail.html
  26. FactoryGirl FactoryGirl: fixture tool FactoryGirl reuses Skinny ORM DAO to

    prepare fixture data for testing. http://skinny-framework.org/documentation/factory-girl.html
  27. Scala.js Ready re-generates application-fastopt.js refers the generated JS file skinny

    comamnd which detects Scala.js code changes and compiles them
  28. 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
  29. 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)
  30. Why not akka-http in 2016? • Work In Progress… •

    I’ll publish the initial version soon :)