Skip to content

Instantly share code, notes, and snippets.

View adamw's full-sized avatar

Adam Warski adamw

View GitHub Profile
/*
SBT dependencies:
val sttpVersion = "1.5.19"
libraryDependencies += Seq(
"com.softwaremill.sttp" %% "core" % sttpVersion,
"com.softwaremill.sttp" %% "circe" % sttpVersion,
)
*/
val buildContainerCommand = Seq(
"docker",
"build",
"-t",
dockerGraalvmNativeImageName.value,
"-f",
(baseDirectory.value.getParentFile / "run-native-image" / "Dockerfile")
.getAbsolutePath,
resultDir.absolutePath
)
val runNativeImageCommand = Seq(
"docker",
"run",
"--rm",
"-v",
s"${cpDir.getAbsolutePath}:/opt/cp",
"-v",
s"${resultDir.getAbsolutePath}:/opt/graalvm",
"graalvm-native-image",
"-cp",
val stageDir = target.value / "native-docker" / "stage"
stageDir.mkdirs()
// copy all jars to the staging directory
val cpDir = stageDir / "cp"
cpDir.mkdirs()
val classpathJars = Seq((packageBin in Compile).value) ++
(dependencyClasspath in Compile).value.map(_.data)
classpathJars.foreach(cpJar => Files.copy(
// build.sbt
val dockerGraalvmNative = taskKey[Unit](
"Create a docker image containing a binary build with GraalVM's native-image.")
import monix.eval.Task
import monix.execution.Scheduler.Implicits.global
object Hello extends App {
val task = Task(
println(s"Hello, world from thread: ${Thread.currentThread().getName}!")
)
(for {
fiber1 <- task.start
_ <- task
_ <- fiber1.join

Can you use tapir with plain java?

No. tapir relies on a couple of Scala-specific features, which won't be possible to port to Java. One example is type-safety. Every time we add a new input, with a value which maps to some request part (query, header, path, ...), the type of the value what we create is extended. This is then used to verify at compile-time that the business logic function has the correct signature - takes values of appropriate types as input as returns either the error or successful output.

I’m just wondering what do you think about using Lombok annotations in Java, do you find level of control over what happens in code with such additions enough?

When we're doing projects in Java, we use Lombok. Definitely better than generating all those getters & setters. However, it's still a hack to make Java a better language. Lombok annotations give you a good level of control, but personally when possible, I simply prefer to use a better, more powerful language (such as Scala).

Does two-s

import tapir.docs.openapi._
import tapir.openapi.circe.yaml._
import tapir.openapi.OpenAPI
val openApi: OpenAPI = List(getBooks, getBookCover, addBook)
.toOpenAPI("The tapir library", "0.29.192-beta-RC1")
val yml: String = openApi.toYaml
val getBooks: Endpoint[BooksQuery, (StatusCode, ErrorInfo), List[Book], Nothing] =
baseEndpoint
.get
.in("books")
.in(booksQueryInput)
.out(jsonBody[List[Book]].example(List(Database.books.head)))
def startServer(): Unit = {
import scala.concurrent.Await
import scala.concurrent.duration._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.Http
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer