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.

    View full-size slide

  2. I came from Tokyo, Japan!
    My second visit to SF!
    From Japan
    @seratch
    Kazuhiro Sera

    View full-size slide

  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 :)

    View full-size slide

  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

    View full-size slide

  5. SmartNews
    https://www.smartnews.com/

    View full-size slide

  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

    View full-size slide

  7. We’re hiring in SF
    http://about.smartnews.com/en/careers/
    SmartNews SF Office!
    (2nd St,)

    View full-size slide

  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

    View full-size slide

  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!)

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  12. What’s Skinny

    View full-size slide

  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

    View full-size slide

  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!

    View full-size slide

  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.

    View full-size slide

  16. Scaffolding
    Bootstrap3-based CRUD pages
    (You can customize the layout)
    Generated form with the validation rules
    corresponding to the table columns

    View full-size slide

  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…

    View full-size slide

  18. 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

    View full-size slide

  19. Redmine CRUD
    Generated code compiles
    without modifying them at all!

    View full-size slide

  20. Tour of Skinny

    View full-size slide

  21. Skinny Stack

    View full-size slide

  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

    View full-size slide

  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”}’

    View full-size slide

  24. Routing DSL
    skinny-framework/skinny-framework-example
    Sinatra DSL and action to run
    (#as can name a routing)
    Mount these definitiions
    to ServletContext

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  29. Entity/DAO
    a plain class as an entity
    Data Access Object

    View full-size slide

  30. Queries
    Detected a typo in compilation time!

    View full-size slide

  31. Helpful Logging
    Print the issued query and its spent time
    Print the stacktrace to help find
    where the query issued in Scala code

    View full-size slide

  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

    View full-size slide

  33. Scalate
    https://scalate.github.io/scalate/

    View full-size slide

  34. Other Components

    View full-size slide

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

    View full-size slide

  36. Skinny Mailer
    #deliver sends an email
    (highly insired by ActionMailer)
    Example to use GMail STMP
    http://skinny-framework.org/documentation/mail.html

    View full-size slide

  37. FactoryGirl
    FactoryGirl: fixture tool
    FactoryGirl reuses Skinny ORM DAO
    to prepare fixture data for testing.
    http://skinny-framework.org/documentation/factory-girl.html

    View full-size slide

  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

    View full-size slide

  39. Scala.js Ready
    Source map works!

    View full-size slide

  40. Reusable Skinny

    View full-size slide

  41. Skinny Stack

    View full-size slide

  42. Reusable Libraries
    Components are independent from Web framework.
    You can reuse them in any kind of app.

    View full-size slide

  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

    View full-size slide

  44. Nothing’s changed

    View full-size slide

  45. 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)

    View full-size slide

  46. Spray + skinny-validator
    Applying Skinny validator to
    a Spray request object

    View full-size slide

  47. Why not akka-http in 2016?
    • Work In Progress…
    • I’ll publish the initial version soon :)

    View full-size slide

  48. Try Skinny!
    Not only Web framework :)

    View full-size slide

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

    View full-size slide