[TIC-Web] refactoring for rest api 96/130596/5
authorChangHyun Lee <leechwin.lee@samsung.com>
Tue, 23 May 2017 05:22:05 +0000 (14:22 +0900)
committerChangHyun Lee <leechwin.lee@samsung.com>
Tue, 23 May 2017 06:55:47 +0000 (15:55 +0900)
- Modify the definition of RESTful api

Change-Id: I5a3c9d1cee39ad8a796d2a7e7849ef450fb0b692
Signed-off-by: ChangHyun Lee <leechwin.lee@samsung.com>
17 files changed:
app.js
config.json
controller/auth.js
controller/cleanup.js
controller/filesystem.js
controller/jobs.js
controller/router.js
controller/socketio.js
public/src/index.html
public/src/js/page/export.js
public/src/js/page/image.js
public/src/js/page/import.js
public/src/js/page/job.js
public/src/js/page/login.js
public/src/js/page/package.js
public/src/js/page/settings.js
public/src/js/util.js

diff --git a/app.js b/app.js
index 481f06e..5a31c0d 100644 (file)
--- a/app.js
+++ b/app.js
@@ -22,7 +22,6 @@ var JL = require('jsnlog').JL;
 var jsnlog_nodejs = require('jsnlog-nodejs').jsnlog_nodejs;
 var logger = JL('app.js');
 var AppConfig = require('./config.json');
-var dbpool = require('./controller/dbpool');
 var cleanup = require('./controller/cleanup');
 
 var app = express();
@@ -58,9 +57,4 @@ app.post('*.logger', function (req, res) {
     res.send('');
 });
 
-process.stdin.resume();
-
-var allCleanUp = function allCleanUp () {
-    dbpool.clearAll();
-};
-cleanup.cleanup(allCleanUp);
+cleanup.cleanup();
index f3a60a1..5bfb420 100644 (file)
@@ -31,7 +31,7 @@
         "LOG": "mic.log"
     },
     "EVENT": {
-        "SOCKET":{
+        "SOCKET": {
             "FS_IMAGE_ADD_FROM": "ws/fs/image/add/from",
             "FS_IMAGE_ADD_TO": "ws/fs/image/add/to",
             "FS_IMAGE_ADD_FINISH": "ws/fs/image/add/finish",
             "MIC_NEXT_JOB_TO": "ws/mic/nextjob/to",
             "MIC_ADD_PID_JOB_TO": "ws/mic/pid/to"
         },
-        "JOB": {
-            "ADD_JOB": "/api/jobs",
-            "CANCEL_JOB": "/api/jobs/",
-            "GET_JOBS": "/api/jobs",
-            "READ_LOG": "/api/logs/"
-        },
-        "PACKAGE": {
+        "REST": {
+            "CONFIG": "/api/config",
+            "ANALYSIS": "/api/analysis",
             "IMPORT": "/api/imports",
             "EXPORT": "/api/exports",
             "EXPORT_KS": "ks",
             "EXPORT_RECIPE": "recipe",
-            "ANALYSIS": "/api/analysis",
-            "RECIPE": "/api/recipe"
-        },
-        "IMAGE": {
-            "GET_IMAGES": "/api/images",
-            "IMAGE_GET_ALL_COUNT": "/api/image/count/",
-            "IMAGE_GET_ALL_LISTITEM": "/api/image/list/",
-            "IMAGE_ADD_ONE": "/api/image/add/"
-        },
-        "SESSION": {
-            "SESSION": "/api/session"
+            "RECIPE": "/api/recipe",
+            "SESSION": "/api/session",
+            "JOBS": "/api/jobs",
+            "IMAGES": "/api/images",
+            "LOGS": "/api/logs/"
         }
     }
 }
\ No newline at end of file
index 8b62382..ca0bd60 100644 (file)
@@ -25,9 +25,9 @@ var logger = JL('auth.js');
 function Auth() {
     var permissions = [
         // TODO: fix the api name
-        { api: AppConfig.EVENT.JOB.JOB_ADD_ONE, method: 'POST', group: 'MASTER' }, // job create
-        { api: '/api/jobs', method: 'PUT', group: 'MASTER' }, // job cancel
-        { api: '/api/images', method: 'DELETE', group: 'ADMIN' } // image delete
+        { api: AppConfig.EVENT.REST.JOBS, method: 'POST', group: 'MASTER' }, // job create
+        { api: AppConfig.EVENT.REST.JOBS, method: 'PUT', group: 'MASTER' }, // job cancel
+        { api: AppConfig.EVENT.REST.IMAGES, method: 'DELETE', group: 'ADMIN' } // image delete
     ];
 
     function getPermissions() {
index 639fdfc..28e02f6 100644 (file)
 
 var JL = require('jsnlog').JL;
 var logger = JL('cleanup.js');
+var dbpool = require('./dbpool');
 
-function noOperation() {};
+var cleanup = function() {
 
-var cleanup = function cleanup (callback) {
-    callback = callback || noOperation;
+    process.stdin.resume();
 
     /**
      * When app is closing
      */
-    process.on('exit', callback);
+    process.on('exit', function() {
+        dbpool.clearAll();
+    });
 
     /**
      * When app catches ctrl + c event
      */
-    process.on('SIGINT', function () {
+    process.on('SIGINT', function() {
         logger.error('ctrl + c ...');
         process.exit(2);
     });
@@ -40,7 +42,7 @@ var cleanup = function cleanup (callback) {
     /**
      * When app catches uncaught exceptions
      */
-    process.on('uncaughtException', function (e) {
+    process.on('uncaughtException', function(e) {
         logger.error('Uncaught exception ...');
         logger.error(e.stack);
         process.exit(99);
index ea3878b..0cf504d 100644 (file)
@@ -32,10 +32,15 @@ FileSystem.readLogFile = function readLogFile(req, res) {
 
     fs.readFile(strJobLogPath, { encoding: 'utf8' }, function (err, data) {
         if (err) {
-            // log file is not exist
-            res.json({});
+            res.json({
+                result: false,
+                messaeg: 'log file is not exist'
+            });
         } else {
-            res.json(data);
+            res.json({
+                result: true,
+                data: data
+            });
         }
     });
 }
index 1cb49d0..f9b9d58 100644 (file)
@@ -15,6 +15,7 @@
  */
 
 'use strict';
+
 const JOBS_INIT_OFFSET = 0;
 const JOBS_INIT_LIMIT = 10;
 
@@ -56,6 +57,7 @@ Jobs.prototype.addJob = function addJob () {
  * Edit the job
  */
 Jobs.prototype.editJob = function editJob (req, res) {
+    // TODO: need to modify according to rest api definition
     var job_id, job_status;
 
     job_id = req.params.id;
index 334cc4c..c683e68 100644 (file)
@@ -72,7 +72,7 @@ var init = function (serv) {
     });
 
     /**
-     * Get The All Jobs
+     * Create Job
      * @URI /api/jobs
      * @TYPE POST
      */
@@ -91,12 +91,18 @@ var init = function (serv) {
 
         // checks - id
         if (!Number(req.params.id)) {
-            return;
+            res.json({
+                result: false,
+                messaeg: 'The parameter is incorrect'
+            });
         }
 
         // checks - job_status
         if (!req.body.job_status || req.body.job_status !== 'CANCELED') {
-            return;
+            res.json({
+                result: false,
+                messaeg: 'The job status is incorrect'
+            });
         }
 
         // call
@@ -106,13 +112,16 @@ var init = function (serv) {
     /**
      * Read the log file
      * @URI /api/logs/:id
-     * @TYPE POST
+     * @TYPE GET
      */
     router.get('/logs/:id', function (req, res) {
+        logger.info('an api called that /api/logs/');
         if (!Number(req.params.id)) {
-            return;
+            res.json({
+                result: false,
+                messaeg: 'The parameter is incorrect'
+            });
         }
-        logger.info('an api called that /api/logs/' + req.params.id);
         filesystem.readLogFile(req, res);
     });
 
@@ -123,7 +132,7 @@ var init = function (serv) {
     /**
      * Get The All Images
      * @URI /api/image/list
-     * @TYPE POST
+     * @TYPE GET
      */
     router.get('/images', function (req, res) {
         logger.info('an api called that /api/images');
index 8317968..0ee9f5a 100644 (file)
@@ -74,11 +74,6 @@ var listen = function listen (server) {
             });
         });
 
-        socket.on('forceDisconnect', function() {
-            logger.error('socket on forceDisconnect.');
-            socket.disconnect(true);
-        });
-
         socket.on('disconnect', function() {
             logger.info('client disconnected');
         });
index 8996b75..0905663 100644 (file)
                                 </table>
                             </div>
                             <div class="panel-footer">
-                                <a type="button" id="tic-package-create" class="btn btn-primary disabled" href="#">Image Creation</a>
+                                <a type="button" id="tic-package-create" class="btn btn-primary" href="#">Image Creation</a>
                             </div>
                         </div>
                     </div><!-- /End Package Right Column -->
index 7e140de..ef9743e 100644 (file)
@@ -34,8 +34,8 @@ define([
     function showExportDialog() {
         Util.showConfirmDialog('Are you sure you want to export recipe?')
         .then(function () {
-            return Util.POST(CONFIG.EVENT.PACKAGE.EXPORT, {
-                format: CONFIG.EVENT.PACKAGE.EXPORT_RECIPE,
+            return Util.POST(CONFIG.EVENT.REST.EXPORT, {
+                format: CONFIG.EVENT.REST.EXPORT_RECIPE,
                 recipes: require('js/page/settings').getRecipeStore(),
                 packages: _.map(require('js/page/package').getCheckedPackages(), 'name')
             });
index 5ff1663..5b6096b 100644 (file)
@@ -148,7 +148,7 @@ define([
         }
 
         function getImages () {
-            var strUrl = AppConfig.EVENT.IMAGE.GET_IMAGES + '?' + Util.encodeURIDataObj(pagingObj);
+            var strUrl = AppConfig.EVENT.REST.IMAGES + '?' + Util.encodeURIDataObj(pagingObj);
             logger.info('getJobs : ' + strUrl);
             return Util.GET(strUrl);
         }
index b13092b..5615fd9 100644 (file)
@@ -42,7 +42,7 @@ define([
             recipes : recipeStore
         };
 
-        return Util.POST(CONFIG.EVENT.PACKAGE.IMPORT, postBody)
+        return Util.POST(CONFIG.EVENT.REST.IMPORT, postBody)
         .then(function (result) {
             if (result.result === 'true') {
                 return result.data.recipes;
@@ -146,7 +146,7 @@ define([
             $('#tic-import-recipe-file-group-help').append(helpDomContent);
         }
         $('#tic-import-recipe-file-input').dropzone({
-            url: CONFIG.EVENT.PACKAGE.RECIPE,
+            url: CONFIG.EVENT.REST.RECIPE,
             method: 'post',
             maxFilesize: 5, // MB
             maxFiles: 2, // Hack for replacing file
index 80db238..50c2cfd 100644 (file)
@@ -95,15 +95,18 @@ define([
             }
         }
 
-        function updateJobsModel (jobs) {
+        function updateJobsModel (res) {
             // successfully get the all jobs
             return new Promise (function (resolve, reject) {
                 var targetPaging, targetTableBody;
+                if (res.result === false) {
+                    reject(res);
+                }
 
                 ModelJobList = [];
 
                 // jobs model
-                _.forEach(jobs.data, function (jobItem) {
+                _.forEach(res.data, function (jobItem) {
                     // add the information of user's group
                     jobItem['job_usergroup'] = _.isEmpty(UserInfo) ? USER_DEFAULT : UserInfo.group;
 
@@ -122,10 +125,14 @@ define([
                     _.forEach(ModelJobList, function (value, index) {
                         targetTableBody.append(new JobTableItem(value, index).getRow());
                         if (value.getJobStatus() === JOB_STATUS_INPROGRESS) {
-                            Util.GET(AppConfig.EVENT.JOB.READ_LOG + value.getJobId())
-                            .then(function (line) {
-                                targetId = '#extended_job_table_row_' + value.getJobId();
-                                $(targetId).append(line);
+                            Util.GET(AppConfig.EVENT.REST.LOGS + value.getJobId())
+                            .then(function (res) {
+                                if (res.result) {
+                                    targetId = '#extended_job_table_row_' + value.getJobId();
+                                    $(targetId).append(res.data);
+                                } else {
+                                    logger.error(res.message);
+                                }
                             });
                         }
                     });
@@ -169,7 +176,7 @@ define([
                             job_status: JOB_STATUS_CANCELED
                         };
 
-                        Util.PUT(AppConfig.EVENT.JOB.CANCEL_JOB + jobId, msgData)
+                        Util.PUT(AppConfig.EVENT.REST.JOBS + '/' + jobId, msgData)
                         .then(updateList);
                     });
                 }
@@ -177,15 +184,15 @@ define([
                 // jobs paging
                 if (_.isEmpty(ModelJobPaging)) {
                     ModelJobPaging = new PagingModel({
-                        total: jobs.total,
-                        offset: jobs.offset,
-                        limit: jobs.limit
+                        total: res.total,
+                        offset: res.offset,
+                        limit: res.limit
                     });
                 } else {
                     ModelJobPaging.update({
-                        total: jobs.total,
-                        offset: jobs.offset,
-                        limit: jobs.limit
+                        total: res.total,
+                        offset: res.offset,
+                        limit: res.limit
                     });
                 }
 
@@ -199,8 +206,8 @@ define([
                     var pagenum = Number($(this).data('pagenum'));
                     if (pagenum !== 0) {
                         updateList({
-                            offset: (pagenum-1)*jobs.limit,
-                            limit: jobs.limit
+                            offset: (pagenum-1)*res.limit,
+                            limit: res.limit
                         });
                     }
                     return;
@@ -213,7 +220,7 @@ define([
          * @description get all the jobs per page
          */
         function getJobs () {
-            var strUrl = AppConfig.EVENT.JOB.GET_JOBS + '?' + Util.encodeURIDataObj(pagingObj);
+            var strUrl = AppConfig.EVENT.REST.JOBS + '?' + Util.encodeURIDataObj(pagingObj);
             logger.info('getJobs : ' + strUrl);
             return Util.GET(strUrl);
         }
index 019e4d9..29c85d4 100644 (file)
@@ -34,6 +34,7 @@ define([
     'use strict';
 
     var logger = Logger('login.js');
+    var CONFIG = null;
 
     /**
      * listener of input change in login
@@ -59,7 +60,7 @@ define([
             // prevent for sumbit of form
             e.preventDefault();
 
-            Util.POST('api/session', {
+            Util.POST(CONFIG.EVENT.REST.SESSION, {
                 email : $('#tic-page-login-form-email').val(),
                 password: $('#tic-page-login-form-password').val()
             })
@@ -96,7 +97,7 @@ define([
             // prevent for sumbit of form
             e.preventDefault();
 
-            Util.DELETE('api/session')
+            Util.DELETE(CONFIG.EVENT.REST.SESSION)
             .then(function (result) {
                 if (result.result) {
                     logger.info('logout success');
@@ -155,7 +156,7 @@ define([
      * FIXME: remove permission control in client side
      */
     function updatePermission() {
-        Util.GET('api/session')
+        Util.GET(CONFIG.EVENT.REST.SESSION)
         .then(function (result) {
             _updateLogin(result.data);
             _updatePackagePage(result.data);
@@ -167,11 +168,16 @@ define([
     function init() {
         logger.info('init');
 
-        _loginFormHandler();
-        _loginHandler();
-        _logoutHandler();
+        Util.getAppConfig()
+        .then(function (conf) {
+            CONFIG = conf;
 
-        updatePermission();
+            _loginFormHandler();
+            _loginHandler();
+            _logoutHandler();
+
+            updatePermission();
+        });
     }
 
     init();
index b6814e9..28e3b86 100644 (file)
@@ -89,14 +89,14 @@ define([
 
         function addJob () {
             var msgObj = {
-                format: AppConfig.EVENT.PACKAGE.EXPORT_KS,
+                format: AppConfig.EVENT.REST.EXPORT_KS,
                 recipes: Settings.getRecipeStore(),
                 packages: _.map(checkedPackagesList, 'name')
             };
 
             logger.info('onClickHandlerForImgCreationBtn.addJob');
 
-            return Util.POST(AppConfig.EVENT.JOB.ADD_JOB, msgObj);
+            return Util.POST(AppConfig.EVENT.REST.JOBS, msgObj);
         }
 
         // confirm dialog
index d952ee9..e9a4204 100644 (file)
@@ -79,7 +79,7 @@ define([
             recipes : recipeStore
         };
 
-        return Util.POST(CONFIG.EVENT.PACKAGE.ANALYSIS, postBody)
+        return Util.POST(CONFIG.EVENT.REST.ANALYSIS, postBody)
         .then(function (result) {
             if (result.result === 'true') {
                 setRecipeStore(result.data.recipes);
index 22489c6..47141f2 100644 (file)
  */
 
 define([
-    'jquery'
+    'jquery',
+    'js/logger'
 ], function (
-    $
+    $,
+    Logger
 ) {
     'use strict';
 
+    var logger = Logger('util.js');
+
     var socket = null;
     var config = null;
 
@@ -40,10 +44,14 @@ define([
         }
 
         function readLog(linkid) {
-            GET(config.EVENT.JOB.READ_LOG + linkid)
-            .then(function (line) {
-                 $('#' + targetId).append(line);
-                 $('#log-view-content').show();
+            GET(config.EVENT.REST.LOGS + linkid)
+            .then(function (res) {
+                if (res.result) {
+                    $('#' + targetId).append(res.data);
+                    $('#log-view-content').show();
+                } else {
+                    logger.error(res.message);
+                }
             });
         }