translate.js
1.6 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
/**
* Author: Wesnydy Lima Ribeiro
* Email: wesnydy@lavid.ufpb.br
*/
'use strict';
/**
* Required libs.
*/
var shortid = require('shortid')
, amqp = require('../helpers/amqpManager')
, error = require('../helpers/error')
, settings = require('../config/settings');
exports.translate = function(req, res, next) {
if (!req.body.text)
return error.badRequest('The text key is missing.', next);
var params = {};
params.id = shortid.generate();
params.text = req.body.text;
params.lang = req.body.lang ? req.body.lang : settings.defaultLang;
amqp.sendToQueue(JSON.stringify(params), id,'texts', false, res, function(err) {
if (err)
return error.internalError('An internal communication error has occurred.', next);
amqp.receiveFromQueue(id, 'translations', false, res, function(err, message) {
if (err)
return error.internalError('An internal communication error has occurred.', next);
res.status(200).send(message);
});
});
};
// TEMPORARY
exports.translateURL = function(req, res, next) {
if (!req.param('text'))
return error.badRequest('The text param is missing.', next);
var id = shortid.generate();
var text = req.param('text').toString('utf8');
amqp.sendToQueue(text, id,'texts', false, res, function(err) {
if (err)
return error.internalError('An internal communication error has occurred.', next);
amqp.receiveFromQueue(id, 'translations', false, res, function(err, message) {
if (err)
return error.internalError('An internal communication error has occurred.', next);
res.status(200).send(message);
});
});
};