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