Slide 1

Slide 1 text

MODERN DEVELOPMENT IN SCALA Alexey Novakov, Ultra Tendency, 2020

Slide 2

Slide 2 text

GOAL Alexey Novakov, Ultra Tendency, 2020 Get an idea on how typical Scala application development looks like today and what are the basic tools for that

Slide 3

Slide 3 text

AGENDA Basics of SBT SBT plugins REST API with Akka-HTTP Alexey Novakov, Ultra Tendency, 2020

Slide 4

Slide 4 text

Scala Build Tool (SBT) Download: https://www.scala-sbt.org/download.html Project Template execute in shell: sbt new scala/scala-seed.g8 name [Scala Seed Project]: akka-http-api Giter8 allows to create your own project template and host on GitHub: sbt new novakov-alexey/scala-service.g8 —> https://github.com/novakov-alexey/scala-service.g8 Alexey Novakov, Ultra Tendency, 2020

Slide 5

Slide 5 text

SBT Tasks (commands) sbt clean - deletes previously built files sbt compile - compiles source code sbt test - run tests, if any exists sbt run - runs detected “Main” class sbt package - makes jar file … user can define new tasks in *.sbt files Alexey Novakov, Ultra Tendency, 2020

Slide 6

Slide 6 text

Scala IDEs IntelliJ + Scala Plugin VSCode + Metals (Scala LSP) Alexey Novakov, Ultra Tendency, 2020

Slide 7

Slide 7 text

Build Definition build.sbt Alexey Novakov, Ultra Tendency, 2020 import Dependencies._ ThisBuild / scalaVersion := "2.13.1" ThisBuild / version := "0.1.0-SNAPSHOT" ThisBuild / organization := "com.example" ThisBuild / organizationName := "example" lazy val root = (project in file(".")) .settings( name := "akka-http-api", libraryDependencies += scalaTest % Test ) Scope: build level, default for all sub-projects

Slide 8

Slide 8 text

Managed Dependencies import Dependencies._ lazy val root = (project in file(".")) .settings( libraryDependencies += scalaTest % Test ) import sbt._ object Dependencies { lazy val scalaTest = "org.scalatest" %% "scalatest" % “3.0.8" } Alexey Novakov, Ultra Tendency, 2020 build.sbt project/ Dependencies.scala

Slide 9

Slide 9 text

libraryDependencies += “org.scalatest" %% "scalatest" % "3.0.8" % Test groupID % artifactID % revision % configuration "org.scalatest" % “scalatest_2.13” % "3.0.8" % Test %% - matches current build Scala version Alexey Novakov, Ultra Tendency, 2020

Slide 10

Slide 10 text

- SBT is using Coursier to download Ivy or Maven dependencies Coursier - Pure Scala Artifact Fetching Alexey Novakov, Ultra Tendency, 2020

Slide 11

Slide 11 text

Example: REST API in Akka-HTTP libraryDependencies ++= Seq( akkaHttp, akkaStreams, … ) lazy val akkaHttp = "com.typesafe.akka" %% "akka-http" % "10.1.11" lazy val akkaStreams = “com.typesafe.akka" %% "akka-stream" % "2.6.1" Alexey Novakov, Ultra Tendency, 2020

Slide 12

Slide 12 text

HTTP Server • Akka-HTTP • Options: 1. Core Server API (HttpRequest => HttpResponse) 2. Routing DSL (Directives) • helps to make code DRY • but requires to learn a DSL (error-prone, time consuming)

Slide 13

Slide 13 text

Run in shell: sbt new novakov-alexey/scalaboot.g8 Giter8 Template Includes: Akka-HTTP, Circe and bunch of useful SBT plugins

Slide 14

Slide 14 text

HTTP Endpoints Get Order by id: GET /api/v1/exchange/ Create new Order: POST /api/v1/exchange (JSON body) final case class Quote(bid: BigDecimal, ask: BigDecimal, ticker: String) final case class Order(id: Long, quotes: List[Quote]) Alexey Novakov, Ultra Tendency, 2020

Slide 15

Slide 15 text

SBT Plugins

Slide 16

Slide 16 text

sbt-revolver - restarts running application automatically, if source files are modified addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1") sbt “~reStart” - enables a super-fast development turnaround for your Scala applications

Slide 17

Slide 17 text

sbt-tpolecat addSbtPlugin( “io.github.davidgregory084" % "sbt-tpolecat" % “0.1.10" ) - enables scalac options according to Rob Noris recommendations. - P.S. the same options can be enabled manually

Slide 18

Slide 18 text

• Implement HTTP routes • Start HTTP Server • Test API via client …. let’s dive into the code TODO

Slide 19

Slide 19 text

sbt-native-packager SBT native packager lets you build application packages in native formats: •Universal zip,tar.gz, xz archives •deb and rpm packages •dmg •msi •docker images •graalvm native images # universal zip sbt universal:packageBin # docker image sbt docker:publishLocal

Slide 20

Slide 20 text

sbt-release addSbtPlugin( "com.github.gseitz" % "sbt-release" % “1.0.12" ) - provides customisable release process - steps to configure: - increment scala project version - run tests - check git branch has no uncommitted changes - push new git tag - create new snapshot version

Slide 21

Slide 21 text

Thanks. Q & A https://github.com/novakov-alexey Twitter: @alexey_novakov

Slide 22

Slide 22 text

IMAGES 1. Drone: https://unsplash.com/photos/0RzFRYMEosc 2. Puzzle: https://unsplash.com/photos/sOK9NjLArCw 3. Basket: https://unsplash.com/photos/j6brni7fpvs