[TIC-Web] save the log as a file
[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
44
45 /**
46  * jsnlog.js on the client by default sends log messages to /jsnlog.logger, using POST.
47  * @URI /*.logger
48  * @TYPE POST
49  */
50 app.post('*.logger', function (req, res) {
51     // Pass the log messages to the server side jsnlog.js
52     jsnlog_nodejs(JL, req.body);
53
54     // Send empty response. This is ok, because client side jsnlog does not use response from server.
55     res.send('');
56 });
57
58 /**
59  * Get package data from tic-core via RESTful API
60  * @URI /analysis
61  * @TYPE POST
62  */
63 app.post('/analysis', function(req, res) {
64     var postData = JSON.stringify(req.body);
65     var addr = server.address();
66
67     var options = {
68         host: addr.address,
69         port: Config.TIC_CORE.PORT || addr.port + 1,
70         method: 'POST',
71         path: '/analysis',
72         headers: {
73             'Content-Type': 'application/json',
74             'Content-Length': Buffer.byteLength(postData)
75         }
76     };
77
78     var data = '';
79     var ticCoreReq = http.request(options, function (ticCoreRes) {
80         ticCoreRes.setEncoding('utf8');
81         ticCoreRes.on('data', function (chunk) {
82             data += chunk;
83         });
84         ticCoreRes.on('end', function () {
85             res.send(data);
86         });
87     });
88
89     ticCoreReq.write(postData);
90     ticCoreReq.end();
91 });
92
93 /**
94  * Get ks file path from tic-core via RESTful API
95  * @URI /exports
96  * @TYPE POST
97  */
98 app.post('/exports', function(req, res) {
99     var postData = JSON.stringify(req.body);
100     var addr = server.address();
101
102     var options = {
103         host: addr.address,
104         port: Config.TIC_CORE.PORT || addr.port + 1,
105         method: 'POST',
106         path: '/exports',
107         headers: {
108             'Content-Type': 'application/json',
109             'Content-Length': Buffer.byteLength(postData)
110         }
111     };
112
113     var data = '';
114     var ticCoreReq = http.request(options, function (ticCoreRes) {
115         ticCoreRes.setEncoding('utf8');
116         ticCoreRes.on('data', function (chunk) {
117             data += chunk;
118         });
119         ticCoreRes.on('end', function () {
120             res.send(data);
121         });
122     });
123
124     ticCoreReq.write(postData);
125     ticCoreReq.end();
126 });
127
128
129 /**
130  * FileSystem controller (read/create)
131  * Connection with MIC
132  */
133 io.on('connection', function (socket) {
134     logger.info('socket connection');
135
136     /**
137      * SocketEvent.FS_IMAGE_LIST_FROM = 'ws/fs/image/list/from'
138      */
139     socket.on(SocketEvent.FS_IMAGE_LIST_FROM, function (data) {
140         var msgData, fileList, targetDirectory;
141
142         msgData = {};
143         fileList = [];
144         targetDirectory = data.path;
145
146         // get the list of file
147         fileList = FileSystem.list(targetDirectory);
148
149         // set the list
150         msgData.list = fileList;
151
152         // send
153         socket.emit(SocketEvent.FS_IMAGE_LIST_TO, msgData);
154     });
155
156     /**
157      * SocketEvent.FS_IMAGE_ADD_FROM = 'ws/fs/image/add/from'
158      */
159     socket.on(SocketEvent.FS_IMAGE_ADD_FROM, function (data) {
160         Mic.create(data, io);
161
162         io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
163     });
164
165     /**
166      * SocketEvent.FS_IMAGE_ADD_KILL = 'ws/fs/image/add/kill'
167      */
168     socket.on(SocketEvent.FS_IMAGE_ADD_KILL, function () {
169         Mic.kill();
170
171         io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
172     });
173
174     /**
175      * SocketEvent.MIC_AVAILABLE_FROM = 'ws/mic/available/from'
176      */
177     socket.on(SocketEvent.MIC_AVAILABLE_FROM, function () {
178         logger.info('mic available: ' + Mic.isAvailable());
179         io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
180     });
181
182     /**
183      * SocketEvent.APP_CONFIG_FROM = 'ws/app/config/from'
184      */
185     socket.on(SocketEvent.APP_CONFIG_FROM, function () {
186         socket.emit(SocketEvent.APP_CONFIG_TO, Config);
187     });
188
189     socket.on('disconnect', function () {
190         logger.info('socket disconnect');
191     });
192
193 });