[TIC-UI] add config file and modified the RESTful API handled by the server 14/107014/4
authorChangHyun Lee <leechwin.lee@samsung.com>
Mon, 26 Dec 2016 06:43:55 +0000 (15:43 +0900)
committerChangHyun Lee <leechwin.lee@samsung.com>
Mon, 26 Dec 2016 11:46:40 +0000 (20:46 +0900)
- add config file
- Modified the RESTful API handled by the server

Change-Id: I4b5da20d5090618c7e732011e4f1b184c7a02853
Signed-off-by: ChangHyun Lee <leechwin.lee@samsung.com>
app.js
config.json [new file with mode: 0644]
public/src/js/page/settings.js

diff --git a/app.js b/app.js
index 31bded6..86d089d 100644 (file)
--- a/app.js
+++ b/app.js
@@ -1,3 +1,4 @@
+/* middleware */
 var http = require('http');
 var path = require('path');
 
@@ -5,19 +6,20 @@ var socketio = require('socket.io');
 var fs = require('fs');
 var express = require('express');
 var bodyParser = require('body-parser');
+var app = express();
+var server = http.createServer(app);
+var io = socketio.listen(server);
 
+/* 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();
-var server = http.createServer(app);
-var io = socketio.listen(server);
-
-app.set('port', process.env.PORT || 8081);
+/* app config */
+app.set('port', process.env.PORT || Config.TIC_WEB.PORT);
 app.use(bodyParser.json());
-app.use(bodyParser.urlencoded());
-
+app.use(bodyParser.urlencoded({extended: true}));
 app.use(express.static(path.join(__dirname, '/public/src'))); //module directory
 app.use('/api', Router);
 
@@ -28,10 +30,48 @@ server.listen(app.get('port'), process.env.IP || "0.0.0.0", function () {
     FileSystem.init();
 });
 
-/* server - socket test
-   TODO: server side logic
-   - fs controllers(read/create)
-   - connection with MIC
+
+
+/**
+ * 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();
+});
+
+
+
+/**
+ * FileSystem controller (read/create)
+ * Connection with MIC
  */
 io.on('connection', function (socket) {
     console.log('socket connection');
@@ -81,4 +121,4 @@ io.on('connection', function (socket) {
         console.log('socket message');
     });
 
-});
\ No newline at end of file
+});
diff --git a/config.json b/config.json
new file mode 100644 (file)
index 0000000..63ef823
--- /dev/null
@@ -0,0 +1,8 @@
+{
+    "TIC_WEB": {
+        "PORT" : 8081
+    },
+    "TIC_CORE": {
+        "PORT" : 8082
+    }
+}
\ No newline at end of file
index d5f88e1..07d9f83 100644 (file)
@@ -11,7 +11,7 @@ define([
 ) {
     'use strict';
 
-    var ANALYSIS_URL = '<%= protocol %>//<%= hostname %>:<%= port %>/analysis';
+    var ANALYSIS_URL = '<%= url %>/analysis';
     var ROPO_LI = '<li class="list-group-item clearfix"><span class="tic-repository-url"><%= url %></span><button type="button" class="btn pull-right btn-default"><i class="fa fa-trash-o"></i></button></li>';
 
     var repoStore = [];  // { name, url }
@@ -44,9 +44,7 @@ define([
 
     function _getAnalysisUrl() {
         return _.template(ANALYSIS_URL)({
-            protocol: location.protocol,
-            hostname: location.hostname,
-            port: parseInt(location.port) + 1 // tic-core port
+            url: location.origin
         });
     }
 
@@ -59,13 +57,10 @@ define([
             };
             $.ajax({
                 type: 'POST',
-                contentType: 'text/plain',
-                data: JSON.stringify(postBody),
+                contentType: 'application/json; charset=UTF-8',
                 dataType: 'json',
+                data: JSON.stringify(postBody),
                 processData: false,
-                // TODO: test url
-                // url: './temp/test.json',
-                // url: 'http://172.21.110.103:59001/analysis',
                 url: _getAnalysisUrl(),
                 success: function(rawData) {
                     repoStore = rawData.data.repos;