Skip to content
Browse files

switch to zappa

  • Loading branch information...
1 parent 9504118 commit 8f3802bf1ee57aaa3b85091146495c9373aeef56 @jedahan jedahan committed Sep 16, 2011
Showing with 39 additions and 51 deletions.
  1. +14 −39 src/faker.coffee
  2. +3 −4 src/public/scripts/lepidoptera.coffee
  3. +22 −8 src/server.coffee
View
53 src/faker.coffee
@@ -3,11 +3,6 @@
# **Faker** is a library for sending fake data to a swarm
#
-http = require 'http'
-express = require 'express'
-app = express.createServer()
-config = JSON.parse require('fs').readFileSync './config.json', 'utf8'
-
#### Constants
#
# center latitude and longitude, max distance, and max mpg
@@ -16,16 +11,6 @@ config = JSON.parse require('fs').readFileSync './config.json', 'utf8'
#
# To start faking data to a random swarm listed in config.json, `GET /add`
-app.get '/add', (req, res) ->
- fakeData()
-
-app.get '/:feed', (req, res) ->
- fakeData req.params.feed
-
-# To start faking data to a specific swarm, `GET /add/:swarm_id`
-app.get '/:feed/:swarm_id', (req, res) ->
- fakeData req.params.feed, req.params.swarm_id
-
#### Helpers
#
# **fakeData** pushes fake data to a swarm
@@ -34,8 +19,17 @@ app.get '/:feed/:swarm_id', (req, res) ->
#
# `{ latitude: -25.1, longitude: 40.1, mpg: 42, rpm: 2600, ... }`
#
+reqs = []
+
+fakeData = (config) ->
+ feed_name="ford"
+ swarm_id=config.swarms[Math.floor(Math.random() * config.swarms.length)]
+ http = require 'http'
+ max_distance = .01
+ center_latitude = (config.techcrunch.latitude - max_distance / 2)
+ center_longitude = (config.techcrunch.longitude - max_distance / 2)
+ cars = [ { name: "Fiesta", count: 0 }, { name: "Fusion", count: 0 } ]
-fakeData = (feed_name="ford", swarm_id=config.swarms[Math.floor(Math.random() * config.swarms.length)]) ->
if config.swarms.indexOf(swarm_id) > -1
options =
host: config.host
@@ -59,28 +53,9 @@ fakeData = (feed_name="ford", swarm_id=config.swarms[Math.floor(Math.random() *
req.write '\n'
reqs.push req
-process.on 'SIGTERM', ->
- process.exit 1
-
-process.on 'SIGINT', ->
- process.exit 1
-
-process.on 'exit', ->
- console.log 'sending presence unavailable'
- for req in reqs
- req.end
-
-
-process.on 'SIGTERM', ->
- process.exit 1
-
-process.on 'SIGINT', ->
- process.exit 1
-
-process.on 'exit', ->
- console.log 'sending presence "unavailable"'
- req.write JSON.stringify { presence: { type: "unavailable" } }
+removeFaker = ->
+ reqs[Math.floor(Math.random() * reqs.length)].end
+module.exports.add = fakeData
+module.exports.remove = removeFaker
-fakeData()
-app.listen 33
View
7 src/public/scripts/lepidoptera.coffee
@@ -3,9 +3,8 @@
# **Lepidoptera** is an example solution built on top of [Swarm](http://github.com/buglabs/swarm)
# This is the main message handler, which uses the DOM as the db, and jQuery as the ORM :-p
#
-
lepidoptera = ->
- # this config should be passed through using jade
+ # this @config should be passed through using jade
console.log "config: #{JSON.stringify config}"
# creating the google map
@@ -15,7 +14,7 @@ lepidoptera = ->
markers = []
# the javascript api handles message callbacks as a consumer only
- SWARM.join apikey: "#{config.consumer_key}", swarms: config.swarms, callback: (stanza) ->
+ SWARM.join apikey: "#{@config.consumer_key}", swarms: @config.swarms, callback: (stanza) ->
from = stanza.presence?.from or stanza.message?.from
resource_id = from?.split('/')[1]
@@ -50,7 +49,7 @@ lepidoptera = ->
dom_name = $("##{resource_id} > .car_name")
dom_name.html(data.car_name) if dom_name[0]?
- for feed in config.feeds
+ for feed in @config.feeds
# if the feed doesn't exist, add it
dom_feed = $("##{resource_id} > .feeds > .#{feed.name}")
View
30 src/server.coffee
@@ -16,20 +16,34 @@
#### Usage
#
# Make sure the configuration in `config.json` is correct, then browse
-# [http://localhost/locations](http://localhost/locations) to witness the magic
+# [http://localhost/](http://localhost/) to witness the magic
#
# You will also want to look at [Faker](faker.html) which can create said magic
-@app = require('zappa') ->
- gzippo = require 'gzippo'
+zappa = require 'zappa'
+config = JSON.parse require('fs').readFileSync 'config.json'
+
+zappa 80, {config}, ->
+ enable 'serve jquery'
configure ->
set 'view engine': 'jade'
set 'view options': { layout: false }
- app.register '.jade', zappa.adapter 'jade'
- use app.router, gzippo.staticGzip __dirname + '/public'
- listen process.env.npm_package_config_port or 8080
+ use app.router, 'static'
+
+ app.register '.jade', zappa.adapter 'jade'#, blacklist
+
- get '/locations': ->
- @config = JSON.parse require('fs').readFileSync './config.json', 'utf8'
+ get '/': ->
+ @config = config
render 'map'
+
+zappa 33, {config}, ->
+ def config: config
+ requiring 'faker'
+
+ get '/add': ->
+ faker.add(config)
+
+ get '/remove': ->
+ faker.remove()

0 comments on commit 8f3802b

Please sign in to comment.
Something went wrong with that request. Please try again.