Last active
September 29, 2018 12:43
-
-
Save rjoydip-zz/aa7a52775386453010077f952fdc6ef1 to your computer and use it in GitHub Desktop.
My first little experimented micro application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const microo = require('../lib/microo'); | |
let microoApp = microo((err, res) => { | |
if (err) { throw err; } | |
console.log(res); | |
}); | |
/*****************************************************************/ | |
// Example 1 | |
// @example /cal/add?a=10&b=20 | |
microoApp.register({ | |
name: "cal", | |
actions: { | |
add: (params) => { | |
return { | |
output: parseInt(params.payload.a) + parseInt(params.payload.b) | |
}; | |
}, | |
sub: (params) => { | |
return { | |
output: parseInt(params.payload.a) - parseInt(params.payload.b) | |
}; | |
} | |
} | |
}); | |
/*****************************************************************/ | |
// Example 2 | |
// @example /test | |
// Example 2.1 | |
microoApp.register({ | |
name: "test", | |
actions: { | |
render : (params) => { | |
return (params) ? { name: 'foo with params',params: params.payload } : {name: 'foo without params'}; | |
} | |
} | |
}); | |
// Example 2.2 | |
microoApp.register({ | |
name: "test-rest", | |
actions: { | |
render : (params) => { | |
return (params) ? { name: 'foo with params',params: params.payload } : {name: 'foo without params'}; | |
}, | |
get: (params) => { | |
return params.payload; | |
// return "This is the GET method of test-post service"; | |
}, | |
post: (params) => { | |
return params.payload; | |
// return "This is the POST method of test-post service"; | |
}, | |
put: (params) => { | |
return params.payload; | |
// return "This is the PUT method of test-post service"; | |
}, | |
patch: (params) => { | |
return params.payload; | |
// return "This is the PATCH method of test-post service"; | |
}, | |
delete: (params) => { | |
return params.payload; | |
// return "This is the DELETE method of test-post service"; | |
} | |
} | |
}); | |
/*****************************************************************/ | |
// Example 3 | |
// @example /action-test | |
microoApp.register({ | |
name: "action-test", | |
actions: (params) => { | |
return { | |
name: 'action test', | |
url: (params.req) ? params.req.url : 'command action does not have req property' | |
}; | |
} | |
}); | |
/*****************************************************************/ | |
// Example 4 | |
// @example /action-param?foo=bar | |
microoApp.register({ | |
name: "action-param", | |
actions: (params) => { | |
return { | |
name: 'action with params '+ params.payload.foo | |
}; | |
} | |
}); | |
/*****************************************************************/ | |
// Example 5 | |
microoApp.register('mod',require('./mod')); | |
/*****************************************************************/ | |
// Example 6 | |
microoApp.register('/mod/foo',require('./mod').foo); | |
/*****************************************************************/ | |
// Example 7.1 | |
microoApp.action('cal.add',{a:21,b:11}, (err,res) => { | |
if(err) { console.log(err); } | |
// console.log("callback response for add : ",res); | |
}); | |
// Example 7.2 | |
microoApp.action('cal.sub',{a:21,b:11}, (err,res) => { | |
if(err) { console.log(err); } | |
// console.log("callback response for sub : ",res); | |
}); | |
/*****************************************************************/ | |
// Example 8 | |
microoApp.action('test',{}, (err,res) => { | |
if(err) { console.log(err); } | |
// console.log("callback response for test : ",res); | |
}); | |
/*****************************************************************/ | |
// Example 9 | |
microoApp.action('action-test',{}, (err,res) => { | |
if(err) { console.log(err); } | |
// console.log("callback response for test : ",res); | |
}); | |
/*****************************************************************/ | |
// Example 10.1 | |
microoApp.action('mod',{}, (err,res) => { | |
if(err) { console.log(err); } | |
// console.log("callback response for test : ",res); | |
}); | |
// Example 10.2 | |
microoApp.action('mod',{a:3}, (err,res) => { | |
if(err) { console.log(err); } | |
// console.log("callback response for test : ",res); | |
}); | |
/*****************************************************************/ | |
// Example 11 | |
microoApp.action('mod.foo',{a:10}, (err,res) => { | |
if(err) { console.log(err); } | |
// console.log("callback response for test : ",res); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const {URLSearchParams} = require('url'); | |
const http = require('http'); | |
const utils = require('./utils'); | |
const config = require('./config'); | |
let microo = () => { | |
/* | |
* Private variable initialization | |
*/ | |
let _config = null, | |
_apply = null, | |
_action = null, | |
_payload = null, | |
_handler = null, | |
__reqData = null, | |
_urlParse = null, | |
_register = null, | |
_cmdAction = null, | |
_serviceList = [], | |
callback = () => {}, | |
_defaultActionName = 'render', | |
_methods = config.methods; | |
// Default configuration object | |
_config = config; | |
_payload = utils.payload; | |
_urlParse = utils.urlParse; | |
/* | |
* Register service | |
* @api public | |
*/ | |
_register = (...args) => { | |
let SO = args; | |
(SO[0].name === undefined) ? _serviceList[SO[0]] = SO[1] : _serviceList[SO[0].name] = SO[0].actions; | |
return this; | |
}; | |
/* | |
* Command action | |
* @api public | |
*/ | |
_cmdAction = (...args) => { | |
let _args = args, | |
_argZero = _args[0], | |
callback = _args[2], | |
_payload = { | |
req: null, | |
res: null, | |
payload: _args[1], | |
}, | |
actionName = null, | |
serviceName = null, | |
_cmdArr = _argZero.split('.'); | |
serviceName = _cmdArr[0]; | |
actionName = _cmdArr[1]; | |
(serviceName) ? | |
(actionName !== undefined) ? | |
callback(null, _serviceList[serviceName][actionName].call(this, _payload)) | |
: (_serviceList[serviceName][_defaultActionName]) ? | |
callback(null, _serviceList[serviceName][_defaultActionName].call(this, _payload)) | |
: callback(null, _serviceList[serviceName].call(this, _payload)) | |
: callback(true,null); | |
}; | |
/* | |
* Setvice action | |
* @api private | |
*/ | |
_action = (_req, _res, callback) => { | |
let url = _req.url.split('/'), | |
params = {}, | |
serviceName, | |
actionName, | |
__urlParse, | |
__payload, | |
_modFlag = 0, | |
_reqMethod = _req.method, | |
_serviceAvailable = _methods.indexOf(_reqMethod); | |
// For example [/test] | |
(url[1]) ? ( | |
__urlParse = _urlParse(url[1]), | |
serviceName = __urlParse.actionName, | |
params = (_serviceAvailable) ? __reqData : __urlParse.params, | |
actionName = (_serviceAvailable > -1) ? | |
(_serviceList[serviceName][_reqMethod.toLowerCase()] !== undefined) ? | |
( | |
_modFlag = 2, | |
_reqMethod.toLowerCase() | |
) | |
: _defaultActionName | |
: _defaultActionName | |
) : false; | |
// For example [ /test/view, /cal/add?a=10&b=20 ] | |
(url[2]) ? ( | |
__urlParse = _urlParse(url[2]), | |
params = __urlParse.params, | |
actionName = __urlParse.actionName, | |
_modFlag = 0 | |
) : false; | |
// payload for pasing callback function's paramater | |
__payload = { payload: params, req: _req, res: _res}; | |
// calling corresponding services with paramater (optional) | |
// Note: If you pass any integer value in url | |
// i.e ?a=10&b=20 it return value in params as string not integer | |
(_modFlag === 0) ? | |
(actionName === 'actions') ? | |
callback(null, _serviceList[serviceName].call(this, __payload)) | |
: (actionName === _defaultActionName) ? | |
(_serviceList[serviceName] === undefined) ? | |
callback(true, null) | |
: callback(null, _serviceList[serviceName][_defaultActionName].call(this, __payload)) | |
: (_serviceList[serviceName] !== undefined) ? | |
callback(null, _serviceList[serviceName][actionName].call(this, __payload)) | |
: callback(true, null) | |
: (_modFlag === 2) ? | |
_req.on('data', function (data) { | |
__reqData = JSON.parse(JSON.stringify(new Buffer(data).toString())); | |
console.log(__reqData); | |
let _searchParams = new URLSearchParams(__reqData), | |
_searchParamsObj = {}; | |
for (const [name, value] of _searchParams) { | |
_searchParamsObj[name] = value; | |
} | |
__payload.payload = _searchParamsObj; | |
callback(null, _serviceList[serviceName][actionName].call(this, __payload)); | |
}) | |
: callback(null, null); | |
}; | |
/* | |
* Apply service action | |
* @link _action function | |
* @api private | |
*/ | |
_apply = (req, res) => { | |
(req.url !== '/favicon.ico') ? | |
(req.url === '/') ? ( | |
res.end(_payload(utils.response(null,req, res, {}))) | |
) | |
: ( | |
_action(req, res, (err, data) => { | |
(err) ? res.end(_payload(utils.response('err',req, res, data))) | |
: res.end(_payload(utils.response(null,req, res, data))) | |
}) | |
) | |
: false; | |
return this; | |
}; | |
/* | |
* App handler | |
* @link _apply function | |
* @api private | |
*/ | |
_handler = (req, res) => { | |
return _apply(req, res); | |
}; | |
/* | |
* App handler | |
* @link _apply function | |
* @example | |
* let faaSApp = faas(); | |
* @api public | |
*/ | |
return (...args) => { | |
let argList = args; // [].slice.call(arguments); | |
// create server | |
let server = http.createServer((req, res) => _handler(req, res)) | |
.listen(_config.gateway.port); | |
// console.log(server); | |
/* | |
* _config object parsing in faas function first parqamter | |
* @example | |
* let faaSApp = faas((err, res) => { | |
* if (err) throw err; | |
* console.log(res); | |
* }); | |
*/ | |
(typeof argList[0] === 'object') ? | |
(argList[0].config !== undefined && typeof argList[0].config === 'object') ? | |
_config = argList[0].config : {} | |
: true; | |
/* | |
* Server running status throught instance function | |
* @example | |
* let faaSApp = faas((err, res) => { | |
* if (err) throw err; | |
* console.log(res); | |
* }); | |
*/ | |
(typeof argList[argList.length - 1] === 'function') ? ( | |
callback = argList.pop(), | |
callback(null, "Server is running on port : " + _config.gateway.port) | |
) : true; | |
// publicly access api method initialization | |
this.action = _cmdAction; | |
this.register = _register; | |
return this; | |
}; | |
}; | |
exports = module.exports = microo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment