Share
Embed
Scala Club: Modern Development in Scala
Basics of SBT
SBT plugins
REST API with Akka-HTTP
Alexey Novakov
January 07, 2020
Transcript
22 slides
-
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 -
AGENDA Basics of SBT SBT plugins REST API with Akka-HTTP
Alexey Novakov, Ultra Tendency, 2020 -
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 -
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 -
Scala IDEs IntelliJ + Scala Plugin VSCode + Metals (Scala
LSP) Alexey Novakov, Ultra Tendency, 2020 -
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 -
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 -
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 -
- SBT is using Coursier to download Ivy or Maven
dependencies Coursier - Pure Scala Artifact Fetching Alexey Novakov, Ultra Tendency, 2020 -
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 -
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) -
Run in shell: sbt new novakov-alexey/scalaboot.g8 Giter8 Template Includes: Akka-HTTP,
Circe and bunch of useful SBT plugins -
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 -
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 -
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 -
• Implement HTTP routes • Start HTTP Server • Test
API via client …. let’s dive into the code TODO -
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 -
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