Permalink
Browse files

Async fetching + storing in MongoDB

  • Loading branch information...
swaroopch committed Jul 27, 2013
1 parent d3e7206 commit b4da6889ad3729cf0a4fc6337c31c530cff887be
Showing with 276 additions and 95 deletions.
  1. +5 −2 project.clj
  2. +10 −30 resources/book.mustache
  3. +31 −3 src/isbnnetinclj2/handler.clj
  4. +44 −0 src/isbnnetinclj2/mongo.clj
  5. +186 −60 src/isbnnetinclj2/store.clj
View
@@ -5,17 +5,20 @@
:url "https://www.apache.org/licenses/LICENSE-2.0.html"}
:min-lein-version "2.0.0"
:dependencies [[org.clojure/clojure "1.5.1"] ; https://github.com/clojure/clojure
+ [org.clojure/core.cache "0.6.3"] ; http://clojure.github.io/core.cache/
[compojure "1.1.5"] ; https://github.com/weavejester/compojure
[ring/ring-jetty-adapter "1.2.0"] ; https://github.com/ring-clojure/ring
[com.taoensso/timbre "2.4.1"] ; https://github.com/ptaoussanis/timbre
- [com.taoensso/carmine "2.0.0"] ; https://github.com/ptaoussanis/carmine
+ [com.novemberain/monger "1.6.0"] ; http://clojuremongodb.info
[stencil "0.3.2"] ; https://github.com/davidsantiago/stencil
[enlive "1.1.1"] ; https://github.com/cgrand/enlive
[cheshire "5.2.0"] ; https://github.com/dakrone/cheshire
[clj-time "0.5.1"] ; https://github.com/clj-time/clj-time
]
:plugins [[lein-ring "0.8.5"]]
:main isbnnetinclj2.handler
- :ring {:handler isbnnetinclj2.handler/app}
+ :ring {:handler isbnnetinclj2.handler/app
+ :init isbnnetinclj2.handler/initialize
+ :destroy isbnnetinclj2.handler/destroy}
:profiles
{:dev {:dependencies [[ring-mock "0.1.5"]]}})
View
@@ -3,6 +3,7 @@
<div class="container-fluid">
<div class="row-fluid">
<div class="span4 offset2">
+ {{#info}}
<dl>
{{#isbn}}
<dt>ISBN</dt>
@@ -33,9 +34,11 @@
<dt>Binding</dt>
<dd>{{binding}}</dd>
{{/binding}}
+ {{/info}}
</dl>
</div>
<div class="span4">
+ {{#info}}
<p>
{{#image}}
{{#imageLink}}
@@ -49,8 +52,9 @@
alt="{{title}}"
title="Image {{#imageSource}}from {{imageSource}}{{/imageSource}} for {{#title}}{{title}}{{/title}}"/>
{{/imageLink}}
- {{/image}}
+ {{/image}}
</p>
+ {{/info}}
</div>
</div>
</div>
@@ -60,36 +64,12 @@
<div class="span4 offset2">
<h3>Prices</h3>
<table class="table table-bordered table-hover">
- {{#priceFlipkart}}
+ {{#price}}
<tr>
- <th>Flipkart</th>
- <td>{{priceFlipkart}}</td>
- </tr>
- {{/priceFlipkart}}
- {{#priceInfibeam}}
- <tr>
- <th>Infibeam</th>
- <td>{{priceInfibeam}}</td>
- </tr>
- {{/priceInfibeam}}
- {{#priceHomeshop18}}
- <tr>
- <th>Homeshop18</th>
- <td>{{priceHomeshop18}}</td>
- </tr>
- {{/priceHomeshop18}}
- {{#priceSnapdeal}}
- <tr>
- <th>Snapdeal</th>
- <td>{{priceSnapdeal}}</td>
- </tr>
- {{/priceSnapdeal}}
- {{#priceAmazonIndia}}
- <tr>
- <th>AmazonIndia</th>
- <td>{{priceAmazonIndia}}</td>
- </tr>
- {{/priceAmazonIndia}}
+ <th>{{name}}</th>
+ <td>{{amount}}</td>
+ </tr>
+ {{/price}}
</table>
</div>
</div>
@@ -1,30 +1,58 @@
(ns isbnnetinclj2.handler
- (:require [compojure.core :refer :all]
+ (:require [taoensso.timbre :as log]
+ [compojure.core :refer :all]
[compojure.handler :as handler]
[compojure.route :as route]
[stencil.core :as mus]
+ [stencil.loader]
[ring.adapter.jetty :as jetty]
+ [clojure.string :as string]
+ [clojure.core.cache]
+ [isbnnetinclj2.mongo :as mongo]
[isbnnetinclj2.store :as store]))
+
(defn front-page
[]
- (mus/render-file "frontpage" {:pageTitle "isbn.net.in"}))
+ (mus/render-file "frontpage" {:pageTitle "isbn.net.in"
+ :stores (sort (map
+ (comp string/capitalize name)
+ (keys store/stores)))}))
+
(defn about-page
[]
(mus/render-file "about" {:pageTitle "About isbn.net.in"}))
+
(defroutes app-routes
(GET "/" [] (front-page))
(GET "/about/" [] (about-page))
(GET ["/:isbn" :isbn #"[\d-]+[xX]?"] [isbn] (store/book-page isbn))
(route/resources "/")
(route/not-found "Not Found"))
+
+(defn initialize
+ []
+ (mongo/init)
+ (when-not (= (System/getenv "ENVIRONMENT") "production")
+ ;; https://github.com/davidsantiago/stencil#manual-cache-management
+ (stencil.loader/set-cache (clojure.core.cache/ttl-cache-factory {} :ttl 0))))
+
+
+(defn destroy
+ []
+ (mongo/destroy))
+
+
(def app
(handler/site app-routes))
+
(defn -main []
+ (initialize)
(jetty/run-jetty app-routes
- {:port (Integer. (or (System/getenv "PORT") 8080))
+ {:port (Integer/parseInt (or (System/getenv "PORT")
+ "8080"))
:join? false}))
@@ -0,0 +1,44 @@
+(ns isbnnetinclj2.mongo
+ (:require [monger.core :as mg]
+ [monger.collection :as mc]
+ [monger.query :as mq]
+ [monger.joda-time]
+ [monger.json]
+ [clj-time.core :as time]
+ [isbnnetinclj2.utils :as utils]))
+
+
+(defn uri
+ []
+ (or (System/getenv "MONGOLAB_URI") ; https://addons.heroku.com/mongolab
+ "mongodb://127.0.0.1:27017/isbnnetinclj2"))
+
+
+(defn init
+ []
+ (mg/connect-via-uri! (uri)))
+
+
+(defn destroy
+ []
+ (mg/disconnect!))
+
+
+(def data-collection "data")
+
+
+(defn get-recent-entry
+ [isbn]
+ (first
+ (mq/with-collection
+ data-collection
+ (mq/find {:isbn isbn :when {"$gt" (utils/twenty-four-hours-ago)}})
+ (mq/sort {:when -1})
+ (mq/limit 1))))
+
+
+(defn create-new-entry
+ [data]
+ (mc/insert-and-return
+ data-collection
+ data))
Oops, something went wrong.

0 comments on commit b4da688

Please sign in to comment.