-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsmileplug.js
More file actions
290 lines (240 loc) · 10 KB
/
smileplug.js
File metadata and controls
290 lines (240 loc) · 10 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
#
#Copyright (c) 2011-2013 Razortooth Communications, LLC. All rights reserved.
#
#Redistribution and use in source and binary forms, with or without modification,
#are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of Razortooth Communications, LLC, nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
#ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
#ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
/**
* smileplug.js
*
* @class js
* @constructor
*/
var js = require('./lib/js.js');
var routes = require('./routes');
var url = require('url');
var starttime = (new Date()).getTime();
js.CONFIG = {
'PORT' : process.env.PORT || 80,
'HOST' : '0.0.0.0',
'VERSION_TAG' : '0.3.2',
'VERSION_DESCRIPTION' : 'SMILE Server',
};
//
// Routes
//
js.put('/smile/currentmessage', routes.handleCurrentMessagePut);
js.post('/smile/currentmessage', routes.handleCurrentMessagePut);
js.get('/smile/currentmessage', routes.handleCurrentMessageGet);
js.put('/smile/startmakequestion', routes.handleStartMakeQuestionPut);
js.post('/smile/startmakequestion', routes.handleStartMakeQuestionPut);
js.put('/smile/startsolvequestion', routes.handleStartSolveQuestionPut);
js.post('/smile/startsolvequestion', routes.handleStartSolveQuestionPut);
js.put('/smile/sendinitmessage', routes.handleSendInitMessagePut);
js.post('/smile/sendinitmessage', routes.handleSendInitMessagePut);
js.put('/smile/sendshowresults', routes.handleSendShowResultsPut);
js.post('/smile/sendshowresults', routes.handleSendShowResultsPut);
js.put('/smile/metadata/rating', routes.handleRatingMetadataPut);
js.post('/smile/metadata/rating', routes.handleRatingMetadataPut);
js.get('/smile/metadata/rating', routes.handleRatingMetadataGet);
js.put('/smile/question', routes.handlePushMessage);
js.post('/smile/question', routes.handlePushMessage);
/**
Get the questions from existing from existin session
@method /smile/question
**/
js.get('/smile/question', routes.handleQuestionGetAll);
/**
Post/Put the csv questions into existing session
@method /smile/question/csv
**/
js.put('/smile/question/csv', routes.handleCsvPushQuestions);
js.post('/smile/question/csv', routes.handleCsvPushQuestions);
/**
Post the IQSet update to an existing :contentid. If :contentid
does not exist, create a new IQSet and return a new :contentid
If no params are present, send JSON data.
If q param is present, send JSON data for specific question to change
If img param is present, send the image content in the message body, or if no
message body, then parse the img param value for a valid URL to pull content XXX TODO
Presumably this can be JSON data, but let's just handle the CSV situation for now
@method /smile/iqset/:contentid
@param q (optional) the question number. Default is body will be
@param image (optional)
**/
// js.post('/smile/iqset/:id', routes.handlePostIQSet, true);
/**
Get the IQSet based on an existing :id. If :id does not exist
return { 'error': <Reason> }
**/
js.get('/smile/iqset/:id', routes.handleGetIQSet, true);
/**
Post the IQSet as a CSV file, creating a new IQSet. Duplicate posts will create
new IDs.
Message body contains a CSV file
XXX TODO: add params to handle type=csv or json
Presumably this can be JSON data, but let's just handle the CSV situation for now
@method /smile/iqset
**/
js.post('/smile/iqset', routes.handlePostNewIQSet);
/**
Get all the inquiry sets
Returns all the IQSets
@method /smile/iqset
**/
js.get('/smile/iqsets', routes.handleGetAllIQSets);
js.put('/smile/question/:id', routes.handlePushMessage, true);
js.post('/smile/question/:id', routes.handlePushMessage, true);
js.get('/smile/question/:id', routes.handleQuestionGet, true);
js.get('/smile/student', routes.handleStudentGetAll);
js.put('/smile/student', routes.handleStudentPut);
js.post('/smile/student', routes.handleStudentPut);
js.get('/smile/student/:id/status', routes.handleStudentStatusGet, true);
// Use this to get a decent results calculation
js.get('/smile/student/:id/result', routes.handleStudentResultsGet, true);
/**
Get session results metadata
@method /smile/results
**/
js.get('/smile/results', routes.handleResultsGet);
/**
Get all session data for current session in a nice json object
@method /smile/session/all
**/
js.get('/smile/session/current', routes.handleCurrentSessionDataGet );
/**
Get all sessions (archived)
@method /smile/sessions
**/
js.get('/smile/sessions', routes.handleAllSessionsGet );
/**
Get all sessions (archived)
@method /smile/session/:id
**/
js.get('/smile/session/:id', routes.handleSessionsGet, true);
/**
Get session messages in an array
@method /smile/all
**/
js.get('/smile/all', routes.handleAllMessagesGet);
/**
XXX TODO: Add debug logging of the session state
@method /smile/debugdump
**/
js.get('/smile/debugdump', routes.handleAllMessagesGet);
/**
XXX TODO: Potentially useless routes ... should they return more than ok?
@method /smile/
**/
js.get('/smile', routes.handleSmileRootGet);
js.get('/smile/', routes.handleSmileRootGet);
js.get('/smile/reset', routes.handleResetGet);
js.put('/smile/reset', routes.handleResetPut);
/**
Store session data
XXX TODO Do we really want to expose this??? Shouldn't it be triggered by internal state?
@method /smile/store
**/
js.put('/smile/store', routes.handleStore);
js.post('/smile/store', routes.handleStore);
/**
Handle complete DB in couchdb export compatible
XXX TODO I think we'll need to write a handler reload a PouchDB database from a doc dump. Is it possible?
What's the mechanism in couchdb to do the same?
@method /smile/backup
**/
js.put('/smile/backup', routes.handleBackup);
js.post('/smile/backup', routes.handleBackup);
js.get('/smile/backup', routes.handleBackup);
js.put('/smile/upload/image', routes.handleImageUpload);
js.post('/smile/upload/image', routes.handleImageUpload);
js.get('/smile/view/sessionstats', routes.handleSessionStats, true); // XXX No regex?
js.get('/smile/view/monitoring.html', routes.handleMonitoringHtmlGet, true); // XXX No regex?
js.get('/smile/questionview/:id_result.html', routes.handleQuestionResultHtmlGet, true);
js.get('/smile/questionview/:id.html', routes.handleQuestionHtmlGet, true);
js.get('/smile/questionview/:id.json', routes.handleQuestionJSONGet, true);
js.get('/smile/questionview/:id.jpg', routes.handleQuestionImageGet, true);
// Compatibility with newer SMILE Student MULTILANG
js.post('/smile/pushmsg.php', routes.handlePushMsgPost);
js.get('/smile/current/MSG/smsg.txt', routes.handleCurrentMessageGet);
// This gets the student status by IP, including their answers
js.get('/smile/current/MSG/:id.txt', routes.handleStudentStatusGetByIP, true);
js.get('/smile/current/:id_result.html', routes.handleQuestionResultHtmlGet, true);
js.get('/smile/current/:id.html', routes.handleQuestionHtmlGet, true);
js.get('/smile/current/:id.jpg', routes.handleQuestionImageGet, true);
var restart = function(req, res) {
app.close();
app.runServer(js.CONFIG.PORT);
res.sendText(HTTP_STATUS_OK, OK);
};
js.get('/smile/restart', restart);
// Backward compatibility with JunctionQuiz
js.get('/JunctionServerExecution/current/MSG/smsg.txt', routes.handleCurrentMessageGet);
js.post('/JunctionServerExecution/pushmsg.php', routes.handlePushMsgPost);
js.get('/JunctionServerExecution/current/MSG/:id.txt', routes.handleStudentStatusGetByIP, true);
js.get('/JunctionServerExecution/current/:id_result.html', routes.handleQuestionResultHtmlGet, true);
js.get('/JunctionServerExecution/current/:id.html', routes.handleQuestionHtmlGet, true);
js.get('/JunctionServerExecution/current/:id.jpg', routes.handleQuestionImageGet, true);
// General utility routes
js.get('/smile/echoclientip', routes.handleEchoClientIP);
// Temporary hack for managing /SMILE and uppercase URLs. See https://github.com/SMILEConsortium/node-smile-server/issues/13
var routeMap = {
'PUT' : {},
'GET' : {},
'POST' : {}
};
Object.keys(js.ROUTE_MAP).forEach(function(method) {
var methodMap = js.ROUTE_MAP[method];
Object.keys(methodMap).forEach(function(k) {
var value = methodMap[k];
routeMap[method][k] = value;
routeMap[method][k.toUpperCase()] = value;
routeMap[method][k.replace('smile', 'SMILE')] = value;
});
});
var reMap = {
'PUT' : {},
'GET' : {},
'POST' : {}
};
Object.keys(js.RE_MAP).forEach(function(method) {
var methodMap = js.RE_MAP[method];
Object.keys(methodMap).forEach(function(k) {
var value = methodMap[k];
reMap[method][k] = value;
reMap[method][k.toUpperCase()] = value;
reMap[method][k.replace('smile', 'SMILE')] = value;
});
});
js.ROUTE_MAP = routeMap;
js.RE_MAP = reMap;
// Hack end
//console.info(js.ROUTE_MAP);
//console.info(js.RE_MAP);
var app = module.exports = js.server;
if (require.main === module) {
app.runServer(js.CONFIG.PORT);
}