-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
125 lines (100 loc) · 3.8 KB
/
app.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var debug = require('debug')('paysage:app');
var World = require('./World');
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var exphbs = require('express-handlebars');
var lessMiddleware = require('less-middleware');
module.exports = function (maybeWorld) {
var app = express();
var world = maybeWorld || new World();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('hbs', exphbs({
extname: '.hbs',
partialsDir: path.join(__dirname, 'views/partials'),
helpers: {
socketioClient: function () {
if (process.env.NODE_ENV === 'production') {
return 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/' + require('socket.io/package').version + '/socket.io.js';
} else {
return '/socket.io/socket.io.js';
}
}
}
}));
app.set('view engine', 'hbs');
if (!process.env.TESTING) app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(lessMiddleware(path.join(__dirname, 'source'), {
dest: path.join(__dirname, 'public')
}));
app.use(express.static(path.join(__dirname, 'public')));
app.locals.reference_url = process.env.REFERENCE_URL || 'http://processing.org/reference/';
// routes setup
app.use('/', require('./routes/create'));
app.use('/playground/', require('./routes/playground'));
app.use('/list', require('./routes/list')(world));
app.use('/workshop', require('./routes/workshop'));
app.use('/:playground', require('./routes/playground-shortcut'));
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function (socket) {
var query = socket.handshake.query;
var playground = world.getOrCreatePlayground(query.playgroundId);
var client = query.client;
if (client === 'renderer') {
rendererUp();
} else {
programmerUp();
}
function sendListOfAllObjects () {
socket.emit('objects list', { data: playground.getData() });
}
function programmerUp () {
debug('a new programmer is up for ' + playground.id);
sendListOfAllObjects();
}
function rendererUp () {
debug('a new renderer is up for ' + playground.id);
socket.emit('playground full update', playground.getData());
}
socket.on('code update', function (data) {
debug(data.codeObjectId + ' for ' + playground.id + ' from ' + client);
var codeObject = playground.getOrCreateCodeObject(data.codeObjectId);
codeObject.setData(data);
});
socket.on('code delete', function (data) {
debug('deleting ' + data.codeObjectId + ' from playground ' + playground.id);
if (playground.contains(data.codeObjectId)) {
playground.deleteCodeObject(data.codeObjectId);
}
});
socket.on('request code', function (data) {
debug(data.codeObjectId + ' for ' + playground.id + ' programmer');
socket.emit('source code', playground.getDataFor(data.codeObjectId));
});
function codeObjectUpdated (codeObject) {
socket.emit('code update', codeObject.getData());
sendListOfAllObjects();
}
playground.on('codeObjectUpdated', codeObjectUpdated);
function codeObjectDeleted (codeObject) {
socket.emit('code delete', {
codeObjectId: codeObject.id
});
sendListOfAllObjects();
}
playground.on('codeObjectDeleted', codeObjectDeleted);
socket.on('disconnect', function () {
playground.removeListener('codeObjectUpdated', codeObjectUpdated);
playground.removeListener('codeObjectDeleted', codeObjectDeleted);
world.releasePlayground(playground);
});
});
return server;
};