App server for the CLIENT FW
authorseshukm <seshu.kumar.m@huawei.com>
Tue, 24 Jan 2017 09:07:50 +0000 (14:37 +0530)
committerseshukm <seshu.kumar.m@huawei.com>
Tue, 24 Jan 2017 09:07:50 +0000 (14:37 +0530)
nodejs based client server FW.

Issue-Id:CLIENT-11

Change-Id: I369f4519c1d83c7010da2bd45a281898e8ed9bdf
Signed-off-by: seshukm <seshu.kumar.m@huawei.com>
portal-common/src/main/webapp/framework/appserver/api.js [new file with mode: 0644]
portal-common/src/main/webapp/framework/appserver/server.js [new file with mode: 0644]
portal-common/src/main/webapp/framework/appserver/start_node.sh [new file with mode: 0644]

diff --git a/portal-common/src/main/webapp/framework/appserver/api.js b/portal-common/src/main/webapp/framework/appserver/api.js
new file mode 100644 (file)
index 0000000..a002fce
--- /dev/null
@@ -0,0 +1,287 @@
+/*\r
+\r
+    Copyright 2016-2017, Huawei Technologies Co., Ltd.\r
+\r
+    Licensed under the Apache License, Version 2.0 (the "License");\r
+    you may not use this file except in compliance with the License.\r
+    You may obtain a copy of the License at\r
+\r
+            http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+    Unless required by applicable law or agreed to in writing, software\r
+    distributed under the License is distributed on an "AS IS" BASIS,\r
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+    See the License for the specific language governing permissions and\r
+    limitations under the License.\r
+\r
+*/\r
+\r
+var express = require('express');\r
+var router = express.Router();\r
+/*var js2xmlparser = require("js2xmlparser");\r
+var xml2js = require('xml2js');*/\r
+var fs = require('fs');\r
+var http = require('http');\r
+var bodyParser = require('body-parser');\r
+var path=require('path');\r
+\r
+var MongoClient = require('mongodb').MongoClient;\r
+var ObjectId = require('mongodb').ObjectId;\r
+\r
+var url = 'mongodb://localhost/rest_test';\r
+\r
+/*var exists = require('file-exists');*/\r
+\r
+// ############################################  Sign In code #################################################\r
+// Routes\r
+router.post('/signin', function (req, res) {\r
+       console.log("signin");\r
+       var obj = {\r
+               "name": req.body.name,\r
+               "pswd": req.body.pswd\r
+       };\r
+\r
+       var ObjLst =[] ;\r
+       var nameExist =0;\r
+       console.log(obj);\r
+       ObjLst = JSON.parse(fs.readFileSync('input.json', 'utf8'));\r
+       for(var index = 0; index < ObjLst.length; index++) {\r
+               if (ObjLst[index].name === req.body.name && ObjLst[index].pswd === req.body.pswd) {\r
+                       console.log("success");\r
+                       nameExist++;\r
+                       res.statusCode=200;\r
+                       res.statusMessage="Sucessfully loged in";\r
+                       break;\r
+               }\r
+       }\r
+       if (nameExist != 1) {\r
+               console.log("failed");\r
+               res.statusCode=404;\r
+               res.statusMessage="failed to login";\r
+       }\r
+       res.send();\r
+});\r
+\r
+// ############################################  Sign Up Code #################################################\r
+\r
+router.post('/signup', function (req, res) {\r
+\r
+       console.log("in api.js");\r
+\r
+       var ObjLst =[] ;\r
+       var nameNotExist =0;\r
+       var obj = {\r
+               "name": req.body.name,\r
+               "pswd": req.body.pswd,\r
+               "email": req.body.email\r
+       };\r
+       if(fs.existsSync("input.json")) {\r
+               console.log("File Exist");\r
+               ObjLst = JSON.parse(fs.readFileSync('input.json', 'utf8'));\r
+       }\r
+\r
+       for(var index = 0; index < ObjLst.length; index++) {\r
+               if (ObjLst[index].name === req.body.name && ObjLst[index].email === req.body.email) {\r
+                       console.log("success");\r
+                       nameNotExist++;\r
+                       break;\r
+               }\r
+       }\r
+       if (nameNotExist == 0) {\r
+               ObjLst.push(obj);\r
+               console.log(ObjLst);\r
+               fs.writeFile('input.json', JSON.stringify(ObjLst),  function(err) {\r
+                       if (err) {\r
+                               return console.error(err);\r
+                       }\r
+                       console.log("Data written successfully!");\r
+                       console.log("Let's read newly written data");\r
+                       res.statusCode=200;\r
+                       res.statusMessage="Sucessfully signed up";\r
+               });\r
+       }\r
+       res.send();\r
+});\r
+\r
+function getDataFrmProvince() {\r
+    var provinceData =[] ;\r
+    if(fs.existsSync("provinceData.json")) {\r
+        console.log("File Exist");\r
+        provinceData = JSON.parse(fs.readFileSync('provinceData.json', 'utf8'));\r
+    }\r
+    return provinceData;\r
+}\r
+\r
+function saveDataToProvince(provinceData) {\r
+    fs.writeFile('provinceData.json', JSON.stringify(provinceData),  function(err) {\r
+        var statueCode = 0;\r
+        if (err) {\r
+            console.error(err);\r
+            return statueCode = 404;\r
+        }\r
+        console.log("Data written successfully!");\r
+        console.log("Let's read newly written data");\r
+        return statueCode = 200;\r
+    });\r
+}\r
+\r
+function getIndexOfIdProvince(id) {\r
+    var provinceData = getDataFrmProvince();\r
+    var returnIndx = -1;\r
+    for (var index = 0; index < provinceData.length; index++) {\r
+        if(provinceData[index].id == id) {\r
+            returnIndx = index;\r
+            break;\r
+        }\r
+    }\r
+    return returnIndx;\r
+}\r
+\r
+function deleteIdFromProvince(idList) {\r
+    var provinceData = getDataFrmProvince();\r
+    /*for (var index = 0; index < provinceData.length; index++) {\r
+        if(provinceData[index].id == id) {\r
+            console.log("Deleting id : " + index);\r
+            provinceData.splice(index, 1);\r
+            break;\r
+        }\r
+    }*/\r
+    for(var i = 0; i < idList.length; i++) {\r
+        for (var index = 0; index < provinceData.length; index++) {\r
+            if(provinceData[index].id == idList[i]) {\r
+                console.log("Deleting id : " + index);\r
+                provinceData.splice(index, 1);\r
+                break;\r
+            }\r
+        }\r
+    }\r
+    return provinceData;\r
+}\r
+\r
+router.get('/getAllProvinceData', function (req, res) {\r
+    var provinceData = getDataFrmProvince();\r
+    if(provinceData) {\r
+        output = '{"provinceData" : ' + JSON.stringify(provinceData) + '}';\r
+\r
+        res.setHeader("Content-Type", "application/json");\r
+        console.log("output : " + JSON.stringify(output));\r
+        res.end(output);\r
+    }\r
+    /*MongoClient.connect(url, function(err, db) {\r
+        console.log("Connected... :-)");\r
+        var cursor = db.collection('ProvinceData').find({},function(err, cursor) {\r
+            cursor.toArray(function (err, items) {\r
+                output = '{"provinceData" : ' + JSON.stringify(items) + '}';\r
+\r
+                res.setHeader("Content-Type", "application/json");\r
+                //console.log("output : " + JSON.stringify(output));\r
+                res.end(output);\r
+            });\r
+        });\r
+        db.close(function(){\r
+            console.log("Connection Closed... :-)");\r
+        });\r
+    });*/\r
+});\r
+\r
+\r
+router.post('/addProvinceData', function(req, res) {\r
+    var provinceData = getDataFrmProvince();\r
+    var gen_id = Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000;\r
+    provinceData.push({\r
+        "id": gen_id,\r
+        "province_name": req.body.province_name,\r
+        "ip": req.body.ip,\r
+        "port": req.body.port\r
+    });\r
+    var statusCode = saveDataToProvince(provinceData);\r
+    res.statusCode=statusCode;\r
+\r
+    if(statusCode == 200) {\r
+        res.statusMessage="Sucessfully signed up";\r
+    }\r
+    else {\r
+        res.statusMessage="Error";\r
+    }\r
+\r
+    /*MongoClient.connect(url, function(err, db) {\r
+        db.collection('ProvinceData').insertOne({\r
+            province_name: req.body.province_name,\r
+            ip: req.body.ip,\r
+            port: req.body.port\r
+        });\r
+        db.close();\r
+    });*/\r
+    res.statusCode=200;\r
+    res.statusMessage="Sucessfully signed up";\r
+    res.send();\r
+});\r
+\r
+router.post('/deleteProvinceData', function (req, res) {\r
+    //var provinceData = getDataFrmProvince();\r
+    console.log("IdList: "+ req.body.idList);\r
+    /*for(var i = req.body.idList.length - 1; i >= 0; i--) {\r
+        var index = getIndexOfIdProvince(req.body.idList[i]);\r
+        console.log("Deleting id : " + index);\r
+        if(index != -1) {\r
+            provinceData.splice(index, 1);\r
+        }\r
+        //deleteIdFromProvince(req.body.idList[i]);\r
+    };*/\r
+\r
+    var provinceData = deleteIdFromProvince(req.body.idList);\r
+\r
+    saveDataToProvince(provinceData);\r
+\r
+    /*MongoClient.connect(url, function(err, db) {\r
+\r
+        console.log("Deleting Province Data... " + req.body.idList);\r
+        for(var i = 0; i < req.body.idList.length; i++) {\r
+            db.collection('ProvinceData').deleteOne({ "_id": ObjectId(req.body.idList[i])});\r
+        }\r
+        /!*db.collection('ProvinceData').deleteOne({ "_id": ObjectId(req.body.idList)});*!/\r
+\r
+        db.close(function(){\r
+            console.log("Connection Closed... :-)");\r
+        });\r
+        res.send();\r
+    });*/\r
+    res.statusCode=200;\r
+    res.statusMessage="Sucessfully signed up";\r
+    res.send();\r
+});\r
+\r
+router.post('/editProvinceData', function (req, res) {\r
+    var provinceData = getDataFrmProvince();\r
+    var index = getIndexOfIdProvince(req.body.id);\r
+    console.log("Editing id : " + index);\r
+    if(index != -1) {\r
+        provinceData[index].province_name = req.body.province_name;\r
+        provinceData[index].ip = req.body.ip;\r
+        provinceData[index].port = req.body.port;\r
+    }\r
+    saveDataToProvince(provinceData);\r
+\r
+    res.statusCode=200;\r
+    res.statusMessage="Sucessfully signed up";\r
+    res.send();\r
+    /*MongoClient.connect(url, function(err, db) {\r
+\r
+        console.log("Editing Province Data... " + req.body._id);\r
+        db.collection('ProvinceData').updateOne(\r
+            { "_id": ObjectId(req.body._id)},\r
+            {\r
+                $set: {'province_name': req.body.province_name, 'ip': req.body.ip, 'port': req.body.port}\r
+            }\r
+        );\r
+\r
+        db.close(function(){\r
+            console.log("Connection Closed... :-)");\r
+        });\r
+        res.send();\r
+    });*/\r
+});\r
+\r
+// Return router\r
+module.exports = router; \r
+\r
diff --git a/portal-common/src/main/webapp/framework/appserver/server.js b/portal-common/src/main/webapp/framework/appserver/server.js
new file mode 100644 (file)
index 0000000..3a2e05b
--- /dev/null
@@ -0,0 +1,48 @@
+/*\r
+\r
+    Copyright 2016-2017, Huawei Technologies Co., Ltd.\r
+\r
+    Licensed under the Apache License, Version 2.0 (the "License");\r
+    you may not use this file except in compliance with the License.\r
+    You may obtain a copy of the License at\r
+\r
+            http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+    Unless required by applicable law or agreed to in writing, software\r
+    distributed under the License is distributed on an "AS IS" BASIS,\r
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+    See the License for the specific language governing permissions and\r
+    limitations under the License.\r
+\r
+*/\r
+\r
+// Dependencies\r
+var express = require('express');\r
+var bodyParser = require('body-parser');\r
+var http=require("http");\r
+var path=require('path');\r
+\r
+\r
+// Express\r
+var app = express();\r
+app.use(bodyParser.urlencoded({ extended: true}));\r
+app.use(bodyParser.json());\r
+app.use(function(req, res, next) {\r
+    res.header("Access-Control-Allow-Origin", "*");\r
+    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");\r
+    next();\r
+});\r
+app.use(express.static(__dirname + '/POC_Angular'));\r
+\r
+// Routes\r
+app.use('/api', require('./api'));\r
+\r
+// viewed at http://localhost:3000\r
+app.get('/', function(req, res) {\r
+       res.sendFile(path.join(__dirname + '/POC_Angular/index.html'));\r
+});\r
+\r
+// Start server\r
+app.listen(3000);\r
+console.log('API is running on port 3000');\r
+console.log('API @@@@');
\ No newline at end of file
diff --git a/portal-common/src/main/webapp/framework/appserver/start_node.sh b/portal-common/src/main/webapp/framework/appserver/start_node.sh
new file mode 100644 (file)
index 0000000..c886d08
--- /dev/null
@@ -0,0 +1,22 @@
+#
+#    Copyright 2016-2017, Huawei Technologies Co., Ltd.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License");
+#    you may not use this file except in compliance with the License.
+#    You may obtain a copy of the License at
+#
+#            http://www.apache.org/licenses/LICENSE-2.0
+#
+#    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.
+#
+
+@ECHO OFF
+ECHO.
+ECHO Starting Node Server
+ECHO.
+node server.js
+EXIT
\ No newline at end of file