Created
June 4, 2011 17:13
-
-
Save bohde/1008081 to your computer and use it in GitHub Desktop.
Simple GET Param to CouchDB app
This file contains hidden or 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
_ = require 'underscore' | |
express = require 'express' | |
cradle = require 'cradle' | |
class Queue | |
# Class to store the queue documents to store in Couch. | |
constructor: -> | |
_.bindAll @, 'flush' | |
@reqs = [] | |
@db = new(cradle.Connection)().database('analytics') | |
setInterval @flush, 1000 | |
# If a value is a string representation of a float, | |
# let's convert it. | |
transform: (query) -> | |
ret = {} | |
for name, value of query | |
ret[name] = parseFloat(value) or value | |
ret | |
# Add a request to be flushed | |
push: (req) -> | |
serialized = _.extend {}, @transform(req.query), | |
timestamp: new Date() | |
'user-agent': req.headers['user-agent'] | |
'referer': req.headers['referer'] | |
ip: req.connection.remoteAddress | |
@reqs.push serialized | |
# Save pending requests to Couch, | |
# outputting to the console so it can be recovered | |
# in case something bad happens | |
flush: -> | |
if @reqs and @reqs.length | |
console.log @reqs | |
@db.save @reqs | |
@reqs = [] | |
queue = new Queue() | |
app = express.createServer() | |
app.get '/tracking.gif', (req, res) -> | |
res.header 'Cache-Control', 'private, no-cache, proxy-revalidate' | |
res.send() | |
queue.push(req) | |
app.listen 3000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment