diff --git a/.do/app.yaml b/.do/app.yaml new file mode 100644 index 0000000..30f6a58 --- /dev/null +++ b/.do/app.yaml @@ -0,0 +1,38 @@ +alerts: + - rule: DEPLOYMENT_FAILED + - rule: DOMAIN_FAILED +databases: + - cluster_name: elevator-system-db + engine: MONGODB + name: elevator-system-db + production: true + version: "7" +features: + - buildpack-stack=ubuntu-22 +ingress: + rules: + - component: + name: elevator-system-api + match: + path: + prefix: /api +name: dolphin-app +region: fra +services: + - dockerfile_path: /api/Dockerfile + envs: + - key: DATABASE_URL + scope: RUN_TIME + value: ${elevator-system-db.DATABASE_URL} + - key: CA_CERT + scope: RUN_TIME + value: ${elevator-system-db.CA_CERT} + github: + branch: feature/4 + deploy_on_push: true + repo: mikolajkapica/elevator-system + http_port: 8080 + instance_count: 2 + instance_size_slug: apps-s-1vcpu-1gb + name: elevator-system-api + source_dir: api diff --git a/api/build.sbt b/api/build.sbt index 811ba02..4bde5b1 100644 --- a/api/build.sbt +++ b/api/build.sbt @@ -6,19 +6,16 @@ resolvers += "Akka library repository" at "https://repo.akka.io/maven" enablePlugins(JavaAppPackaging) -val scalatestVersion = "3.2.18" val AkkaVersion = "2.9.3" -val AkkaHttpVersion = "10.6.3" -val MongoDBVersion = "5.1.0" libraryDependencies ++= { Seq( - "org.scalatest" %% "scalatest" % scalatestVersion % "test", + "org.scalatest" %% "scalatest" % "3.2.18" % "test", "com.typesafe.akka" %% "akka-actor-typed" % AkkaVersion, "com.typesafe.akka" %% "akka-stream" % AkkaVersion, - "com.typesafe.akka" %% "akka-http" % AkkaHttpVersion, + "com.typesafe.akka" %% "akka-http" % "10.6.3", "org.slf4j" % "slf4j-simple" % "2.0.13", ) } -libraryDependencies += ("org.mongodb.scala" %% "mongo-scala-driver" % MongoDBVersion).cross( +libraryDependencies += ("org.mongodb.scala" %% "mongo-scala-driver" % "5.1.0").cross( CrossVersion.for3Use2_13 ) diff --git a/api/docker-compose.yaml b/api/docker-compose.yaml index 4682422..1e2f616 100644 --- a/api/docker-compose.yaml +++ b/api/docker-compose.yaml @@ -8,8 +8,6 @@ services: - 8080:8080 environment: - DATABASE_URL=mongodb://db:27017 - stdin_open: true - tty: true db: image: mongo:7 ports: diff --git a/api/src/main/scala/Database.scala b/api/src/main/scala/Database.scala index 338d729..4783b16 100644 --- a/api/src/main/scala/Database.scala +++ b/api/src/main/scala/Database.scala @@ -1,27 +1,9 @@ -import org.mongodb.scala.Document import org.mongodb.scala.MongoClient -import org.mongodb.scala.MongoCollection - -import tour.Helpers._ +import org.mongodb.scala.MongoDatabase object Database { - def test(): Unit = - println("Connecting to MongoDB...") - val mongoClient = MongoClient(sys.env("DATABASE_URL")) - val database = mongoClient.getDatabase("mydb") - database.createCollection("test2") - val collection: MongoCollection[Document] = database.getCollection("test2") - - val document = Document( - "name" -> "MongoDB", - "type" -> "database", - "count" -> 1, - "info" -> Document("x" -> 203, "y" -> 102), - ) - - collection.insertOne(document).results() - collection.find().printResults() - println("Document inserted") + def connect(db_name: String): MongoDatabase = MongoClient(sys.env("DATABASE_URL")) + .getDatabase(db_name) } diff --git a/api/src/main/scala/Helpers.scala b/api/src/main/scala/Helpers.scala index eaafb52..3849f6c 100644 --- a/api/src/main/scala/Helpers.scala +++ b/api/src/main/scala/Helpers.scala @@ -42,7 +42,7 @@ object Helpers { def headResult() = Await.result(observable.head(), Duration(10, TimeUnit.SECONDS)) def printResults(initial: String = ""): Unit = { - if (initial.length > 0) + if (initial.nonEmpty) print(initial) results().foreach(res => println(converter(res))) } diff --git a/api/src/main/scala/Router.scala b/api/src/main/scala/Router.scala new file mode 100644 index 0000000..438d462 --- /dev/null +++ b/api/src/main/scala/Router.scala @@ -0,0 +1,47 @@ +import Database.connect +import akka.http.scaladsl.model.ContentTypes +import akka.http.scaladsl.model.HttpEntity +import akka.http.scaladsl.server.Directives.complete +import akka.http.scaladsl.server.Directives.get +import akka.http.scaladsl.server.Directives.path +import akka.http.scaladsl.server.Directives.pathSingleSlash +import akka.http.scaladsl.server.Route +import akka.http.scaladsl.server.Directives._enhanceRouteWithConcatenation +import org.mongodb.scala.bson.collection.immutable.Document +import tour.Helpers._ + +object Router { + + val route: Route = + pathSingleSlash { + get { + complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "

Welcome to the API

")) + } + } ~ + path("hello") { + get { + complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "

Say hello to akka-http

")) + } + } ~ + path("health") { + get { + complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "

OK

")) + } + } ~ + path("db-test") { + get { + val db = connect("test") + db.createCollection("test") + db.getCollection("test").insertOne(Document("name" -> "test")).results() + val result = db.getCollection("test").find().results().map(_.toJson).mkString(", ") + + complete( + HttpEntity( + ContentTypes.`text/html(UTF-8)`, + s"

DB Test

Result: $result

", + ) + ) + } + } + +} diff --git a/api/src/main/scala/Routes.scala b/api/src/main/scala/Routes.scala deleted file mode 100644 index ee863cc..0000000 --- a/api/src/main/scala/Routes.scala +++ /dev/null @@ -1,17 +0,0 @@ -import akka.http.scaladsl.model.ContentTypes -import akka.http.scaladsl.model.HttpEntity -import akka.http.scaladsl.server.Directives.complete -import akka.http.scaladsl.server.Directives.get -import akka.http.scaladsl.server.Directives.path -import akka.http.scaladsl.server.Route - -object Routes { - - val route: Route = - path("hello") { - get { - complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "

Say hello to akka-http

")) - } - } - -} diff --git a/api/src/main/scala/Server.scala b/api/src/main/scala/Server.scala index 1c1a902..9de3913 100644 --- a/api/src/main/scala/Server.scala +++ b/api/src/main/scala/Server.scala @@ -2,29 +2,26 @@ import akka.actor.typed.ActorSystem import akka.actor.typed.scaladsl.Behaviors import akka.http.scaladsl.Http -import scala.concurrent.ExecutionContextExecutor -import scala.io.StdIn +import scala.concurrent.Await +import scala.concurrent.Promise +import scala.concurrent.duration.Duration object Server { - def run(): Unit = - implicit val system: ActorSystem[Any] = ActorSystem(Behaviors.empty, "my-system") - implicit val executionContext: ExecutionContextExecutor = system.executionContext + def run() = + implicit val system = ActorSystem(Behaviors.empty, "api") + implicit val executionContext = system.executionContext val host = "0.0.0.0" val port = 8080 val bindingFuture = Http() .newServerAt(host, port) - .bind(Routes.route) + .bind(Router.route) - println(s"Server online at http://$host:$port/\nPress RETURN to stop...") + println(s"Server online at http://$host:$port/") - Database.test() - - StdIn.readLine() - bindingFuture - .flatMap(_.unbind()) - .onComplete(_ => system.terminate()) + val keepAlive = Promise[Unit].future + Await.result(keepAlive, Duration.Inf) }