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

Scala Club: Modern Development in Scala

Scala Club: Modern Development in Scala

Basics of SBT

SBT plugins

REST API with Akka-HTTP

7a04b88e1469561db6da3818348d4b8f?s=128

Alexey Novakov

January 07, 2020
Tweet

Transcript

  1. MODERN DEVELOPMENT IN SCALA Alexey Novakov, Ultra Tendency, 2020

  2. 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
  3. AGENDA Basics of SBT SBT plugins REST API with Akka-HTTP

    Alexey Novakov, Ultra Tendency, 2020
  4. 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
  5. 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
  6. Scala IDEs IntelliJ + Scala Plugin VSCode + Metals (Scala

    LSP) Alexey Novakov, Ultra Tendency, 2020
  7. 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
  8. 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
  9. 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
  10. - SBT is using Coursier to download Ivy or Maven

    dependencies Coursier - Pure Scala Artifact Fetching Alexey Novakov, Ultra Tendency, 2020
  11. 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
  12. 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)
  13. Run in shell: sbt new novakov-alexey/scalaboot.g8 Giter8 Template Includes: Akka-HTTP,

    Circe and bunch of useful SBT plugins
  14. HTTP Endpoints Get Order by id: GET /api/v1/exchange/<id> 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
  15. SBT Plugins

  16. 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
  17. 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
  18. • Implement HTTP routes • Start HTTP Server • Test

    API via client …. let’s dive into the code TODO
  19. 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
  20. 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
  21. Thanks. Q & A https://github.com/novakov-alexey Twitter: @alexey_novakov

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