|
@@ -1,13 +1,16 @@
|
|
|
package scruffy.examples
|
|
|
|
|
|
-import com.mongodb.casbah.Imports._
|
|
|
-import com.sksamuel.scruffy.HttpEndpointProvider
|
|
|
import java.util.concurrent.ThreadLocalRandom
|
|
|
|
|
|
+import com.mongodb.casbah.Imports._
|
|
|
+import com.sksamuel.scruffy.HttpModule
|
|
|
+
|
|
|
/** @author Stephen Samuel */
|
|
|
-class Test2Endpoint() extends HttpEndpointProvider {
|
|
|
+object Test2Endpoint extends HttpModule {
|
|
|
+
|
|
|
+ import com.sksamuel.scruffy.jackson.ScruffyJackson.Implicits._
|
|
|
|
|
|
- val hostname = "database_host"
|
|
|
+ val hostname = "localhost"
|
|
|
val connection = MongoConnection(hostname, 27017)
|
|
|
val collection = connection.getDB("hello_world").getCollection("world")
|
|
|
|
|
@@ -17,13 +20,11 @@ class Test2Endpoint() extends HttpEndpointProvider {
|
|
|
//for ( k <- 1 to 10000 )
|
|
|
// collection.save(DBObject("_id" -> k, "id" -> k, "randomNumber" -> random.nextInt(10000).toDouble))
|
|
|
|
|
|
- get("db") { implicit req =>
|
|
|
- json {
|
|
|
- val id = ThreadLocalRandom.current.nextInt(10000)
|
|
|
- val dbo = collection.findOne(DBObject("_id" -> id), fields)
|
|
|
- val randomNumber = Math.round(dbo.get("randomNumber").toString.toFloat)
|
|
|
- Output(id, randomNumber)
|
|
|
- }
|
|
|
+ get("db") { req =>
|
|
|
+ val id = ThreadLocalRandom.current.nextInt(10000)
|
|
|
+ val dbo = collection.findOne(DBObject("_id" -> id), fields)
|
|
|
+ val randomNumber = Math.round(dbo.get("randomNumber").toString.toFloat)
|
|
|
+ Output(id, randomNumber).json
|
|
|
}
|
|
|
}
|
|
|
|