Skip to content

Commit

Permalink
Feat: redo arch layout (#9)
Browse files Browse the repository at this point in the history
* fix(all): redo the initial architecture layout

* fix(README): make `pre-push` executable

* fix(githooks): include tests in the `pre-push` script

* fix(Docker): use `.gitignore` & use `build.properties` & use `sbt-1.9.6` & use `Maven` repository

* refactor(all): clean `Dockerfile` & clean `build.sbt` & remove `.env` example
  • Loading branch information
sobakavosne authored Nov 5, 2024
1 parent 5df8b4b commit 336111d
Show file tree
Hide file tree
Showing 24 changed files with 160 additions and 45 deletions.
40 changes: 40 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Ignore SBT and IDE-specific files
.metals/
.metals.sbt
.bloop/
.vscode/
.idea/

# Ignore OS-specific files
.DS_Store
Thumbs.db

# Ignore build and target directories
target/
project/target/
project/.bloop/
project/.metals/
project/**/target/

# Ignore local caches
.coursier/
.ivy2/
.sbt/

# Ignore logs and temporary files
logs/
*.log
tmp/
task-temp-directory/

# Ignore example files and test results
examples/
test-reports/

# Ignore git and version control files
.git/
.gitignore

# Ignore any sensitive files (if applicable)
.env
secrets/
22 changes: 21 additions & 1 deletion .githooks/pre-push
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh

echo "Running scalafmt check..."
echo ""
sbt scalafmtCheckAll

EXIT_CODE=$?
Expand All @@ -16,7 +17,26 @@ fi

echo ""
echo "======================================"
echo "Code format check passed. Proceeding with push."
echo "Code format check passed. Running tests."
echo "======================================"
echo ""

sbt test

TEST_EXIT_CODE=$?

if [ $TEST_EXIT_CODE -ne 0 ]; then
echo ""
echo "======================================"
echo "Tests failed. Please fix the issues before pushing."
echo "======================================"
echo ""
exit 1
fi

echo ""
echo "======================================"
echo "All tests passed. Proceeding with push."
echo "======================================"
echo ""
exit 0
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
target/
project/target/
project/project/
project/build*
project/metals*
.vscode/

Expand Down
17 changes: 10 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ FROM openjdk:21-jdk-slim

WORKDIR /app

RUN \
apt-get update && apt-get install -y curl \
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
RUN curl -Lo /usr/local/bin/sbt-launch.jar https://repo1.maven.org/maven2/org/scala-sbt/sbt-launch/1.9.6/sbt-launch-1.9.6.jar

RUN echo '#!/bin/sh\nexec java -jar /usr/local/bin/sbt-launch.jar "$@"' > /usr/local/bin/sbt \
&& chmod +x /usr/local/bin/sbt

COPY project/build.properties project/build.properties

COPY build.sbt .
COPY project/ project/

COPY build.sbt .

RUN sbt update

COPY src/ ./src/

RUN sbt compile

COPY . .
Expand Down
3 changes: 2 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ A distributed system designed to manage and execute parallel requests to the Che
Git Hooks Setup

To enable custom Git hooks for this repository, configure Git to use the `.githooks` directory:


$> chmod +x .githooks/pre-push
$> git config --local core.hooksPath .githooks
13 changes: 6 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ 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")
enablePlugins(DockerPlugin, JavaAppPackaging)

Docker / packageName := "chemist-flow"
Docker / dockerExposedPorts := Seq(8081)

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

lazy val root = (project in file("."))
.settings(
name := ".",
name := "chemist-flow",
libraryDependencies ++= Seq(
scalaLogging,
scalaTest,
Expand All @@ -34,6 +32,7 @@ lazy val root = (project in file("."))
http4sDSL,
pureconfig.cross(CrossVersion.for3Use2_13),
akkaStream.cross(CrossVersion.for3Use2_13),
// akkaCluster.cross(CrossVersion.for3Use2_13)
// akkaActor.cross(CrossVersion.for3Use2_13),
// akkaTest.cross(CrossVersion.for3Use2_13),
// docker
Expand Down
2 changes: 0 additions & 2 deletions examples/.env.example

This file was deleted.

1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ object Dependencies {

lazy val akkaActor = "com.typesafe.akka" %% "akka-actor-typed" % akkaVersion
lazy val akkaStream = "com.typesafe.akka" %% "akka-stream" % akkaVersion
lazy val akkaCluster = "com.typesafe.akka" %% "akka-cluster" % akkaVersion
lazy val scalaLogging = "ch.qos.logback" % "logback-classic" % scalaLogVersion
lazy val log4cats = "org.typelevel" %% "log4cats-core" % log4catsVersion
lazy val docker = "com.spotify" % "docker-client" % dockerVersion
Expand Down
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.9.6
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package resource.api
package api

import cats.effect.IO
import cats.syntax.semigroupk.toSemigroupKOps
import org.http4s.HttpRoutes
import org.http4s.dsl.io._
import org.http4s.server.Router
import org.http4s.server.middleware.Logger
import org.http4s.circe.CirceEntityEncoder._
import org.http4s.circe.CirceEntityEncoder.circeEntityEncoder
import io.circe.syntax.EncoderOps
import io.circe.generic.auto._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource.api
package api

import cats.effect.IO
import org.http4s._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource.api
package api

import cats.effect.{IO, Resource}
import com.comcast.ip4s.{Host, Port}
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/app/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package app
import akka.actor.ActorSystem
import cats.effect.{ExitCode, IO, IOApp, Resource}
import com.comcast.ip4s.{Host, Port}
import resource.api.{Endpoints, ServerBuilder}
import resource.core.util.ConfigLoader
import api.{Endpoints, ServerBuilder}
import config.ConfigLoader
import org.typelevel.log4cats.slf4j.Slf4jLogger
import org.typelevel.log4cats.Logger
import scala.concurrent.ExecutionContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource.core.util
package config

import com.comcast.ip4s.{Host, Port}
import com.typesafe.config.{Config, ConfigFactory}
Expand Down Expand Up @@ -62,13 +62,13 @@ implicit val appConfigReader: ConfigReader[AppConfig] =
ConfigReader.forProduct3("kafka", "http", "database")(AppConfig.apply)

object ConfigLoader {
System.setProperty("logback.configurationFile", "src/main/scala/resource/core/configs/logback.xml")
System.setProperty("logback.configurationFile", "src/main/scala/resource/logback.xml")

private val refConf: Config =
ConfigFactory.parseFile(new File("src/main/scala/resource/core/configs/reference.conf"))
ConfigFactory.parseFile(new File("src/main/scala/resource/reference.conf"))

private val appConf: Config =
ConfigFactory.parseFile(new File("src/main/scala/resource/core/configs/application.conf"))
ConfigFactory.parseFile(new File("src/main/scala/resource/application.conf"))

private val config: Config =
appConf.withFallback(refConf).resolve()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource.core.domain
package core.domain

import io.circe.{Decoder, Encoder}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource.core.domain
package core.domain

import io.circe.{Decoder, Encoder}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Default Akka configuration (some settings may already be in application.conf)
akka {
loglevel = "INFO"
loggers = ["akka.event.slf4j.Slf4jLogger"]
logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"

remote {
artery {
Expand All @@ -16,9 +16,18 @@ akka {
lifecycle = on
}
}

coordinated-shutdown {
run-by-jvm-shutdown-hook = on
phases {
actor-system-terminate {
timeout = 2s # Timeout for actor system termination
}
}
}
}

# Default Kafka settings for the library
# Kafka configuration (does not need changes for logging consistency)
kafka {
producer {
retries = 3
Expand Down
10 changes: 0 additions & 10 deletions src/main/scala/utility/Common.scala

This file was deleted.

4 changes: 2 additions & 2 deletions src/test/scala/app/MainSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package app

import api.Endpoints
import api.ServerBuilder
import cats.effect.IO
import cats.effect.unsafe.implicits.global
import com.comcast.ip4s.Host
Expand All @@ -9,8 +11,6 @@ import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpec
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger
import resource.api.Endpoints
import resource.api.ServerBuilder

class MainSpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll {

Expand Down
54 changes: 54 additions & 0 deletions src/test/scala/config/ConfigLoaderSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package config

import com.comcast.ip4s.{Host, Port}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import pureconfig.error.ConfigReaderException
import pureconfig.ConfigSource

class ConfigLoaderSpec extends AnyWordSpec with Matchers {

"ConfigLoader" should {

"load the Kafka configuration correctly" in {
val kafkaConfig = ConfigLoader.kafkaConfig

kafkaConfig.bootstrapServers shouldBe "localhost:9092"
kafkaConfig.topic.reactions shouldBe "reactions-topic"
kafkaConfig.topic.mechanisms shouldBe "mechanisms-topic"
}

"load the HTTP configuration correctly" in {
val httpConfig = ConfigLoader.httpConfig

httpConfig.host shouldBe Host.fromString("0.0.0.0").get
httpConfig.port shouldBe Port.fromInt(8081).get
}

"load the Database configuration correctly" in {
val databaseConfig = ConfigLoader.databaseConfig
databaseConfig.url shouldBe "jdbc:postgresql://localhost:5432/chemist_db"
databaseConfig.user shouldBe "chemist_user"
databaseConfig.password shouldBe "chemist_password"
}

"load the entire AppConfig correctly" in {
val appConfig = ConfigLoader.appConfig

appConfig.kafka.bootstrapServers shouldBe "localhost:9092"
appConfig.http.host shouldBe Host.fromString("0.0.0.0").get
appConfig.http.port shouldBe Port.fromInt(8081).get
appConfig.database.url shouldBe "jdbc:postgresql://localhost:5432/chemist_db"
appConfig.database.user shouldBe "chemist_user"
appConfig.database.password shouldBe "chemist_password"
}

"fail gracefully if required configuration is missing" in {
intercept[ConfigReaderException[AppConfig]] {
val invalidConfigSource = ConfigSource.string("""{ invalidKey: "invalidValue" }""")

invalidConfigSource.loadOrThrow[AppConfig]
}
}
}
}

0 comments on commit 336111d

Please sign in to comment.