[TIC-Web] Bug fixed. the problem that paging
[archive/20170607/tools/tic.git] / app.js
diff --git a/app.js b/app.js
index 085469b..5be94ea 100644 (file)
--- a/app.js
+++ b/app.js
-/* middleware */
-var http = require('http');
-var path = require('path');
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 
-var socketio = require('socket.io');
-var fs = require('fs');
 var express = require('express');
+var session = require('express-session');
+var path = require('path');
 var bodyParser = require('body-parser');
-var app = express();
-var server = http.createServer(app);
-var io = socketio.listen(server);
+var JL = require('jsnlog').JL;
+var jsnlog_nodejs = require('jsnlog-nodejs').jsnlog_nodejs;
+var logger = JL('app.js');
+var AppConfig = require('./config.json');
 
-/* app lib */
-var Config = require('./config.json');
-var FileSystem = require('./server/fs/filesystem');
-var Mic = require('./server/fs/mic');
-var Router = require('./server/routes/router');
 
+var app = express();
 /* app config */
-app.set('port', process.env.PORT || Config.TIC_WEB.PORT);
+app.set('port', process.env.PORT || AppConfig.TIC_WEB.PORT);
 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({extended: true}));
 app.use(express.static(path.join(__dirname, '/public/src'))); //module directory
-app.use('/api', Router);
-
-server.listen(app.get('port'), process.env.IP || "0.0.0.0", function () {
-    var addr = server.address();
-    console.log('Server listening at', addr.address + ':' + addr.port);
-
-    FileSystem.init();
-});
-
 
+var server = require('./controller/server').start(app);
+var io = require('./controller/socketio').listen(server);
+var router = require('./controller/router').init(server);
 
-/**
- * Get package data from tic-core via RESTful API
- * @URI /analysis
- * @TYPE POST
- */
-app.post('/analysis', function(req, res) {
-    var postData = JSON.stringify(req.body);
-    var addr = server.address();
+app.use(session({
+    secret: 'tic',
+    resave: false,
+    saveUninitialized: true
+}));
 
-    var options = {
-        host: addr.address,
-        port: Config.TIC_CORE.PORT || addr.port + 1,
-        method: 'POST',
-        path: '/analysis',
-        headers: {
-            'Content-Type': 'application/json',
-            'Content-Length': Buffer.byteLength(postData)
-        }
-    };
-
-    var data = '';
-    var ticCoreReq = http.request(options, function (ticCoreRes) {
-        ticCoreRes.setEncoding('utf8');
-        ticCoreRes.on('data', function (chunk) {
-            data += chunk;
-        });
-        ticCoreRes.on('end', function () {
-            res.send(data);
-        });
-    });
-
-    ticCoreReq.write(postData);
-    ticCoreReq.end();
-});
+app.use('/api', router);
 
 /**
- * Get ks file path from tic-core via RESTful API
- * @URI /exports
+ * jsnlog.js on the client by default sends log messages to /jsnlog.logger, using POST.
+ * @URI /*.logger
  * @TYPE POST
  */
-app.post('/exports', function(req, res) {
-    var postData = JSON.stringify(req.body);
-    var addr = server.address();
-
-    var options = {
-        host: addr.address,
-        port: Config.TIC_CORE.PORT || addr.port + 1,
-        method: 'POST',
-        path: '/exports',
-        headers: {
-            'Content-Type': 'application/json',
-            'Content-Length': Buffer.byteLength(postData)
-        }
-    };
-
-    var data = '';
-    var ticCoreReq = http.request(options, function (ticCoreRes) {
-        ticCoreRes.setEncoding('utf8');
-        ticCoreRes.on('data', function (chunk) {
-            data += chunk;
-        });
-        ticCoreRes.on('end', function () {
-            res.send(data);
-        });
-    });
-
-    ticCoreReq.write(postData);
-    ticCoreReq.end();
-});
-
-
-/**
- * FileSystem controller (read/create)
- * Connection with MIC
- */
-io.on('connection', function (socket) {
-    console.log('socket connection');
-
-    socket.on('ws/fs/image/list/from', function (data) {
-        var msgData, fileList, targetDirectory;
-
-        msgData = {};
-        fileList = [];
-        targetDirectory = data.path;
-
-        // get the list of file
-        fileList = FileSystem.list(targetDirectory);
-
-        // set the list
-        msgData.list = fileList;
-
-        // send
-        socket.emit('ws/fs/image/list/to', msgData);
-    });
-
-    socket.on('ws/fs/image/add/from', function (data) {
-        console.log(data);
-        var msgData, ans;
-
-        msgData = {};
-
-        function sendMsg(msg) {
-            // send
-            socket.emit('ws/fs/image/add/to', msg);
-        }
-
-        // get the list of file
-        Mic.create(data, socket);
-    });
-
-    socket.on('ws/fs/image/download/from', function (data) {
-        console.log(data);
-
-    });
-
-    socket.on('disconnect', function () {
-        console.log('socket disconnect');
-    });
-
-    socket.on('message', function (msg) {
-        console.log('socket message');
-    });
+app.post('*.logger', function (req, res) {
+    // Pass the log messages to the server side jsnlog.js
+    jsnlog_nodejs(JL, req.body);
 
+    // Send empty response. This is ok, because client side jsnlog does not use response from server.
+    res.send('');
 });