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

Alexey Novakov

January 07, 2020
Tweet

More Decks by Alexey Novakov

Other Decks in Programming

Transcript

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

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

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

    dependencies Coursier - Pure Scala Artifact Fetching Alexey Novakov, Ultra Tendency, 2020
  10. 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
  11. 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)
  12. 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
  13. 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
  14. 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
  15. • Implement HTTP routes • Start HTTP Server • Test

    API via client …. let’s dive into the code TODO
  16. 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
  17. 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