Merge "[TIC-Web] fix multi-selection bug" into develop
[archive/20170607/tools/tic.git] / app.js
1 /* middleware */
2 var http = require('http');
3 var path = require('path');
4 var socketio = require('socket.io');
5 var fs = require('fs');
6 var express = require('express');
7 var bodyParser = require('body-parser');
8 var winston = require('winston');
9 var JL = require('jsnlog').JL;
10 var jsnlog_nodejs = require('jsnlog-nodejs').jsnlog_nodejs;
11
12 var logger = JL('app.js');
13 var app = express();
14 var server = http.createServer(app);
15 var io = socketio.listen(server);
16
17 /* app lib */
18 var Config = require('./config.json');
19 var FileSystem = require('./server/fs/filesystem');
20 var Mic = require('./server/fs/mic');
21 var Router = require('./server/routes/router');
22 var SocketEvent = require('./server/fs/socketEvent');
23
24 /* app config */
25 app.set('port', process.env.PORT || Config.TIC_WEB.PORT);
26 app.use(bodyParser.json());
27 app.use(bodyParser.urlencoded({extended: true}));
28 app.use(express.static(path.join(__dirname, '/public/src'))); //module directory
29 app.use('/api', Router);
30
31 server.listen(app.get('port'), process.env.IP || "0.0.0.0", function () {
32     var addr = server.address();
33
34     // init logger(winston and jsnlog)
35     var consoleAppender = new (winston.transports.Console)();
36     var fileAppender = new winston.transports.File({ json: false, filename: Config.TIC_WEB.LOG_FILE });
37     JL().setOptions({ appenders: [ consoleAppender, fileAppender ] });
38
39     logger.info('Server listening at' + addr.address + ':' + addr.port);
40
41     FileSystem.init();
42 });
43 server.on('error', function(err) {
44     if (err.errno === 'EADDRINUSE') {
45         logger.error('The specified port(' + app.get('port') + ') is already in use. Please check that port is available.');
46     } else {
47         logger.error(err.message);
48     }
49 });
50
51
52 /**
53  * jsnlog.js on the client by default sends log messages to /jsnlog.logger, using POST.
54  * @URI /*.logger
55  * @TYPE POST
56  */
57 app.post('*.logger', function (req, res) {
58     // Pass the log messages to the server side jsnlog.js
59     jsnlog_nodejs(JL, req.body);
60
61     // Send empty response. This is ok, because client side jsnlog does not use response from server.
62     res.send('');
63 });
64
65 /**
66  * Get package data from tic-core via RESTful API
67  * @URI /analysis
68  * @TYPE POST
69  */
70 app.post('/analysis', function(req, res) {
71     var postData = JSON.stringify(req.body);
72     var addr = server.address();
73
74     var options = {
75         host: addr.address,
76         port: Config.TIC_CORE.PORT || addr.port + 1,
77         method: 'POST',
78         path: '/analysis',
79         headers: {
80             'Content-Type': 'application/json',
81             'Content-Length': Buffer.byteLength(postData)
82         }
83     };
84
85     var data = '';
86     var ticCoreReq = http.request(options, function (ticCoreRes) {
87         ticCoreRes.setEncoding('utf8');
88         ticCoreRes.on('data', function (chunk) {
89             data += chunk;
90         });
91         ticCoreRes.on('end', function () {
92             res.send(data);
93         });
94     });
95
96     ticCoreReq.write(postData);
97     ticCoreReq.end();
98 });
99
100 /**
101  * Get ks file path from tic-core via RESTful API
102  * @URI /exports
103  * @TYPE POST
104  */
105 app.post('/exports', function(req, res) {
106     var postData = JSON.stringify(req.body);
107     var addr = server.address();
108
109     var options = {
110         host: addr.address,
111         port: Config.TIC_CORE.PORT || addr.port + 1,
112         method: 'POST',
113         path: '/exports',
114         headers: {
115             'Content-Type': 'application/json',
116             'Content-Length': Buffer.byteLength(postData)
117         }
118     };
119
120     var data = '';
121     var ticCoreReq = http.request(options, function (ticCoreRes) {
122         ticCoreRes.setEncoding('utf8');
123         ticCoreRes.on('data', function (chunk) {
124             data += chunk;
125         });
126         ticCoreRes.on('end', function () {
127             res.send(data);
128         });
129     });
130
131     ticCoreReq.write(postData);
132     ticCoreReq.end();
133 });
134
135
136 /**
137  * FileSystem controller (read/create)
138  * Connection with MIC
139  */
140 io.on('connection', function (socket) {
141     logger.info('socket connection');
142
143     /**
144      * SocketEvent.FS_IMAGE_LIST_FROM = 'ws/fs/image/list/from'
145      */
146     socket.on(SocketEvent.FS_IMAGE_LIST_FROM, function (data) {
147         var msgData, fileList, targetDirectory;
148
149         msgData = {};
150         fileList = [];
151         targetDirectory = data.path;
152
153         // get the list of file
154         fileList = FileSystem.list(targetDirectory);
155
156         // set the list
157         msgData.list = fileList;
158
159         // send
160         socket.emit(SocketEvent.FS_IMAGE_LIST_TO, msgData);
161     });
162
163     /**
164      * SocketEvent.FS_IMAGE_ADD_FROM = 'ws/fs/image/add/from'
165      */
166     socket.on(SocketEvent.FS_IMAGE_ADD_FROM, function (data) {
167         Mic.create(data, io);
168
169         io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
170     });
171
172     /**
173      * SocketEvent.FS_IMAGE_ADD_KILL = 'ws/fs/image/add/kill'
174      */
175     socket.on(SocketEvent.FS_IMAGE_ADD_KILL, function () {
176         Mic.kill();
177
178         io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
179     });
180
181     /**
182      * SocketEvent.MIC_AVAILABLE_FROM = 'ws/mic/available/from'
183      */
184     socket.on(SocketEvent.MIC_AVAILABLE_FROM, function () {
185         logger.info('mic available: ' + Mic.isAvailable());
186         io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
187     });
188
189     /**
190      * SocketEvent.APP_CONFIG_FROM = 'ws/app/config/from'
191      */
192     socket.on(SocketEvent.APP_CONFIG_FROM, function () {
193         socket.emit(SocketEvent.APP_CONFIG_TO, Config);
194     });
195
196     socket.on('disconnect', function () {
197         logger.info('socket disconnect');
198     });
199
200 });