-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweb.js
485 lines (419 loc) · 14.5 KB
/
web.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
Error.stackTraceLimit = Infinity;
var express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
http = require('http'),
https = require('https'),
session = require('express-session'),
errorHandler = require('express-error-handler'),
methodOverride = require('method-override'),
//passport = require('passport'),
path = require('path'),
fs = require('fs'),
mongoStore = require('connect-mongo')(session),
params = require('express-params'),
cors = require('cors'),
getJSON = require('./server/getJSON'),
config = require('./server/config/config'),
_ = require('lodash');
var app = express();
// Connect to database
var db = require('./server/db/mongo').db;
// Bootstrap models - this approach lets you avoid adding each model explicitly
var modelsPath = path.join(__dirname, '/server/models');
fs.readdirSync(modelsPath).forEach(function (file) {
require(modelsPath + '/' + file);
});
// App Configuration
var env = process.env.NODE_ENV || 'development';
//var env = 'production';
// express-redis-cache middleware
// configure the cache based upon where we are
if ('development' === env) {
// configure stuff here
app.use(express.static(path.join(__dirname, '.tmp')));
app.use(express.static(path.join(__dirname, 'app')));
app.use(errorHandler());
app.set('views', __dirname + '/app/views');
// setup the local redis cache
var cache = require('express-redis-cache')();
}
if ('production' === env) {
// app.use(express.favicon(path.join(__dirname, 'dist', 'favicon.ico')));
app.use(express.static(path.join(__dirname, 'dist')));
app.set('views', __dirname + '/dist/views');
// TODO: if we are on heroku
// setup the REDISTOGO connection on heroku - from https://devcenter.heroku.com/articles/nodejs-support
//var rtg = require("url").parse(process.env.REDISTOGO_URL);
//var redis = require("redis").createClient(rtg.port, rtg.hostname);
//redis.auth(rtg.auth.split(":")[1]);
//var cache = require('express-redis-cache')({
// client: redis
//});
//console.log('NODE_ENV: ' + process.env.NODE_ENV);
// TODO: if we are not on heroku
// setup the local redis cache
var cache = require('express-redis-cache')();
}
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(cors());
// bodyParser should be above methodOverride
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(methodOverride());
// cookieParser should be above session
app.use(cookieParser());
// express/mongo/passport session storage
app.use(session({
secret: config.secret,
store: new mongoStore({
url: config.db,
collection: 'sessions'
}),
resave: true,
saveUninitialized: true
}));
// configure passport serializeUser and deserializeUser
// note that this method of setting up passport returns a _modified_ and _configured_ version of passport
var passport = require('./server/config/passport');
// use passport session
app.use(passport.initialize());
app.use(passport.session());
//routes should be at the last
//app.use(app.router);
params.extend(app);
// TODO: move this to routes, call the TM inside the TM middleware
app.use('/users/:userId/tm', function(req, res, next) {
// invoked for any request starting with /users/:userId/tm
console.log('Inside web.js - test TM middleware')
next();
});
app.use('/tm', function(req, res, next) {
// invoked for any request starting with /users/:userId/tm
console.log('Inside web.js - test TM middleware')
next();
});
// TODO: move these routes to a separate file and bootstrap
// CONCORDANCER -- using wikipedia (via wikimedia API)
// add a route to query media wiki
app.param('lang', /^\w{2}$/);
app.get('/wikipedia/:lang', function(req, res){
// the search parameter name is 'srsearch'
var lang = req.params.lang.toString().trim();
// Question: put in quotes to search literally?
var searchQuery = encodeURIComponent(req.query.srsearch);
var query = '&srsearch=' + searchQuery;
// removes the region from the language (if any)
// en-US -> en, es-ES -> es
var lang = lang.split('-')[0];
var lang_host = lang + '.wikipedia.org';
var options = {
host: lang_host,
path: '/w/api.php?action=query&format=json&list=search&srprop=snippet' + query,
method: 'GET'
};
getJSON.getJSON(options,
function(err, result) {
if (err) {
res.status(404)
res.send();
}
var searchResults = result.query.search;
res.json(searchResults);
},
res
);
});
// TODO: WORKING - the glossary route should be an interface to all of the user's glossaries
// add a route to query glosbe as a glossary
// glosbe says that you can get around limits by using jsonp
// routes which implement the glossary API should be specified in the config
// TODO: glosbe returns html on error -- handle that case
var q = require('q');
var queryGlosbe = function (fromLang, toLang, queryString, res) {
// lowercase query
queryString = queryString.toLowerCase();
console.log('query glosbe with (lowercased): ' + queryString);
var deferred = q.defer();
// TODO: add a full language code mapping/conversion utility that covers most lang-code conventions
// TODO: this mapping will need to be done for every utility that does not conform to BCP 47: http://tools.ietf.org/html/bcp47#appendix-A
// TODO: let each field be a regex to make the mapping shorter and more flexible
var langCodeMapping = {
'en-US': 'eng',
'en-us': 'eng',
'de-DE': 'deu',
'es-ES': 'spa',
'es': 'spa',
'de': 'deu'
};
if (langCodeMapping[toLang]) toLang = langCodeMapping[toLang];
if (langCodeMapping[fromLang]) fromLang = langCodeMapping[fromLang];
// note the & is missing from the 'from' param
var from = 'from=' + fromLang;
var to = '&dest=' + toLang;
var phrase = '&phrase=' + encodeURIComponent(queryString);
var format = '&format=json'
var options = {
host : 'glosbe.com',
path : '/gapi/translate?' + from + to + phrase + format,
method: 'GET',
// set protocol to https - glosbe requires this
port : 443
// EXAMPLE:
// https://glosbe.com/gapi/tm?from=eng&dest=deu&format=json&phrase="the company grew"&pretty=true
};
// TODO: use the requests library for this
getJSON.getJSON(options,
function (err, result) {
if (err) {
deferred.reject(err);
return;
}
var matches = result.tuc.map(
function (glossaryObj) {
// parse the results here
var p = glossaryObj.phrase;
return p;
}
)
.filter(function (match) {
if (match !== undefined) {
return true;
}
});
// make sure there aren't any duplicates
var unique = {};
matches.forEach(function (match) {
unique[match.text] = 1;
})
matches = matches.filter(function (i) {
return unique[i.text];
})
deferred.resolve(matches);
},
res
);
return deferred.promise;
}
// take the request, and get the params to pass to the glossary function
var askGlossary = function(req,res) {
var fromLang = req.query.sourceLang;
var toLang = req.query.targetLang;
var queryString = req.params.word.toString().trim();
var glossaryPromise = queryGlosbe(fromLang, toLang, queryString, res);
glossaryPromise.then(
function(matches) {
console.log('from glosbe:');
console.log(matches);
res.json(matches);
}
);
}
var natural = require('natural');
var tokenizer = new natural.WordTokenizer();
var glossaryWordList = function(req,res) {
var sourceLang = 'en-US';
var targetLang = 'de-DE';
// tokenize the sentence, and query the glossary for everything that's not punctuation
var phrase = req.params.phrase.toString().trim()
var tokens = tokenizer.tokenize(phrase);
console.log(tokens);
// call queryGlosbe for every token
// wait till every promise resolves
var allQueries = tokens.map(function(token) {
return queryGlosbe(sourceLang, targetLang, token, res);
});
// TODO: this isn't handling errors correctly
q.all(allQueries).then(
function(allResults) {
res.json(_.flatten(allResults));
},
function(err) {
res.status(404);
res.json('error');
}
);
}
// TODO: glossary is disabled for now until errors are properly handled
//app.get('/glossary/segment/:phrase', cache.route(), glossaryWordList);
//app.get('/glossary/word/:word', cache.route(), askGlossary);
// This is for the entity linker demo
// TODO: move this to a plugin
//var DbEntities = require('./server/db/queryEntities');
var DbEntities = require('./server/db/queryEntities');
// MONGO DB ENTITY STORE ROUTES
app.get('/surface-forms/:lang/:entity', function(req, res){
DbEntities.findSurfaceFormByEntityName(req, res);
// res.setHeader('Content-Type', 'application/json');
// res.send(searchResults);
});
// Note that this route must be first, since /logger/:sessionId also matches
var ActionLogger = require('./server/logger/actionLogger');
app.post('/logger', function(req, res){
console.log('posting to /logger');
ActionLogger.logAction(req, res);
});
// working -- how to manage which users can write to which logs? - see express passport & openID
//var ActionLogger = require('./server/logger/actionLogger');
//app.post('/logger/:sessionId', function(req, res){
// var sessionId = req.param('sessionId');
// console.log('posting to /logger/:sessionId with id ' + sessionId);
// console.log(sessionId);
// ActionLogger.addEntryToSession(sessionId, req, res);
// res.setHeader('Content-Type', 'application/json');
// res.send({ "logged": true });
//});
// Note that we need to proxy microservice routes
// proxy the Xliff Creator
var xliffCreatorUrl = 'http://localhost:8080/create-xliff/1.2'
app.get('/create-xliff', function(req,res) {
var sourceLang = req.query.sourceLang;
var targetLang = req.query.targetLang;
var sourceText = req.query.sourceText;
var options = {
url: xliffCreatorUrl,
method: 'GET',
qs: {
sourceLang: sourceLang,
targetLang: targetLang,
sourceText: sourceText
}
};
request(options, function (error, response, body) {
if (error){
console.error('Got error from XLIFF creator: ' + error.code);
}
}).pipe(res);
});
var parallelXliffCreatorUrl = 'http://localhost:8080/create-parallel-xliff/1.2'
app.get('/create-parallel-xliff', function(req,res) {
var sourceLang = req.query.sourceLang;
var targetLang = req.query.targetLang;
var sourceText = req.query.sourceText;
var targetText = req.query.targetText;
var options = {
url: parallelXliffCreatorUrl,
method: 'GET',
qs: {
sourceLang: sourceLang,
targetLang: targetLang,
sourceText: sourceText,
targetText: targetText
}
};
request(options, function (error, response, body) {
if (error){
console.error('Got error from XLIFF creator: ' + error.code);
}
}).pipe(res);
});
// lm_autocompleter
var request = require('request');
// baseline -- return empty list
app.get('/lm_autocomplete/default', function(req,res) {
res.json({'ranked_completions': []});
});
app.get('/imt/neural_imt', function(req,res) {
console.log('IMT ENDPOINT');
console.log(req.query);
var lang_code_mapping = {
'en-EN': 'en',
'fr-FR': 'fr',
'de-DE': 'de',
'pt-PT': 'pt',
'pt-BR': 'pt',
'ga-IE': 'ga'
}
// TODO: error when lang_code is not found -- otherwise this can fail silently
nimtUrl = 'http://localhost:5000/nimt';
var options = {
uri: nimtUrl,
method: 'POST',
json: {
"source_lang": lang_code_mapping[req.query.source_lang],
"target_lang": lang_code_mapping[req.query.target_lang],
"source_sentence": req.query.source_segment,
"target_prefix": req.query.target_prefix,
"request_time": req.query.request_time
}
};
request(options, function (error, response, body) {
if (error && error.code === 'ECONNREFUSED'){
console.error('Refused connection to: ' + nimtUrl);
}
}).pipe(res);
});
var url = require('url');
app.get('/lm_autocomplete/constrained', function(req,res) {
var url_parts = url.parse(req.url, true);
var query_hash = url_parts.query;
// TODO: read the microservice locations from config
var newurl = 'http://localhost:8010/lm_autocomplete';
request({url: newurl, qs: query_hash},
function(error, response, body){
if (error && error.code === 'ECONNREFUSED'){
console.error('Refused connection to: ' + newurl);
}
}
).pipe(res);
});
// vocablist
// proxy to the vocab list server/:langn
app.get('/vocablist/:lang', function(req,res) {
var lang = req.params.lang.toString().trim();
console.log('lang: ' + lang);
// TODO: read the microservice locations from config
// TODO: why doesn't localhost work with restify?
var newurl = 'http://0.0.0.0:8082/vocablist/' + lang;
request(newurl,
function(error, response, body){
if (error && error.code === 'ECONNREFUSED'){
console.error('Refused connection to: ' + newurl);
}
}
).pipe(res);
});
// graph tm
var graphTMUrl = 'http://localhost:8899/tm';
app.get('/tm', function(req,res) {
var url_parts = url.parse(req.url, true);
var query_hash = url_parts.query;
// TODO: read the microservice locations from config
var newurl = 'http://localhost:8899/tm';
request({url: newurl, qs: query_hash},
function(error, response, body){
if (error && error.code === 'ECONNREFUSED'){
console.error('Refused connection');
}
}
).pipe(res);
});
app.post('/tm', function(req,res) {
var newurl = 'http://localhost:8899/tm';
console.log('tm req body');
console.log(req.body);
request({url: newurl, json: req.body},
function(err, remoteResponse, remoteBody) {
if (error && error.code === 'ECONNREFUSED'){
console.error('Refused connection');
}
res.end(remoteBody);
});
});
// End microservice proxies
//Bootstrap routes - remember that routes must be added after application middleware
require('./server/config/routes')(app);
//app.listen(process.env.PORT || 5002);
var server = http.createServer(app).listen(process.env.PORT || 5002);
//console.log('Express server listening on port: ' + server.address().port);
// For https -- note that you need a certificate for this to work
//var privateKey = fs.readFileSync('sslcert/server.key', 'utf8');
//var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
//var credentials = {key: privateKey, cert: certificate};
//https.createServer(options, app).listen(443);
//https.createServer(options, app).listen(8443);
// other datasources to try:
// tausdata, wiktionary