-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.coffee
82 lines (68 loc) · 2.34 KB
/
main.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
express = require 'express'
dataset = require './lib/dataset'
util = require 'util'
fs = require 'fs'
coffee = require 'coffee-script'
app = express.createServer()
app.use express.bodyParser()
app.use express.static(__dirname + '/static')
app.listen(3000)
io = require 'socket.io'
iosocket = io.listen app
global_sockets = []
subscriptions = []
iosocket.sockets.on 'test', (d) =>
console.log "test", d
iosocket.sockets.on 'connection',(newsocket) =>
id = "#{newsocket.id}"
console.log "got id #{id}"
global_sockets[id] = newsocket
ds = new dataset.Dataset
list = []
iosocket.sockets.on 'disconnect', (oldsocket) =>
id = "#{oldsocket.id}"
console.log "Disconnecting #{id}"
delete global_sockets[id]
iosocket.sockets.on 'subscribe', (name) =>
console.log "subscription incoming", name
console.log "name: #{util.inspect name}"
subscriptions[name.name].push
app.get '/sets/:name', (req,res) ->
ds = new dataset.Dataset
res.send {name: req.params.name}, 200
app.get '/sets',(req,res) ->
ds = new dataset.Dataset
foo = ds.list (sets) =>
console.log "get /sets", util.inspect sets
res.send {href: '/sets',sets: sets}, 200
app.post '/sets', (req,res) ->
ds = new dataset.Dataset
console.log "post /sets", req.body
ds.new req.body.set.name
loc = "http://localhost:3000/sets/#{req.body.set.name}"
res.header "Location", loc
res.send {status: 201, message: "created", href: loc},201
app.post '/sets/:name/data', (req, res) =>
obj = { set: req.params.name, data: {} }
obj.data.value = req.body.value
obj.data.timestamp = new Date(req.body.timestamp)
obj.data.attributes = req.body.attributes if req.body.attributes?
ds = new dataset.Dataset
# for id, sock of global_sockets
# sock.emit 'data', obj
# ds.insert obj, req.params.name
res.send
status: 201,
message: "added",
href: "http://localhost:3000/sets/#{req.params.name}/data"
#app.get '/', (req,res) ->
# res.sendfile __dirname + '/static/index.html'
app.get '/javascript/:script.js', (req, res) ->
res.header 'Content-Type', 'application/x-javascript'
cs = fs.readFileSync "#{__dirname}/coffee/#{req.params.script}.coffee", "ascii"
js = coffee.compile cs
res.send js
app.get '/css/:style.css', (req, res) ->
res.header 'Content-Type', 'text/css'
css = fs.readFileSync "#{__dirname}/css/#{req.params.style}.css", "ascii"
res.send css