[TIC-UI] add config file and modified the RESTful API handled by the server
[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 app = express();
10 var server = http.createServer(app);
11 var io = socketio.listen(server);
12
13 /* app lib */
14 var Config = require('./config.json');
15 var FileSystem = require('./server/fs/filesystem');
16 var Mic = require('./server/fs/mic');
17 var Router = require('./server/routes/router');
18
19 /* app config */
20 app.set('port', process.env.PORT || Config.TIC_WEB.PORT);
21 app.use(bodyParser.json());
22 app.use(bodyParser.urlencoded({extended: true}));
23 app.use(express.static(path.join(__dirname, '/public/src'))); //module directory
24 app.use('/api', Router);
25
26 server.listen(app.get('port'), process.env.IP || "0.0.0.0", function () {
27     var addr = server.address();
28     console.log('Server listening at', addr.address + ':' + addr.port);
29
30     FileSystem.init();
31 });
32
33
34
35 /**
36  * Get package data from tic-core via RESTful API
37  * @URI /analysis
38  * @TYPE POST
39  */
40 app.post('/analysis', function(req, res) {
41     var postData = JSON.stringify(req.body);
42     var addr = server.address();
43
44     var options = {
45         host: addr.address,
46         port: Config.TIC_CORE.PORT || addr.port + 1,
47         method: 'POST',
48         path: '/analysis',
49         headers: {
50             'Content-Type': 'application/json',
51             'Content-Length': Buffer.byteLength(postData)
52         }
53     };
54
55     var data = '';
56     var ticCoreReq = http.request(options, function (ticCoreRes) {
57         ticCoreRes.setEncoding('utf8');
58         ticCoreRes.on('data', function (chunk) {
59             data += chunk;
60         });
61         ticCoreRes.on('end', function () {
62             res.send(data);
63         });
64     });
65
66     ticCoreReq.write(postData);
67     ticCoreReq.end();
68 });
69
70
71
72 /**
73  * FileSystem controller (read/create)
74  * Connection with MIC
75  */
76 io.on('connection', function (socket) {
77     console.log('socket connection');
78
79     socket.on('ws/fs/image/list/from', function (data) {
80         var msgData, fileList, targetDirectory;
81
82         msgData = {};
83         fileList = [];
84         targetDirectory = data.path;
85
86         // get the list of file
87         fileList = FileSystem.list(targetDirectory);
88
89         // set the list
90         msgData.list = fileList;
91
92         // send
93         socket.emit('ws/fs/image/list/to', msgData);
94     });
95
96     socket.on('ws/fs/image/add/from', function (data) {
97         console.log(data);
98         var msgData, ans;
99
100         msgData = {};
101
102         function sendMsg(msg) {
103             // send
104             socket.emit('ws/fs/image/add/to', msg);
105         }
106
107         // get the list of file
108         Mic.create(data, socket);
109     });
110
111     socket.on('ws/fs/image/download/from', function (data) {
112         console.log(data);
113
114     });
115
116     socket.on('disconnect', function () {
117         console.log('socket disconnect');
118     });
119
120     socket.on('message', function (msg) {
121         console.log('socket message');
122     });
123
124 });