Commit 70e7acfeed549e04fdc8699fb1835949fb93e411

Authored by Wesnydy Ribeiro
1 parent 688244b5

Express basic structure

translate-api/app.js
@@ -11,16 +11,13 @@ @@ -11,16 +11,13 @@
11 /** 11 /**
12 * Module dependencies. 12 * Module dependencies.
13 */ 13 */
14 -var express = require('express');  
15 -var bodyParser = require('body-parser');  
16 -var shortid = require('shortid');  
17 -var amqp = require('amqplib/callback_api'); 14 +var express = require('express')
  15 +var bodyParser = require('body-parser')
  16 +
  17 +var translate = require('./routes/translate')
  18 +var video = require('./routes/video');
18 19
19 -/**  
20 - * Express application and router object.  
21 - */  
22 var app = express(); 20 var app = express();
23 -var router = express.Router();  
24 21
25 /** 22 /**
26 * For parsing application/json and application/x-www-form-urlencoded 23 * For parsing application/json and application/x-www-form-urlencoded
@@ -29,96 +26,25 @@ app.use(bodyParser.json()); @@ -29,96 +26,25 @@ app.use(bodyParser.json());
29 app.use(bodyParser.urlencoded({ extended: true })); 26 app.use(bodyParser.urlencoded({ extended: true }));
30 27
31 /** 28 /**
32 - * Function to send text to the queue.  
33 - */  
34 -function sendToQueue(id, text, queue, durability, res) {  
35 - amqp.connect('amqp://localhost', function(err, conn) {  
36 - if(err) {  
37 - res.json({message : err});  
38 - throw err;  
39 - }  
40 - conn.createChannel(function(err, ch) {  
41 - if(err) {  
42 - res.json({message : err});  
43 - throw err;  
44 - }  
45 - ch.assertQueue(queue, {durable : durability});  
46 - ch.sendToQueue(queue, new Buffer(text), {correlationId : id});  
47 - try {  
48 - ch.close();  
49 - }  
50 - catch (alreadyClosed) {  
51 - console.log(alreadyClosed.stackAtStateChange);  
52 - }  
53 - });  
54 - setTimeout(function() { conn.close(); }, 500000);  
55 - });  
56 -}  
57 -  
58 -/**  
59 - * Function to receive gloss from the queue. 29 + * Public directory.
60 */ 30 */
61 -function receiveFromQueue(id, queue, durability, res) {  
62 - amqp.connect('amqp://localhost', function(err, conn) {  
63 - if(err) {  
64 - res.json({message : err});  
65 - throw err;  
66 - }  
67 - conn.createChannel(function(err, ch) {  
68 - if(err){  
69 - res.json({message : err});  
70 - throw err;  
71 - }  
72 - ch.assertQueue(queue, {durable : durability});  
73 - ch.consume(queue, function(msg) {  
74 - if (msg.properties.correlationId === id) {  
75 - ch.ack(msg);  
76 - res.send(msg.content.toString())  
77 - try {  
78 - ch.close();  
79 - }  
80 - catch (alreadyClosed) {  
81 - console.log(alreadyClosed.stackAtStateChange);  
82 - }  
83 - }  
84 - else {  
85 - ch.reject(msg);  
86 - }  
87 - }, {noAck : false});  
88 - });  
89 - setTimeout(function() { conn.close(); }, 500000);  
90 - });  
91 -}  
92 -  
93 -/**  
94 - * Route to process text and return gloss.  
95 - */  
96 -router.post('/', function(req, res) {  
97 - if (!req.body.text)  
98 - return console.log('Text key is missing');  
99 -  
100 - var id = shortid.generate();  
101 - var text = req.body.text;  
102 - sendToQueue(id, text, 'texts', false, res);  
103 - sendToQueue(id, text, 'log', true, res);  
104 - receiveFromQueue(id, 'glosses', false, res);  
105 -}); 31 +app.use('/video', express.static(process.env.VLIBRAS_VIDEO_LIBRAS));
106 32
107 /** 33 /**
108 - * OLD VERSION 34 + * Allow cross origin requests.
109 */ 35 */
110 -router.get('/', function(req, res) {  
111 - var texto = req.param('text').toString('utf8');  
112 - var id = shortid.generate();  
113 - sendToQueue(id, texto, 'texts', false, res);  
114 - sendToQueue(id, texto, 'log', true, res);  
115 - receiveFromQueue(id, 'glosses', false, res); 36 +app.use(function(req, res, next) {
  37 + res.setHeader("Access-Control-Allow-Methods", "POST, GET");
  38 + res.header("Access-Control-Allow-Origin", "http://localhost:80");
  39 + res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  40 + next();
116 }); 41 });
117 42
118 /** 43 /**
119 - * Register routes with prefix '/translate'. 44 + * Register routes.
120 */ 45 */
121 -app.use('/translate', router); 46 +app.use('/translate', translate);
  47 +app.use('/video', video);
122 48
123 /** 49 /**
124 * Error handler. 50 * Error handler.
translate-api/controllers/translate.js 0 → 100644
@@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
  1 +/**
  2 + * Author: Wesnydy Lima Ribeiro
  3 + * Email: wesnydy@lavid.ufpb.br
  4 + */
  5 +
  6 +'use strict';
  7 +
  8 +/**
  9 + * Required libs.
  10 + */
  11 +var shortid = require('shortid')
  12 + , amqp = require('../helpers/amqpManager');
  13 +
  14 +exports.translate = function(req, res) {
  15 +
  16 + var body = {};
  17 + var id = shortid.generate();
  18 +
  19 + if (!req.body.text)
  20 + return console.log('Text key is missing');
  21 +
  22 + body.text = req.body.text;
  23 + body.type = 'gloss';
  24 +
  25 + amqp.sendToQueue(body, id,'texts', false, res);
  26 + amqp.sendToQueue(body, id,'logs', true, res);
  27 + amqp.receiveFromQueue(id, 'glosses', false, res);
  28 +};
  29 +
  30 +exports.urltranslate = function(req, res) {
  31 +
  32 + var body = {};
  33 + var id = shortid.generate();
  34 +
  35 + if (!req.body.text)
  36 + return console.log('Text key missing');
  37 +
  38 + body.text = req.param('text').toString('utf8');
  39 + body.type = 'gloss';
  40 +
  41 + amqp.sendToQueue(body, id, 'texts', false, res);
  42 + amqp.sendToQueue(body, id, 'logs', true, res);
  43 + amqp.receiveFromQueue(id, 'glosses', false, res);
  44 +};
translate-api/controllers/video.js 0 → 100644
@@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
  1 +/**
  2 + * Author: Wesnydy Lima Ribeiro
  3 + * Email: wesnydy@lavid.ufpb.br
  4 + */
  5 +
  6 +'use strict';
  7 +
  8 +/**
  9 + * Required libs.
  10 + */
  11 +var shortid = require('shortid')
  12 + , amqp = require('../helpers/amqpManager');
  13 +
  14 +exports.createVideo = function(req, res) {
  15 +
  16 + var body = {};
  17 + var id = shortid.generate();
  18 +
  19 + if (!req.body.text)
  20 + return console.log('Text key missing');
  21 +
  22 + body.text = req.body.text;
  23 + body.type = 'video';
  24 +
  25 + amqp.sendToQueue(body, id, 'texts', false, res);
  26 + amqp.sendToQueue(body, id, 'logs', true, res);
  27 + amqp.receiveFromQueue(id, 'videos', false, res);
  28 +};
translate-api/helpers/amqpManager.js 0 → 100644
@@ -0,0 +1,75 @@ @@ -0,0 +1,75 @@
  1 +/**
  2 + * Author: Wesnydy Lima Ribeiro
  3 + * Email: wesnydy@lavid.ufpb.br
  4 + */
  5 +
  6 +'use strict';
  7 +
  8 +/**
  9 + * Required libs.
  10 + */
  11 +var amqplib = require('amqplib/callback_api');
  12 +
  13 +/**
  14 + * Function to send text to the queue.
  15 + */
  16 +exports.sendToQueue = function(body, id, queue, durability, res) {
  17 +
  18 + amqplib.connect('amqp://localhost', function(err, conn) {
  19 + if(err) {
  20 + res.json({message : err});
  21 + throw err;
  22 + }
  23 + conn.createChannel(function(err, ch) {
  24 + if(err) {
  25 + res.json({message : err});
  26 + throw err;
  27 + }
  28 + ch.assertQueue(queue, {durable : durability});
  29 + ch.sendToQueue(queue, new Buffer(JSON.stringify(body)), {correlationId : id});
  30 + try {
  31 + ch.close();
  32 + }
  33 + catch (alreadyClosed) {
  34 + console.log(alreadyClosed.stackAtStateChange);
  35 + }
  36 + });
  37 + setTimeout(function() { conn.close(); }, 500000);
  38 + });
  39 +};
  40 +
  41 +/**
  42 + * Function to receive gloss from the queue.
  43 + */
  44 +exports.receiveFromQueue = function(id, queue, durability, res) {
  45 +
  46 + amqplib.connect('amqp://localhost', function(err, conn) {
  47 + if(err) {
  48 + res.json({message : err});
  49 + throw err;
  50 + }
  51 + conn.createChannel(function(err, ch) {
  52 + if(err){
  53 + res.json({message : err});
  54 + throw err;
  55 + }
  56 + ch.assertQueue(queue, {durable : durability});
  57 + ch.consume(queue, function(msg) {
  58 + if (msg.properties.correlationId === id) {
  59 + ch.ack(msg);
  60 + res.send(msg.content.toString())
  61 + try {
  62 + ch.close();
  63 + }
  64 + catch (alreadyClosed) {
  65 + console.log(alreadyClosed.stackAtStateChange);
  66 + }
  67 + }
  68 + else {
  69 + ch.reject(msg);
  70 + }
  71 + }, {noAck : false});
  72 + });
  73 + setTimeout(function() { conn.close(); }, 500000);
  74 + });
  75 +};
translate-api/routes/translate.js 0 → 100644
@@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
  1 +/**
  2 + * Author: Wesnydy Lima Ribeiro
  3 + * Email: wesnydy@lavid.ufpb.br
  4 + */
  5 +
  6 +var express = require('express')
  7 + , router = express.Router()
  8 + , translateController = require('../controllers/translate');
  9 +
  10 +/**
  11 + * Routes to process text and return gloss.
  12 + */
  13 +router
  14 + .get('/', translateController.urltranslate)
  15 + .post('/', translateController.translate)
  16 +
  17 +module.exports = router;
translate-api/routes/video.js 0 → 100644
@@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
  1 +/**
  2 + * Author: Wesnydy Lima Ribeiro
  3 + * Email: wesnydy@lavid.ufpb.br
  4 + */
  5 +
  6 +var express = require('express')
  7 + , router = express.Router()
  8 + , videoController = require('../controllers/video');
  9 +
  10 +/**
  11 + * Route to process text and create video.
  12 + */
  13 +router
  14 + .post('/', videoController.createVideo)
  15 +
  16 +module.exports = router;