-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.coffee
97 lines (74 loc) · 2.45 KB
/
server.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
express = require 'express'
connect = require 'connect'
sys = require 'sys'
fs = require 'fs'
Mustache = require './lib/mustache'
# Data (will live within redis in the future)
# ==============================================================================
visualizations = [
{
name: 'linechart'
title: 'Linechart'
descr: 'An interactive, animated Linechart built with Unveil.js'
},
{
name: 'stacks'
title: 'Stacks'
descr: 'Visualizing groups of items using self-organizing stacks.'
},
{
name: 'scatterplot'
title: 'Scatterplot'
descr: 'An interactive animated Scatterplot visualization that allows zooming and panning.'
},
{
name: 'bullets'
title: 'Bullets'
descr: 'Unveil.js graphics demo..'
}
]
collections = [
{
name: 'artists'
title: 'Music Artists'
uri: 'http://collections.freebaseapps.com/artists'
}
]
# Helpers
# ==============================================================================
render = (filename, view) ->
template = fs.readFileSync(filename, 'utf-8')
Mustache.to_html(template, view)
app = express.createServer()
# Configuration
# ==============================================================================
app.configure ->
app.set 'views', __dirname + '/views'
app.use connect.bodyDecoder()
app.use app.router
app.use connect.staticProvider(__dirname + '/public')
app.configure 'development', ->
app.use connect.errorHandler({ dumpExceptions: true, showStack: true })
app.configure 'production', ->
app.use connect.errorHandler()
# Routes
# ==============================================================================
app.get '/', (req, res) ->
view = {visualizations: visualizations}
res.send render('templates/index.mustache', view)
# Serves collections
app.get '/collections/:collection', (req, res) ->
res.send('TODO: Serve a collection')
# Serves static files from the visualization directory
app.get /^\/files\/(.+)$/, (req, res) ->
res.writeHead(200, {
'Content-Type': if req.params[0].indexOf('css') >= 0 then 'text/css' else 'text/javascript'
})
res.write fs.readFileSync("visualizations/#{req.params[0]}", 'utf-8')
res.end()
app.get '/:vis', (req, res) ->
manifest = JSON.parse(fs.readFileSync("visualizations/#{req.params.vis}/manifest.json", 'utf-8'))
manifest['dir'] = "/files/#{req.params.vis}/"
manifest['hosted'] = true
res.send render('templates/show.mustache', manifest)
app.listen 5001