Slide 1

Slide 1 text

Scala on Rails Yet Another Web Framework as a Scala Gateway Scala by the Bay 2016 Kazuhiro Sera (@seratch) SmartNews, Inc.

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

SmartNews https://www.smartnews.com/

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

What’s Skinny

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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!

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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…

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Redmine CRUD Generated code compiles without modifying them at all!

Slide 20

Slide 20 text

Tour of Skinny

Slide 21

Slide 21 text

Skinny Stack

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Queries Detected a typo in compilation time!

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Other Components

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

Scala.js Ready re-generates application-fastopt.js refers the generated JS file skinny comamnd which detects Scala.js code changes and compiles them

Slide 39

Slide 39 text

Scala.js Ready Source map works!

Slide 40

Slide 40 text

Reusable Skinny

Slide 41

Slide 41 text

Skinny Stack

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

Nothing’s changed

Slide 45

Slide 45 text

Task Runner

Slide 46

Slide 46 text

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)

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Try Skinny! Not only Web framework :)

Slide 50

Slide 50 text

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