Scala on Rails @ Scalae by the Bay 2016 #scalae

Scala on Rails @ Scalae by the Bay 2016 #scalae

132fe0f031849e12eea7ce74f99b90f0?s=128

Kazuhiro Sera

November 11, 2016
Tweet

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 https://www.smartnews.com/

  6. 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
  7. We’re hiring in SF http://about.smartnews.com/en/careers/ SmartNews SF Office! (2nd St,)

  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. 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!)
  10. 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
  11. 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
  12. What’s Skinny

  13. 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
  14. 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!
  15. 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.
  16. Scaffolding Bootstrap3-based CRUD pages (You can customize the layout) Generated

    form with the validation rules corresponding to the table columns
  17. 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…
  18. Redmine CRUD • Let’s generate CRUD for all tables for

    Redmine • Watch http://showterm.io/07c7af9acdc87c50ee955 git clone git@github.com: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
  19. Redmine CRUD Generated code compiles without modifying them at all!

  20. Tour of Skinny

  21. Skinny Stack

  22. 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
  23. 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”}’
  24. Routing DSL skinny-framework/skinny-framework-example Sinatra DSL and action to run (#as

    can name a routing) Mount these definitiions to ServletContext
  25. 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
  26. 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
  27. 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
  28. 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
  29. Entity/DAO a plain class as an entity Data Access Object

  30. Queries Detected a typo in compilation time!

  31. Helpful Logging Print the issued query and its spent time

    Print the stacktrace to help find where the query issued in Scala code
  32. 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
  33. Scalate https://scalate.github.io/scalate/

  34. Other Components

  35. OAuth Integration • Google, GitHub, Facebook, Dropbox, Twitter, etc.

  36. Skinny Mailer #deliver sends an email (highly insired by ActionMailer)

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

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

    comamnd which detects Scala.js code changes and compiles them
  39. Scala.js Ready Source map works!

  40. Reusable Skinny

  41. Skinny Stack

  42. Reusable Libraries Components are independent from Web framework. You can

    reuse them in any kind of app.
  43. 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
  44. Nothing’s changed

  45. Task Runner

  46. 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)
  47. Spray + skinny-validator Applying Skinny validator to a Spray request

    object
  48. Why not akka-http in 2016? • Work In Progress… •

    I’ll publish the initial version soon :)
  49. Try Skinny! Not only Web framework :)

  50. Learn More • http://skinny-framework.org/ • http://scalikejdbc.org/ • https://gitter.im/skinny-framework/skinny-framework • https://gitter.im/scalikejdbc/en