Skip to content

Commit

Permalink
Merge pull request #5 from mikolajkapica/feature/4
Browse files Browse the repository at this point in the history
Feature/4
  • Loading branch information
mikolajkapica authored Jun 23, 2024
2 parents ed0e5c0 + 9f876f8 commit b8e5c80
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 60 deletions.
38 changes: 38 additions & 0 deletions .do/app.yaml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 3 additions & 6 deletions api/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 0 additions & 2 deletions api/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ services:
- 8080:8080
environment:
- DATABASE_URL=mongodb://db:27017
stdin_open: true
tty: true
db:
image: mongo:7
ports:
Expand Down
24 changes: 3 additions & 21 deletions api/src/main/scala/Database.scala
Original file line number Diff line number Diff line change
@@ -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)

}
2 changes: 1 addition & 1 deletion api/src/main/scala/Helpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand Down
47 changes: 47 additions & 0 deletions api/src/main/scala/Router.scala
Original file line number Diff line number Diff line change
@@ -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)`, "<h1>Welcome to the API</h1>"))
}
} ~
path("hello") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
} ~
path("health") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>OK</h1>"))
}
} ~
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"<h1>DB Test</h1><p>Result: $result</p>",
)
)
}
}

}
17 changes: 0 additions & 17 deletions api/src/main/scala/Routes.scala

This file was deleted.

23 changes: 10 additions & 13 deletions api/src/main/scala/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

0 comments on commit b8e5c80

Please sign in to comment.