From 13ae20d0245d2fb8f038c530f66e368bde9008b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kapica?= Date: Sat, 22 Jun 2024 19:38:41 +0200 Subject: [PATCH] Check the db connection --- api/src/main/scala/Database.scala | 27 +++++++++++++++ api/src/main/scala/Helpers.scala | 56 +++++++++++++++++++++++++++++++ api/src/main/scala/Main.scala | 36 +------------------- api/src/main/scala/Routes.scala | 17 ++++++++++ api/src/main/scala/Server.scala | 30 +++++++++++++++++ 5 files changed, 131 insertions(+), 35 deletions(-) create mode 100644 api/src/main/scala/Database.scala create mode 100644 api/src/main/scala/Helpers.scala create mode 100644 api/src/main/scala/Routes.scala create mode 100644 api/src/main/scala/Server.scala diff --git a/api/src/main/scala/Database.scala b/api/src/main/scala/Database.scala new file mode 100644 index 0000000..338d729 --- /dev/null +++ b/api/src/main/scala/Database.scala @@ -0,0 +1,27 @@ +import org.mongodb.scala.Document +import org.mongodb.scala.MongoClient +import org.mongodb.scala.MongoCollection + +import tour.Helpers._ + +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") + +} diff --git a/api/src/main/scala/Helpers.scala b/api/src/main/scala/Helpers.scala new file mode 100644 index 0000000..eaafb52 --- /dev/null +++ b/api/src/main/scala/Helpers.scala @@ -0,0 +1,56 @@ +/* + * Copyright 2008-present MongoDB, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tour + +import java.util.concurrent.TimeUnit + +import scala.concurrent.Await +import scala.concurrent.duration.Duration + +import org.mongodb.scala._ + +object Helpers { + + implicit class DocumentObservable[C](val observable: Observable[Document]) + extends ImplicitObservable[Document] { + override val converter: (Document) => String = doc => doc.toJson + } + + implicit class GenericObservable[C](val observable: Observable[C]) extends ImplicitObservable[C] { + override val converter: (C) => String = doc => Option(doc).map(_.toString).getOrElse("") + } + + trait ImplicitObservable[C] { + val observable: Observable[C] + val converter: (C) => String + + def results(): Seq[C] = Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS)) + def headResult() = Await.result(observable.head(), Duration(10, TimeUnit.SECONDS)) + + def printResults(initial: String = ""): Unit = { + if (initial.length > 0) + print(initial) + results().foreach(res => println(converter(res))) + } + + def printHeadResult(initial: String = ""): Unit = println( + s"${initial}${converter(headResult())}" + ) + + } + +} diff --git a/api/src/main/scala/Main.scala b/api/src/main/scala/Main.scala index 6babf3d..17dd7d4 100644 --- a/api/src/main/scala/Main.scala +++ b/api/src/main/scala/Main.scala @@ -1,37 +1,3 @@ -import akka.actor.typed.ActorSystem -import akka.actor.typed.scaladsl.Behaviors -import akka.http.scaladsl.Http -import akka.http.scaladsl.model.* -import akka.http.scaladsl.server.Directives.* - -import scala.concurrent.ExecutionContextExecutor -import scala.io.StdIn - object Main { - - def main(args: Array[String]): Unit = { - implicit val system: ActorSystem[Any] = ActorSystem(Behaviors.empty, "my-system") - implicit val executionContext: ExecutionContextExecutor = system.executionContext - - val host = "0.0.0.0" - val port = 8080 - - val route = - path("hello") { - get { - complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "

Say hello to akka-http

")) - } - } - - val bindingFuture = Http() - .newServerAt(host, port) - .bind(route) - - println(s"Server online at http://$host:$port/\nPress RETURN to stop...") - StdIn.readLine() - bindingFuture - .flatMap(_.unbind()) // trigger unbinding from the port - .onComplete(_ => system.terminate()) // and shutdown when done - } - + def main(args: Array[String]): Unit = Server.run() } diff --git a/api/src/main/scala/Routes.scala b/api/src/main/scala/Routes.scala new file mode 100644 index 0000000..ee863cc --- /dev/null +++ b/api/src/main/scala/Routes.scala @@ -0,0 +1,17 @@ +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 new file mode 100644 index 0000000..1c1a902 --- /dev/null +++ b/api/src/main/scala/Server.scala @@ -0,0 +1,30 @@ +import akka.actor.typed.ActorSystem +import akka.actor.typed.scaladsl.Behaviors +import akka.http.scaladsl.Http + +import scala.concurrent.ExecutionContextExecutor +import scala.io.StdIn + +object Server { + + def run(): Unit = + implicit val system: ActorSystem[Any] = ActorSystem(Behaviors.empty, "my-system") + implicit val executionContext: ExecutionContextExecutor = system.executionContext + + val host = "0.0.0.0" + val port = 8080 + + val bindingFuture = Http() + .newServerAt(host, port) + .bind(Routes.route) + + println(s"Server online at http://$host:$port/\nPress RETURN to stop...") + + Database.test() + + StdIn.readLine() + bindingFuture + .flatMap(_.unbind()) + .onComplete(_ => system.terminate()) + +}