Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 1 | var express = require('express'); |
| 2 | var router = express.Router(); |
| 3 | var exec = require('child_process').exec; |
| 4 | var util = require('util'); |
| 5 | var fs = require('fs'); |
| 6 | var dbRoutes = require('./dbRoutes'); |
| 7 | var csp = require('./csp'); |
| 8 | var bodyParser = require('body-parser'); |
| 9 | var sax = require('sax'),strict=true,parser = sax.parser(strict); |
| 10 | |
| 11 | // SVC_LOGIC table columns |
| 12 | var _module=''; // cannot use module its a reserved word |
| 13 | var version=''; |
| 14 | var rpc=''; |
| 15 | var mode=''; |
| 16 | var xmlfile=''; |
| 17 | |
| 18 | |
| 19 | //router.use(bodyParser()); |
| 20 | router.use(bodyParser.urlencoded({ |
| 21 | extended: true |
| 22 | })); |
| 23 | |
| 24 | |
| 25 | // GET |
| 26 | router.get('/listUsers', csp.checkAuth, function(req,res) { |
| 27 | dbRoutes.listUsers(req,res, {user:req.session.loggedInAdmin,code:'', msg:''} ); |
| 28 | }); |
| 29 | router.get('/deleteUser', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 30 | dbRoutes.deleteUser(req,res, {code:'', msg:''} ); |
| 31 | }); |
| 32 | |
| 33 | //router.get('/activate', csp.checkAuth, dbRoutes.checkDB, function(req,res){ |
| 34 | |
| 35 | //var _module = req.query.module; |
| 36 | //var rpc = req.query.rpc; |
| 37 | //var version = req.query.version; |
| 38 | //var mode = req.query.mode; |
| 39 | |
| 40 | //dbRoutes.activate(req,res,_module,rpc,version,mode); |
| 41 | //}); |
| 42 | |
| 43 | //router.get('/deactivate', csp.checkAuth, dbRoutes.checkDB, function(req,res){ |
| 44 | |
| 45 | //var _module = req.query.module; |
| 46 | //var rpc = req.query.rpc; |
| 47 | //var version = req.query.version; |
| 48 | //var mode = req.query.mode; |
| 49 | // |
| 50 | //dbRoutes.deactivate(req,res,_module,rpc,version,mode); |
| 51 | //}); |
| 52 | |
| 53 | //router.get('/deleteDG', csp.checkAuth, dbRoutes.checkDB, function(req,res){ |
| 54 | |
| 55 | //var _module = req.query.module; |
| 56 | //var rpc = req.query.rpc; |
| 57 | //var version = req.query.version; |
| 58 | //var mode = req.query.mode; |
| 59 | |
| 60 | //dbRoutes.deleteDG(req,res,_module,rpc,version,mode); |
| 61 | //}); |
| 62 | /* |
| 63 | // SAX |
| 64 | parser.onerror = function (e) { |
| 65 | logger.debug('onerror'); |
| 66 | // an error happened. |
| 67 | }; |
| 68 | parser.ontext = function (t) { |
| 69 | // got some text. t is the string of text. |
| 70 | logger.debug('ontext:'+t); |
| 71 | }; |
| 72 | parser.onopentag = function (node) { |
| 73 | // opened a tag. node has "name" and "attributes" |
| 74 | if ( node.name == 'service-logic' ) |
| 75 | { |
| 76 | _module = node.attributes.module; |
| 77 | version = node.attributes.version; |
| 78 | } |
| 79 | if ( node.name == 'method' ) |
| 80 | { |
| 81 | rpc = node.attributes.rpc; |
| 82 | mode = node.attributes.mode; |
| 83 | } |
| 84 | }; |
| 85 | parser.onattribute = function (attr) { |
| 86 | // an attribute. attr has "name" and "value" |
| 87 | logger.debug('onattribute:'+attr); |
| 88 | }; |
| 89 | parser.onend = function () { |
| 90 | // parser stream is done, and ready to have more stuff written to it. |
| 91 | logger.debug('onend:'); |
| 92 | }; |
| 93 | */ |
| 94 | |
| 95 | |
| 96 | // POST |
| 97 | router.post('/updateUser', csp.checkAuth, dbRoutes.checkDB, function(req,res,next){ |
| 98 | dbRoutes.updateUser(req,res,{code:'',msg:''}); |
| 99 | }); |
| 100 | router.post('/addUser', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 101 | dbRoutes.addUser(req,res, {code:'', msg:''} ); |
| 102 | }); |
| 103 | |
| 104 | //router.post('/upload', csp.checkAuth, dbRoutes.checkDB, function(req, res, next){ |
| 105 | |
| 106 | /* |
| 107 | logger.debug("upload"); |
| 108 | if(req.files.filename){ |
| 109 | if (req.files.filename.size == 0) { |
| 110 | resultObj = |
| 111 | {code:'danger', msg:'There was an error uploading the file, please try again.'}; |
| 112 | dbRoutes.listSLA(req,res, resultObj); |
| 113 | } |
| 114 | fs.exists(req.files.filename.path, function(exists) { |
| 115 | if(exists) { |
| 116 | resultObj = {code:'success', msg:'File sucessfully uploaded.'}; |
| 117 | |
| 118 | // parse xml |
| 119 | try { |
| 120 | var file_buf = fs.readFileSync(req.files.filename.path, "utf8"); |
| 121 | logger.debug('file '+req.files.filename); |
| 122 | |
| 123 | |
| 124 | // call Dan's svclogic shell script from here |
| 125 | var commandToExec = process.cwd() |
| 126 | + "/shell/svclogic.sh load " |
| 127 | + req.files.filename.path + " " |
| 128 | + process.cwd() |
| 129 | + "/config/svclogic.properties"; |
| 130 | |
| 131 | logger.debug("commandToExec:" + commandToExec); |
| 132 | child = exec(commandToExec ,function (error,stdout,stderr){ |
| 133 | if(error){ |
| 134 | logger.info("error:" + error); |
| 135 | } |
| 136 | if(stderr){ |
| 137 | logger.info("stderr:" + stderr); |
| 138 | } |
| 139 | if(stdout){ |
| 140 | logger.info("OUTPUT:" + stdout); |
| 141 | dbRoutes.listSLA(req,res, resultObj); |
| 142 | } |
| 143 | |
| 144 | // remove the grave accents, the sax parser does not like them |
| 145 | //parser.write(file_buf.replace(/\`/g,'').toString('utf8')).close(); |
| 146 | //dbRoutes.addDG(_module,version,rpc,mode,file_buf,req,res); |
| 147 | //dbRoutes.listSLA(req,res, resultObj); |
| 148 | }); |
| 149 | } catch(ex) { |
| 150 | // keep 'em silent |
| 151 | logger.debug('sax error:'+ex); |
| 152 | } |
| 153 | |
| 154 | } else { |
| 155 | resultObj = |
| 156 | {code:'danger', msg:'There was an error uploading the file, please try again.'}; |
| 157 | dbRoutes.listSLA(req,res, resultObj); |
| 158 | } |
| 159 | }); |
| 160 | } |
| 161 | else { |
| 162 | resultObj = |
| 163 | {code:'danger', msg:'There was an error uploading the file, please try again.'}; |
| 164 | dbRoutes.listSLA(req,res, resultObj); |
| 165 | } |
| 166 | }); |
| 167 | */ |
| 168 | |
| 169 | module.exports = router; |