From c4a567d009fbbfd669d02bfd5c614df842d3bae9 Mon Sep 17 00:00:00 2001 From: Sumate Kongpui Date: Tue, 28 Aug 2018 15:26:29 +0700 Subject: [PATCH] merge --- ais-structure/config.txt | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ais-structure/src/config/express.js | 40 +++++++++++++++++++++++----------------- ais-structure/src/modules/customer/customer.ctrl.js | 24 ++++++++++++------------ ais-structure/src/modules/customer/customer.route.js | 24 ++++++++++++------------ ais-structure/src/modules/helper/connection.js | 316 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------- ais-structure/src/modules/helper/log.js | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------ ais-structure/src/modules/main.route.js | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ais-structure/src/modules/vizcard/vizCard.ctrl.js | 18 +++++++++--------- 8 files changed, 472 insertions(+), 246 deletions(-) create mode 100644 ais-structure/config.txt create mode 100644 ais-structure/src/modules/main.route.js diff --git a/ais-structure/config.txt b/ais-structure/config.txt new file mode 100644 index 0000000..2ce9f04 --- /dev/null +++ b/ais-structure/config.txt @@ -0,0 +1,74 @@ +{ + "development": { + "app_name": "CMF", + "app_host": "0.0.0.0", + "app_port": "3000", + "app_https": false, + "KEY": "key1.pem", + "CERT": "cert1.pem", + "service": { + "PANDORA": { + "URI": "http://25.27.7.151:34680", + "URI_EQX": "http://25.27.7.151:15300", + "KEY": "./PANDORA_CERT/server.key", + "CERT": "./PANDORA_CERT/server.pem", + "PREFIX": "/phxPartner/v1/partner" + }, + "SACF": { + "Name": "SACF", + "POST_SearchCustomer_URL": "http://10.1.3.74:8080", + "POST_SearchCustomer_Timeout": 10 + }, + "D01": { + "Name": "D01", + "GET_Customer_URL": "http://localhost:8080", + "GET_Customer_Timeout": 10, + "POST_Customer_URL": "http://localhost:8080", + "POST_Customer_Timeout": 10, + "POST_Limit": 20, + "GET_Limit": 1000 + }, + "CMF": { + "Name": "CMF", + "GET_VIZCard_URL": [ + "/cmf/v1/vizcards/:vizCardId", + "/cmf/v1/vizcards" + ], + "GET_Customer_URL": [ + "/cmf/v1/customers/:customerId", + "/cmf/v1/customers/:userType/:userData", + "/cmf/v1/customers" + ], + "POST_Customer_URL": [ + "/cmf/v1/customers" + ], + "GET_CustomerMembership_URL": [ + "/cmf/v1/customers/:customerId/membercards", + "/cmf/v1/customers/:userType/:userData/membercards", + "/cmf/v1/customers/membercards" + ], + "POST_CustomerMembership_URL": [ + "/cmf/v1/customers/:customerId/membercards", + "/cmf/v1/customers/:userType/:userData/membercards" + ] + } + }, + "log": { + "logTime": 15, + "statTime": 2, + "detailTime": 15, + "summaryTime": 15, + "statInterval": 60, + "appLogPath": "./logTest/", + "summaryPath": "./logTest/", + "statPath": "./logTest/", + "detailPath": "./logTest/", + "level": "debug" + }, + "http_req_timeout": 120, + "session": 30, + "Default_Timeout": 10, + "Default_Retry": 5, + "Uri_length": 1000 + } +} \ No newline at end of file diff --git a/ais-structure/src/config/express.js b/ais-structure/src/config/express.js index a94bdec..e20fb8b 100644 --- a/ais-structure/src/config/express.js +++ b/ais-structure/src/config/express.js @@ -160,6 +160,7 @@ module.exports = function () { var headerLog = 'IP|'+remoteIp+'|USER|'+username+'|REQUESTID|'+req.id; logger.setHeader(headerLog); + // console.log(headerLog); // console.log("req " +req.headers['x-token']); // console.log("fullUrl " +req.originalUrl); // console.log("method " +req.method); @@ -209,10 +210,7 @@ module.exports = function () { return res.resBody?res.resBody:'' ; }) - app.use( function( req, res, next ) { - log.startEC(req); - next(); -} ); + // app.use(morgan('IP|:remote-ip|USER|:user-id|REQUESTID|:id|METHOD|:method|URI|:uri|REQUESTBODY|:json|RESPSTATUS|:status|RESPBODY|:resp-body|RESPTIME|:response-time', { // "stream": logger.stream @@ -223,6 +221,13 @@ module.exports = function () { // var ALL_ROUTES = require('../routes/index').GET_ALL_ROUTES(); // app.use('/', ALL_ROUTES); + app.use( function( req, res, next ) { + // console.log(req.id); + log.startEC(req); + next(); + }); + + logger.info("load module"); var load = require('express-load'); // load('modules', { @@ -274,19 +279,20 @@ module.exports = function () { /* ------------- [END NOT MATCH ROUTE - 404 ] ------------ */ app.use( function( req, res, next ) { - // console.log("end"); - // console.log(req.res.resBody); - if(req.res.resBody) - { - log.logSummary(JSON.parse(req.res.resBody)); - }else - { - res.status(200).json(responseMsg.unknow(req)); - } - - log.endEC(res); - next(); -} ); + // console.log("end"); + // console.log(req.res.resBody); + if(req.res.resBody) + { + log.logSummary(req,JSON.parse(req.res.resBody)); + }else + { + res.status(200).json(responseMsg.unknow(req)); + } + + log.endEC(req,res); + next(); + } ); + return app; }; diff --git a/ais-structure/src/modules/customer/customer.ctrl.js b/ais-structure/src/modules/customer/customer.ctrl.js index 86411fc..b5990e8 100644 --- a/ais-structure/src/modules/customer/customer.ctrl.js +++ b/ais-structure/src/modules/customer/customer.ctrl.js @@ -10,7 +10,6 @@ exports.customer = async function (req, res, next) { var sendCmd = "Customer"; var d01 = "D01" var method = constant.METHOD.GET - console.log(req.params); if(req.params.customerId){ @@ -20,25 +19,26 @@ exports.customer = async function (req, res, next) { } else if(req.query.clientName && req.query.commandId){ var customerId = req.query.clientName+"@"+req.query.commandId; } +; - log.startlog(method+"_"+getCmd,req.query.commandId,customerId); - log.logDetail.addInput(req.query.clientName,method+"_"+getCmd,constant.REQUEST,req,req.body); + log.startlog(req,method+"_"+getCmd,req.query.commandId,customerId); + log.logDetail.addInput(req,req.query.clientName,method+"_"+getCmd,constant.REQUEST,req,req.body); var err = validator(req,getCmd); if(err.length > 0) { - log.addErrorSummary(req.query.clientName,method+"_"+getCmd,"null",constant.FAIL); + log.addErrorSummary(req,req.query.clientName,method+"_"+getCmd,"null",constant.FAIL); log.log(err,'error'); var response = responseMsg.error(req,getCmd,40300); // res.status(200).json(response); }else { - log.addSuccessSummary(req.query.clientName,method+"_"+getCmd,"null",constant.SUCCESS); + log.addSuccessSummary(req,req.query.clientName,method+"_"+getCmd,"null",constant.SUCCESS); var objectData = Object.assign(req.query,req.params); - const result = await connection.requestJsonToD01(objectData,sendCmd,method); + const result = await connection.requestJsonToD01(req,objectData,sendCmd,method); // console.log(Object.keys(result)) - log.logDetail.addInput(d01,method+"_"+sendCmd,constant.RESPONSE,result,result.response); + log.logDetail.addInput(req,d01,method+"_"+sendCmd,constant.RESPONSE,result,result.response); if(typeof result.err === 'undefined'){ // console.log(result.response); var resultObj = result.response; @@ -48,21 +48,21 @@ exports.customer = async function (req, res, next) { { stats.receiveRestResponse(d01,method,sendCmd,constant.SUCCESS); var response = responseMsg.success(req,getCmd,resultObj); - log.addSuccessSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage); + log.addSuccessSummary(req,d01,method+"_"+sendCmd,response.resultCode,response.developerMessage); }else { stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage); var response = responseMsg.error(req,getCmd,40401); - log.addErrorSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage); + log.addErrorSummary(req,d01,method+"_"+sendCmd,response.resultCode,response.developerMessage); } } else if(resultObj.resultCode.startsWith("404")){ stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage); var response = responseMsg.direct(req,getCmd,resultObj); - log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage); + log.addErrorSummary(req,d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage); } else if(resultObj.resultCode.startsWith("5")){ stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR); var response = responseMsg.direct(req,getCmd,resultObj); - log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage); + log.addErrorSummary(req,d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage); } else { stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR); var response = responseMsg.error(req,getCmd,50000); @@ -74,7 +74,7 @@ exports.customer = async function (req, res, next) { } } - log.logDetail.addOutput(req.query.clientName,method+"_"+getCmd,constant.RESPONSE,response,response); + log.logDetail.addOutput(req,req.query.clientName,method+"_"+getCmd,constant.RESPONSE,response,response); res.status(200).json(response); next(); }; diff --git a/ais-structure/src/modules/customer/customer.route.js b/ais-structure/src/modules/customer/customer.route.js index dc3e812..d880ea3 100644 --- a/ais-structure/src/modules/customer/customer.route.js +++ b/ais-structure/src/modules/customer/customer.route.js @@ -5,19 +5,19 @@ module.exports = function (app) { var customerCtrl = app.modules.customer.customerCtrl; var postCustomerCtrl = app.modules.customer.postCustomerCtrl; - app.get(cfg.service.CMF.GET_Customer_URL[0], - customerCtrl.customer - ); + // app.get(cfg.service.CMF.GET_Customer_URL[0], + // customerCtrl.customer + // ); - app.get(cfg.service.CMF.GET_Customer_URL[1], - customerCtrl.customer - ); + // app.get(cfg.service.CMF.GET_Customer_URL[1], + // customerCtrl.customer + // ); - app.get(cfg.service.CMF.GET_Customer_URL[2], - customerCtrl.customer - ); + // app.get(cfg.service.CMF.GET_Customer_URL[2], + // customerCtrl.customer + // ); - app.post(cfg.service.CMF.POST_Customer_URL[0], - postCustomerCtrl.postCustomer - ); + // app.post(cfg.service.CMF.POST_Customer_URL[0], + // postCustomerCtrl.postCustomer + // ); }; diff --git a/ais-structure/src/modules/helper/connection.js b/ais-structure/src/modules/helper/connection.js index 1f16314..a64a885 100644 --- a/ais-structure/src/modules/helper/connection.js +++ b/ais-structure/src/modules/helper/connection.js @@ -9,194 +9,206 @@ var constants = require('../helper/constants.js'); var connection = []; -connection.requestSoapToSACF = async function (soap,cmd,myMethod) { +connection.requestSoapToSACF = async function (req,soap,cmd,myMethod) { + try { + log.log("Connect to SACF") + + var service = cfg.service.SACF; + var retry = service[myMethod+"_"+cmd+"_Retry"]; + + var params = { + url : service[myMethod+"_"+cmd+"_URL"], + body : messageSOAP.objectToSOAP(soap,cmd), + method : myMethod, + timeout : cfg.service.SACF[myMethod+"_"+cmd+"_Timeout"]*1000 + } - log.log("Connect to SACF") + log.log("requestData : "+JSON.stringify(params)) - var service = cfg.service.SACF; - var retry = service[myMethod+"_"+cmd+"_Retry"]; + if(!retry) + retry = cfg.Default_Retry; - var params = { - url : service[myMethod+"_"+cmd+"_URL"], - body : messageSOAP.objectToSOAP(soap,cmd), - method : myMethod, - timeout : cfg.service.SACF[myMethod+"_"+cmd+"_Timeout"]*1000 - } + var objectData = { + maxRetry : retry, + curRetry : 1 + } + + var funS = function(){ + stats.sendSACFRequest(service.Name,cmd); + log.logDetail.addOutput(req,service.Name,myMethod+"_"+cmd,constants.REQUEST,params,soap); + }; + + var funStats = { + start : funS, + error : function(response){log.addErrorSummary(req,service.Name,myMethod+"_"+cmd,response.resultCode,response.resultDescription);} + }; - log.log(JSON.stringify(params)) - if(!retry) - retry = cfg.Default_Retry; + var result = await asyncRequest(params,objectData,funStats); - var objectData = { - maxRetry : retry, - curRetry : 1 + return checkResponse(result,funStats); } - - var funS = function(){ - stats.sendSACFRequest(service.Name,cmd); - log.logDetail.addOutput(service.Name,myMethod+"_"+cmd,constants.REQUEST,params,soap); - }; - - var funStats = { - start : funS, - error : function(response){log.addErrorSummary(service.Name,myMethod+"_"+cmd,response.resultCode,response.resultDescription);} - }; - + catch(err) { + log.log(err); - var result = await asyncRequest(params,objectData,funStats); + var result = { + response : constants.RESPONSECONN.MESSAGE.ERROR + }; + return result; - return checkResponse(result,funStats); + } }; -connection.requestJsonToD01 = async function (obj,cmd,myMethod) { - - log.log("Connect to D01") - - var service = cfg.service.D01; - var retry = service[myMethod+"_"+cmd+"_Retry"]; - if(!retry) - retry = cfg.Default_Retry; +connection.requestJsonToD01 = async function (req,obj,cmd,myMethod) { + try { + + log.log("Connect to D01") - var objectData = { - maxRetry : retry, - curRetry : 1 - } + var service = cfg.service.D01; + var retry = service[myMethod+"_"+cmd+"_Retry"]; + if(!retry) + retry = cfg.Default_Retry; - // JSON.stringify(json) - var params = { - url : service[myMethod+"_"+cmd+"_URL"], - body : obj, - method : myMethod, - timeout : service[myMethod+"_"+cmd+"_Timeout"]*1000 - } + var objectData = { + maxRetry : retry, + curRetry : 1 + } - log.log(JSON.stringify(params)) + // JSON.stringify(json) + var params = { + url : service[myMethod+"_"+cmd+"_URL"], + body : obj, + method : myMethod, + timeout : service[myMethod+"_"+cmd+"_Timeout"]*1000 + } - var funStats = function(){ - stats.sendRestRequest(service.Name,myMethod,cmd); - log.logDetail.addOutput(service.Name,myMethod+"_"+cmd,constants.Request,params,obj); - }; - - var funStats = { - start : funStats, - error : function(response){log.addErrorSummary(service.Name,myMethod+"_"+cmd,response.resultCode,response.resultDescription);} - }; + log.log("requestData : "+JSON.stringify(params)) - + var funStats = function(){ + stats.sendRestRequest(service.Name,myMethod,cmd); + log.logDetail.addOutput(req,service.Name,myMethod+"_"+cmd,constants.Request,params,obj); + }; + + var funStats = { + start : funStats, + error : function(response){log.addErrorSummary(req,service.Name,myMethod+"_"+cmd,response.resultCode,response.resultDescription);} + }; - var result = {}; - result.response = {}; - result.response.resultData = []; + - + var result = {}; + result.response = {}; + result.response.resultData = []; - if(params.method == constants.METHOD.GET) - { - var genUrl = getUrl(params,service["searchRecordLimit"]); - if(genUrl.length > 1) - { - console.log("multi Send"); - var multiSend = []; - for(var i=0;i 1) { - // console.log("start loop"); - var loopRes = parallelRes[i]; - - if(loopRes.response) + console.log("multi Send"); + var multiSend = []; + for(var i=0;i Incoming Message <====# "; + messageRes += newLine+"Session : "+req.id; messageRes += newLine+"Header : "+header; messageRes += newLine+"Body : "+body; messageRes += newLine+"Time in : "+startTime.toISOString(); messageRes += newLine - logger.log(messageRes); + logg.debug(messageRes); + // logger.log(messageRes); }; -logger.endEC = function(res) +logger.endEC = function(req,res) { var newLine = "\r\n"; var messageRes = ''; + var startTime = mapLog[req.id].startTime; var endTime = new Date(); + + // console.log(mapLog); var body = ""; if(res.resBody) @@ -241,6 +263,7 @@ logger.endEC = function(res) } messageRes += newLine+newLine+"#====> Outgoing Message <====# "; + messageRes += newLine+"Session : "+req.id; messageRes += newLine+"Header : "+header; messageRes += newLine+"Body : "+body; messageRes += newLine+"Time out : "+endTime.toISOString(); @@ -285,7 +308,8 @@ logger.endEC = function(res) // ________________________________________________________________________________________________________________________ // `; - logger.log(messageRes); + logg.debug(messageRes); + // logger.log(messageRes); messageRes += newLine }; diff --git a/ais-structure/src/modules/main.route.js b/ais-structure/src/modules/main.route.js new file mode 100644 index 0000000..0e21d2b --- /dev/null +++ b/ais-structure/src/modules/main.route.js @@ -0,0 +1,114 @@ +var env = process.env.NODE_ENV || 'development'; +var responseMsg = require('../modules/helper/responseMsg.js'); +var log = require('../modules/helper/log.js'); +var constants = require('../modules/helper/constants.js'); + +var cfg = require('../config/config.js').get(env); +var load = require('express-load'); + + +module.exports = function (app) { + + + + app.all('/*', function (req, res,next) { + + + // console.log(req.params) + // console.log(req.query) + // console.log(req.url) + var splitPath = req._parsedUrl.pathname.split("/"); + //0 = ip host + //1 = 'cmf' + //2 = 'v1' + + var objectData = { + method:req.method, + req:req, + res:res, + next:next, + splitPath:splitPath, + pathname:req._parsedUrl.pathname + }; + + switch (splitPath[3]) { + case "customers": + if(splitPath[splitPath.length-1] == "membercards") + { + doMembercards(objectData); + } + else + { + objectData.modules = app.modules.customer; + doCustomers(objectData); + } + break; + case "vizcards": + doVizcard(objectData); + break; + default: next(); + break; + } + + + + + }); + + function doVizcard(objectData) + { + console.log("Route : "+objectData.pathname+" => doVizcard"); + objectData.next(); + } + + function doMembercards(objectData) + { + console.log("Route : "+objectData.pathname+" => doMembercards"); + objectData.next(); + + } + + function doCustomers(objectData) + { + console.log("Route : "+objectData.pathname+" => doCustomers"); + + // "/cmf/v1/customers/:customerId", + // "/cmf/v1/customers/:userType/:userData", + // "/cmf/v1/customers" + + var doAction = function(){ + + if(objectData.method == constants.METHOD.GET) + objectData.modules.customerCtrl.customer(objectData.req,objectData.res,objectData.next); + else + objectData.modules.postCustomerCtrl.customer(objectData.req,objectData.res,objectData.next); + }; + + switch (objectData.splitPath.length) { + case 4: + + doAction(); + break; + case 5: + objectData.req.params = { + customerId:objectData.splitPath[4] + }; + doAction(); + break; + case 6: + objectData.req.params = { + userType:objectData.splitPath[4], + userData:objectData.splitPath[5], + }; + doAction(); + break; + + default: + objectData.next(); + break; + } + } + + + +}; diff --git a/ais-structure/src/modules/vizcard/vizCard.ctrl.js b/ais-structure/src/modules/vizcard/vizCard.ctrl.js index 2a27226..77b4ab1 100644 --- a/ais-structure/src/modules/vizcard/vizCard.ctrl.js +++ b/ais-structure/src/modules/vizcard/vizCard.ctrl.js @@ -17,20 +17,20 @@ exports.vizcard = async function (req, res, next) { if(req.query.clientName && req.query.commandId){ var customerId = req.query.clientName+"@"+req.query.commandId; } - log.startlog(method+"_"+getCmd,req.query.commandId,customerId); - log.logDetail.addInput(req.query.clientName,method+"_"+getCmd,constant.REQUEST,req,req.body); + log.startlog(req,method+"_"+getCmd,req.query.commandId,customerId); + log.logDetail.addInput(req,req.query.clientName,method+"_"+getCmd,constant.REQUEST,req,req.body); var err = validator(req,getCmd); if(err.length > 0) { - log.addErrorSummary(req.query.clientName,method+"_"+getCmd,"null","Fail"); - log.log(err,'error'); + log.addErrorSummary(req,req.query.clientName,method+"_"+getCmd,"null","Fail"); + // log.log(err,'error'); var response = responseMsg.error(req,getCmd,40300); // res.status(200).json(response); }else { - log.addSuccessSummary(req.query.clientName,method+"_"+getCmd,"null","Success"); + log.addSuccessSummary(req,req.query.clientName,method+"_"+getCmd,"null","Success"); var objectData = { Username : "axviz", Password : "1234", @@ -57,13 +57,13 @@ exports.vizcard = async function (req, res, next) { stats.receiveSACFResponse(sacf,sendCmd,"Success"); var response = responseMsg.success(req,getCmd,{resultData:resultSet.returnData,rowCount:resultSet.returnData.length}); - log.addSuccessSummary(sacf,method+"_"+sendCmd,response.resultCode,response.developerMessage); + log.addSuccessSummary(req,sacf,method+"_"+sendCmd,response.resultCode,response.developerMessage); } catch(error) { log.log(error,'error') stats.receiveSACFBadResponse(sacf,sendCmd); response = responseMsg.error(req,getCmd,50000); - log.addErrorSummary(sacf,method+"_"+sendCmd,response.resultCode,response.developerMessage); + log.addErrorSummary(req,sacf,method+"_"+sendCmd,response.resultCode,response.developerMessage); var resultSet = {rawDataJson : ""} } @@ -74,11 +74,11 @@ exports.vizcard = async function (req, res, next) { var resultSet = {rawDataJson : ""} } - log.logDetail.addInput(sacf,method+"_"+sendCmd,constant.RESPONSE,result,resultSet.rawDataJson); + log.logDetail.addInput(req,sacf,method+"_"+sendCmd,constant.RESPONSE,result,resultSet.rawDataJson); } // console.log(response); - log.logDetail.addOutput(sacf,method+"_"+getCmd,constant.RESPONSE,response,response); + log.logDetail.addOutput(req,sacf,method+"_"+getCmd,constant.RESPONSE,response,response); res.status(200).json(response); next(); }; -- libgit2 0.21.2