Skip to content

Commit

Permalink
Insertion into MongoDB just works. Brilliant.
Browse files Browse the repository at this point in the history
  • Loading branch information
jponge committed Feb 23, 2012
1 parent 17b9092 commit d207bf5
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 17 deletions.
3 changes: 1 addition & 2 deletions buildfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ define 'finagle-test' do
package :jar
manifest['Main-Class'] = 'my.app.MyApp'

compile.with transitive(FINAGLE)
compile.with transitive(CASBAH)
compile.with transitive(FINAGLE), transitive(CASBAH)
end

task :execute => :package do
Expand Down
128 changes: 118 additions & 10 deletions finagle-test.iml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/scala-lang/scala-library/2.9.0-1/scala-library-2.9.0-1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/scala-lang/scala-library/2.9.1/scala-library-2.9.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Expand All @@ -23,7 +23,7 @@
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/scala-lang/scala-compiler/2.9.0-1/scala-compiler-2.9.0-1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/scala-lang/scala-compiler/2.9.1/scala-compiler-2.9.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Expand All @@ -32,7 +32,7 @@
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/finagle-http/1.11.1/finagle-http-1.11.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/finagle-http_2.9.1/1.11.1/finagle-http_2.9.1-1.11.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Expand All @@ -41,7 +41,7 @@
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/util-logging/1.12.13/util-logging-1.12.13.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/util-logging_2.9.1/1.12.13/util-logging_2.9.1-1.12.13.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Expand All @@ -50,7 +50,7 @@
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/util-core/1.12.13/util-core-1.12.13.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/util-core_2.9.1/1.12.13/util-core_2.9.1-1.12.13.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Expand All @@ -59,7 +59,16 @@
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/util-codec/1.12.13/util-codec-1.12.13.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/scala-lang/scala-library/2.9.1/scala-library-2.9.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/util-codec_2.9.1/1.12.13/util-codec_2.9.1-1.12.13.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Expand All @@ -86,7 +95,7 @@
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/finagle-core/1.11.1/finagle-core-1.11.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/finagle-core_2.9.1/1.11.1/finagle-core_2.9.1-1.11.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Expand All @@ -95,7 +104,7 @@
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/util-hashing/1.12.13/util-hashing-1.12.13.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/netty/netty/3.3.1.Final/netty-3.3.1.Final.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Expand All @@ -104,7 +113,7 @@
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/util-collection/1.12.13/util-collection-1.12.13.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/util-collection_2.9.1/1.12.13/util-collection_2.9.1-1.12.13.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Expand All @@ -131,7 +140,106 @@
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/io/netty/netty/3.3.1.Final/netty-3.3.1.Final.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/twitter/util-hashing_2.9.1/1.12.13/util-hashing_2.9.1-1.12.13.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/mongodb/casbah/casbah-query_2.9.0-1/2.1.5.0/casbah-query_2.9.0-1-2.1.5.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/mongodb/casbah/casbah-commons_2.9.0-1/2.1.5.0/casbah-commons_2.9.0-1-2.1.5.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.6.0/slf4j-api-1.6.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/mongodb/mongo-java-driver/2.5.3/mongo-java-driver-2.5.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/scalaj/scalaj-collection_2.8.0/1.0/scalaj-collection_2.8.0-1.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/scala-lang/scala-library/2.8.0/scala-library-2.8.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/scala-tools/time/time_2.8.0/0.2/time_2.8.0-0.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/joda-time/joda-time/1.6/joda-time-1.6.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/scala-lang/scala-library/2.9.0-1/scala-library-2.9.0-1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/mongodb/casbah/casbah-core_2.9.0-1/2.1.5.0/casbah-core_2.9.0-1-2.1.5.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/mongodb/casbah/casbah-gridfs_2.9.0-1/2.1.5.0/casbah-gridfs_2.9.0-1-2.1.5.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Expand Down
2 changes: 1 addition & 1 deletion finagle-test.ipr
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
<mapping directory="" vcs="Git" />
</component>
</project>

37 changes: 33 additions & 4 deletions src/main/scala/MyApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import java.net.InetSocketAddress
import com.twitter.util.Future
import scala.util.matching.Regex
import com.twitter.finagle.http.Http
import org.joda.time.DateTime

object MyApp {

Expand Down Expand Up @@ -101,30 +102,58 @@ object MyApp {
val keywordSplitter = "\\+".r
}

class SearchEngineFilter(val processor: String => Option[SearchEngineQuery]) extends SimpleFilter[HttpRequest, HttpResponse] {
class SearchEngineFilter(
val processor: String => Option[SearchEngineQuery],
val mongodb: MongoDBStore
) extends SimpleFilter[HttpRequest, HttpResponse] {

def apply(request: HttpRequest, service: Service[HttpRequest, HttpResponse]) = {

val query = processor(request.getUri())
if (query.isDefined) {
println(query.get)
mongodb.insert(query.get)
}

service(request)
}
}

class MongoDBStore(val host: String = "127.0.0.1", val port: Int = 27017) {

import com.mongodb.casbah.Imports._
import com.mongodb.casbah.commons.conversions.scala._
RegisterJodaTimeConversionHelpers()

val connection = MongoConnection(host, port)
val db = connection("HttpProxyQueries")

val COLLECTION = "queries"

def insert(query: SearchEngineQuery) {
val entry = MongoDBObject(
"when" -> new DateTime(),
"query" -> query.query,
"keywords" -> query.keywords
)
db(COLLECTION) += entry
}

}

def main(args: Array[String]) {

val handleExceptions = new HandleExceptions
val proxyClient = new ProxyHttpClient


val mongodb = new MongoDBStore()

val google = new GoogleSearch
val bing = new BingSearch
val yahoo = new YahooSearch
val wikipedia = new WikipediaSearch

val searchEngineProcessor = (google orElse bing orElse yahoo orElse wikipedia).lift
val searchEngineFilter = new SearchEngineFilter(searchEngineProcessor)
val searchEngineFilter = new SearchEngineFilter(searchEngineProcessor, mongodb)

val myService = handleExceptions andThen searchEngineFilter andThen proxyClient

Expand Down

0 comments on commit d207bf5

Please sign in to comment.