[TIC-Web] Bug fixed. the problem that paging
[archive/20170607/tools/tic.git] / app.js
diff --git a/app.js b/app.js
index bebb4bf..5be94ea 100644 (file)
--- a/app.js
+++ b/app.js
@@ -1,53 +1,47 @@
-/* middleware */
-var http = require('http');
-var path = require('path');
-var socketio = require('socket.io');
-var fs = require('fs');
+/*
+ * 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 express = require('express');
+var session = require('express-session');
+var path = require('path');
 var bodyParser = require('body-parser');
-var winston = require('winston');
 var JL = require('jsnlog').JL;
 var jsnlog_nodejs = require('jsnlog-nodejs').jsnlog_nodejs;
-
 var logger = JL('app.js');
-var app = express();
-var server = http.createServer(app);
-var io = socketio.listen(server);
+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 SocketEvent = require('./server/fs/socketEvent');
 
+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();
-
-    // init logger(winston and jsnlog)
-    var consoleAppender = new (winston.transports.Console)();
-    var fileAppender = new winston.transports.File({ json: false, filename: Config.TIC_WEB.LOG_FILE });
-    JL().setOptions({ appenders: [ consoleAppender, fileAppender ] });
 
-    logger.info('Server listening at' + addr.address + ':' + addr.port);
+var server = require('./controller/server').start(app);
+var io = require('./controller/socketio').listen(server);
+var router = require('./controller/router').init(server);
 
-    FileSystem.init();
-});
-server.on('error', function(err) {
-    if (err.errno === 'EADDRINUSE') {
-        logger.error('The specified port(' + app.get('port') + ') is already in use. Please check that port is available.');
-    } else {
-        logger.error(err.message);
-    }
-});
+app.use(session({
+    secret: 'tic',
+    resave: false,
+    saveUninitialized: true
+}));
 
+app.use('/api', router);
 
 /**
  * jsnlog.js on the client by default sends log messages to /jsnlog.logger, using POST.
@@ -61,140 +55,3 @@ app.post('*.logger', function (req, res) {
     // Send empty response. This is ok, because client side jsnlog does not use response from server.
     res.send('');
 });
-
-/**
- * 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();
-
-    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();
-});
-
-/**
- * Get ks file path from tic-core via RESTful API
- * @URI /exports
- * @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) {
-    logger.info('socket connection');
-
-    /**
-     * SocketEvent.FS_IMAGE_LIST_FROM = 'ws/fs/image/list/from'
-     */
-    socket.on(SocketEvent.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(SocketEvent.FS_IMAGE_LIST_TO, msgData);
-    });
-
-    /**
-     * SocketEvent.FS_IMAGE_ADD_FROM = 'ws/fs/image/add/from'
-     */
-    socket.on(SocketEvent.FS_IMAGE_ADD_FROM, function (data) {
-        Mic.create(data, io);
-
-        io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
-    });
-
-    /**
-     * SocketEvent.FS_IMAGE_ADD_KILL = 'ws/fs/image/add/kill'
-     */
-    socket.on(SocketEvent.FS_IMAGE_ADD_KILL, function () {
-        Mic.kill();
-
-        io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
-    });
-
-    /**
-     * SocketEvent.MIC_AVAILABLE_FROM = 'ws/mic/available/from'
-     */
-    socket.on(SocketEvent.MIC_AVAILABLE_FROM, function () {
-        logger.info('mic available: ' + Mic.isAvailable());
-        io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
-    });
-
-    /**
-     * SocketEvent.APP_CONFIG_FROM = 'ws/app/config/from'
-     */
-    socket.on(SocketEvent.APP_CONFIG_FROM, function () {
-        socket.emit(SocketEvent.APP_CONFIG_TO, Config);
-    });
-
-    socket.on('disconnect', function () {
-        logger.info('socket disconnect');
-    });
-
-});