From 76eb0c10a55e6da6e7b42fcbcbf9c13dde532573 Mon Sep 17 00:00:00 2001 From: ChangHyun Lee Date: Mon, 26 Dec 2016 15:43:55 +0900 Subject: [PATCH] [TIC-UI] add config file and modified the RESTful API handled by the server - add config file - Modified the RESTful API handled by the server Change-Id: I4b5da20d5090618c7e732011e4f1b184c7a02853 Signed-off-by: ChangHyun Lee --- app.js | 64 ++++++++++++++++++++++++++++++++++-------- config.json | 8 ++++++ public/src/js/page/settings.js | 13 +++------ 3 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 config.json diff --git a/app.js b/app.js index 31bded6..86d089d 100644 --- 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 index 0000000..63ef823 --- /dev/null +++ b/config.json @@ -0,0 +1,8 @@ +{ + "TIC_WEB": { + "PORT" : 8081 + }, + "TIC_CORE": { + "PORT" : 8082 + } +} \ No newline at end of file diff --git a/public/src/js/page/settings.js b/public/src/js/page/settings.js index d5f88e1..07d9f83 100644 --- a/public/src/js/page/settings.js +++ b/public/src/js/page/settings.js @@ -11,7 +11,7 @@ define([ ) { 'use strict'; - var ANALYSIS_URL = '<%= protocol %>//<%= hostname %>:<%= port %>/analysis'; + var ANALYSIS_URL = '<%= url %>/analysis'; var ROPO_LI = '
  • <%= url %>
  • '; 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; -- 2.7.4