-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7edd15f
commit 13ae20d
Showing
5 changed files
with
131 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())}" | ||
) | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)`, "<h1>Say hello to akka-http</h1>")) | ||
} | ||
} | ||
|
||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)`, "<h1>Say hello to akka-http</h1>")) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
|
||
} |