Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add Docker support #2

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bsp/sbt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"sbt","version":"1.9.1","bspVersion":"2.1.0-M1","languages":["scala"],"argv":["/usr/lib/jvm/java-21-openjdk-amd64/bin/java","-Xms100m","-Xmx100m","-classpath","/home/dm/.cache/coursier/arc/https/github.com/sbt/sbt/releases/download/v1.9.6/sbt-1.9.6.zip/sbt/bin/sbt-launch.jar","-Dsbt.script=/home/dm/.cache/coursier/arc/https/github.com/sbt/sbt/releases/download/v1.9.6/sbt-1.9.6.zip/sbt/bin/sbt","xsbt.boot.Boot","-bsp"]}
16 changes: 16 additions & 0 deletions .github/workflows/scala.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@ name: Scala CI
on:
push:
branches: ["master"]
paths-ignore:
- "**/*.txt"
- "**/*.sh"
- "examples/**"
- "migrations/**"
- "scripts/**"
- "Dockerfile*"
- "docker*"
pull_request:
branches: ["master"]
paths-ignore:
- "**/*.txt"
- "**/*.sh"
- "examples/**"
- "migrations/**"
- "scripts/**"
- "Dockerfile*"
- "docker*"

permissions:
contents: read
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ project/metals*

*.class
*.log

.env
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM openjdk:21-jdk-slim

WORKDIR /app

RUN \
apt-get update && apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/*

RUN \
curl -Lo sbt.deb https://repo.scala-sbt.org/scalasbt/debian/sbt-1.9.1.deb \
&& dpkg -i sbt.deb \
&& rm sbt.deb

COPY build.sbt .
COPY project/ project/

RUN sbt update

COPY src/ ./src/
RUN sbt compile

COPY . .

EXPOSE 8081

CMD ["sbt", "run"]
10 changes: 9 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@ ThisBuild / organizationName := "chemist.flow"
Compile / mainClass := Some("app.Main")
Compile / scalaSource := baseDirectory.value / "src" / "main" / "scala"

enablePlugins(DockerPlugin)
enablePlugins(JavaAppPackaging)
dockerExposedPorts ++= Seq(8081)
Docker / packageName := "chemist-flow"
// dockerEnvVars ++= Map(("CHEMIST_FLOW_HOST", "localhost"), ("CHEMIST_FLOW_PORT", "8081"))
// dockerExposedVolumes := Seq("/opt/docker/.logs", "/opt/docker/.keys")

Test / scalaSource := baseDirectory.value / "src" / "test" / "scala"

lazy val root = (project in file("."))
.settings(
name := ".",
libraryDependencies ++= Seq(
scalaLogging,
akkaStream,
scalaTest,
akkaActor,
akkaStream,
akkaHttp,
akkaTest,
docker,
spray
)
)
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
chemist-flow:
container_name: chemist-flow
build:
context: .
dockerfile: Dockerfile
ports:
- "8081:8081"
environment:
- CHEMIST_FLOW_HOST
- CHEMIST_FLOW_PORT
stdin_open: true
tty: true
2 changes: 2 additions & 0 deletions examples/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CHEMIST_FLOW_HOST=0.0.0.0
CHEMIST_FLOW_PORT=8081
2 changes: 2 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ object Dependencies {
lazy val scalaTestVersion = "3.2.15"
lazy val scalaLogVersion = "1.2.11"
lazy val sprayVersion = "1.3.6"
lazy val dockerVersion = "8.9.0"

lazy val akkaActor = "com.typesafe.akka" %% "akka-actor-typed" % akkaVersion
lazy val akkaStream = "com.typesafe.akka" %% "akka-stream" % akkaVersion
lazy val akkaHttp = "com.typesafe.akka" %% "akka-http" % akkaHttpVersion
lazy val scalaLogging = "ch.qos.logback" % "logback-classic" % scalaLogVersion
lazy val spray = "io.spray" %% "spray-json" % sprayVersion
lazy val docker = "com.spotify" % "docker-client" % dockerVersion
lazy val akkaTest = "com.typesafe.akka" %% "akka-testkit" % akkaVersion % Test
lazy val scalaTest = "org.scalatest" %% "scalatest" % scalaTestVersion % Test
}
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0")
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.4")
resolvers += "Scalafmt Releases" at "https://oss.sonatype.org/content/repositories/releases"
10 changes: 7 additions & 3 deletions src/main/scala/app/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ object Main extends App {
implicit val system: ActorSystem = ActorSystem("ChemistActorSystem")
implicit val materializer: Materializer = Materializer(system)

val host = sys.env.getOrElse("CHEMIST_FLOW_HOST", "0.0.0.0")
val port = sys.env.getOrElse("CHEMIST_FLOW_PORT", "8081").toInt

val endpoints = new Endpoints()
endpoints.startServer("localhost", 8080)
endpoints.startServer(host, port)

println("Press ENTER to exit...")
scala.io.StdIn.readLine()

system.terminate()
sys.addShutdownHook {
system.terminate()
}
}
21 changes: 11 additions & 10 deletions src/main/scala/resource/api/Endpoints.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import akka.http.scaladsl.server.Directives._
import akka.actor.ActorSystem
import scala.concurrent.ExecutionContextExecutor
import scala.concurrent.Future
import scala.util.{Failure, Success}

class Endpoints(implicit system: ActorSystem) {
implicit val executionContext: ExecutionContextExecutor = system.dispatcher
Expand All @@ -13,18 +14,14 @@ class Endpoints(implicit system: ActorSystem) {
path("health")(get(complete("Health check response")))

private def getReactionRoute = path("reaction" / Segment) { id =>
get {
complete(s"Get reaction details for ID: $id")
}
get(complete(s"Get reaction details for ID: $id"))
}

private def postReactionRoute =
path("reaction")(post(complete("Create new reaction")))

private def deleteReactionRoute = path("reaction" / Segment) { id =>
delete {
complete(s"Delete reaction with ID: $id")
}
delete(complete(s"Delete reaction with ID: $id"))
}

val routes = pathPrefix("api") {
Expand All @@ -37,10 +34,14 @@ class Endpoints(implicit system: ActorSystem) {
def startServer(interface: String, port: Int): Future[Http.ServerBinding] = {
val bindingFuture = Http().newServerAt(interface, port).bind(routes)

bindingFuture.foreach { binding =>
println(
s"Server online at http://${binding.localAddress.getHostName}:${binding.localAddress.getPort}/"
)
bindingFuture.onComplete {
case Success(binding) =>
println(
s"Server online at http://${binding.localAddress.getHostName}:${binding.localAddress.getPort}/"
)
case Failure(exception) =>
println(s"Failed to bind HTTP server: ${exception.getMessage}")
system.terminate()
}

bindingFuture
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/app/MainSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class MainSpec extends AnyWordSpec with Matchers with BeforeAndAfterAll {
"start the ActorSystem and HTTP server" in {
val endpoints = new Endpoints()
val bindingFuture: Future[Http.ServerBinding] =
endpoints.startServer("localhost", 8080)
endpoints.startServer("0.0.0.0", 8081)

val binding = Await.result(bindingFuture, 5.seconds)

binding.localAddress.getPort shouldEqual 8080
binding.localAddress.getPort shouldEqual 8081

// More tests here to verify specific endpoint behavior
}
Expand Down
Loading