boilerplate-server
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -27,4 +27,4 @@ 'use strict'; | ||
'configure': function configure(application) { | ||
return regeneratorRuntime.async(function configure$(context$1$0) { | ||
'configure': function configureEngineHook(application) { | ||
return regeneratorRuntime.async(function configureEngineHook$(context$1$0) { | ||
while (1) switch (context$1$0.prev = context$1$0.next) { | ||
@@ -47,5 +47,5 @@ case 0: | ||
'start': function start(application) { | ||
'start': function startEngineHook(application) { | ||
var core, corePkgPath, pkgPath, corePkg, pkg, userPath, user; | ||
return regeneratorRuntime.async(function start$(context$1$0) { | ||
return regeneratorRuntime.async(function startEngineHook$(context$1$0) { | ||
while (1) switch (context$1$0.prev = context$1$0.next) { | ||
@@ -89,6 +89,6 @@ case 0: | ||
context$1$0.prev = 17; | ||
context$1$0.t8 = context$1$0['catch'](13); | ||
context$1$0.t0 = context$1$0['catch'](13); | ||
this.log.error('Error while reading user configuration from ' + userPath + '.'); | ||
this.log.error(context$1$0.t8); | ||
this.log.error(context$1$0.t0); | ||
@@ -107,5 +107,6 @@ throw new Error('Failed loading user configuration'); | ||
_lodash.merge(application.configuration, core, user, application.runtime.api); | ||
application.runtime.prefix = application.runtime.prefix || '/'; | ||
return context$1$0.abrupt('return', this); | ||
case 27: | ||
case 28: | ||
case 'end': | ||
@@ -112,0 +113,0 @@ return context$1$0.stop(); |
@@ -18,2 +18,6 @@ 'use strict'; | ||
var _koaRouter = require('koa-router'); | ||
var _koaRouter2 = _interopRequireDefault(_koaRouter); | ||
var _libraryUtilitiesPorts = require('../../../library/utilities/ports'); | ||
@@ -51,10 +55,16 @@ | ||
server = application.configuration.server; | ||
context$3$0.next = 7; | ||
if (application.router) { | ||
fuel.use(application.router.routes()); | ||
fuel.use(application.router.allowedMethods()); | ||
} | ||
context$3$0.next = 8; | ||
return _libraryUtilitiesPorts2['default'].test(port, host); | ||
case 7: | ||
context$3$0.t117 = context$3$0.sent; | ||
case 8: | ||
context$3$0.t1 = context$3$0.sent; | ||
if (!(context$3$0.t117 !== true)) { | ||
context$3$0.next = 15; | ||
if (!(context$3$0.t1 !== true)) { | ||
context$3$0.next = 17; | ||
break; | ||
@@ -64,3 +74,3 @@ } | ||
if (!(server.autoPort !== true)) { | ||
context$3$0.next = 11; | ||
context$3$0.next = 12; | ||
break; | ||
@@ -71,23 +81,36 @@ } | ||
case 11: | ||
case 12: | ||
application.log.warn('[application] Port ' + port + ' is taken, trying to obtain next open port... '); | ||
context$3$0.next = 14; | ||
context$3$0.next = 15; | ||
return _libraryUtilitiesPorts2['default'].find(server.port + 1, server.port + 51, server.host); | ||
case 14: | ||
case 15: | ||
server.port = context$3$0.sent; | ||
case 15: | ||
application.subs.forEach(function (sub) { | ||
application.log.info('[application:subapplication] Changing configuration of subapplications ' + sub.name); | ||
sub.mountable.configuration.server = server; | ||
sub.mountable.configuration.client = Object.assign(sub.mountable.configuration.client || {}, { | ||
host: server.host, | ||
port: server.port | ||
}); | ||
application.log.info('[application:subapplication] ' + sub.mountable.name + '.configuration.server: ' + JSON.stringify(sub.mountable.configuration.server)); | ||
application.log.info('[application:subapplication] ' + sub.mountable.name + '.configuration.client: ' + JSON.stringify(sub.mountable.configuration.client)); | ||
}); | ||
case 17: | ||
application.log.info('[application]', 'Starting server at http://' + server.host + ':' + server.port + ' in environment \'' + application.configuration.environment + '\' ...'); | ||
context$3$0.next = 18; | ||
context$3$0.next = 20; | ||
return fuel.listen(server.port, server.host); | ||
case 18: | ||
case 20: | ||
http = context$3$0.sent; | ||
return context$3$0.abrupt('return', application); | ||
case 20: | ||
case 22: | ||
case 'end': | ||
@@ -138,43 +161,42 @@ return context$3$0.stop(); | ||
var depth = path.split('/').length; | ||
var fragments = path.split('/'); | ||
var hostFragments = application.runtime.prefix.split('/'); | ||
var depth = fragments.length; | ||
if (mountable instanceof application.constructor !== true) { | ||
throw new TypeError('mountable is no BoilerPlateServer'); | ||
application.log.info('[application:subapplication] Mounting ' + mountable.name + ' on ' + path); | ||
if (path !== '/') { | ||
mountable.router.prefix(path); | ||
} else { | ||
mountable.router.stack.routes.forEach(function (route) { | ||
var match = application.router.route(route.name); | ||
if (match) { | ||
var index = application.router.stack.routes.indexOf(match); | ||
application.router.stack.routes.splice(index, 1); | ||
application.log.info('[applications:subapplication] Route "' + route.name + '" of "' + mountable.name + '" overwrites ' + application.name + '\'s route with same name.'); | ||
} | ||
}); | ||
application.router.stack.routes = application.router.stack.routes.concat(mountable.router.stack.routes); | ||
} | ||
application.router.add('GET', path + '/*', function callee$3$0(next) { | ||
var fragments, path, lookup, fn, args; | ||
return regeneratorRuntime.async(function callee$3$0$(context$4$0) { | ||
while (1) switch (context$4$0.prev = context$4$0.next) { | ||
case 0: | ||
fragments = this.path.split('/').filter(function (item, index) { | ||
return index >= depth; | ||
}); | ||
path = fragments.length > 0 ? fragments.join('/') : '/'; | ||
lookup = mountable.router.find('GET', path); | ||
fn = lookup[0]; | ||
args = lookup[1]; | ||
fuel.use(mountable.router.routes()); | ||
fuel.use(mountable.router.allowedMethods()); | ||
if (!(typeof fn === 'function')) { | ||
context$4$0.next = 12; | ||
break; | ||
} | ||
application.router.subs = application.router.subs || []; | ||
application.router.subs.push(mountable.router); | ||
fn = fn.bind(this); | ||
this.path = path; | ||
this.params = args; | ||
context$4$0.next = 11; | ||
return fn(next); | ||
mountable.runtime.prefix = '/' + fragments.concat(hostFragments).filter(function (item) { | ||
return item; | ||
}).join('/'); | ||
case 11: | ||
return context$4$0.abrupt('return', context$4$0.sent); | ||
application.subs.push({ path: path, mountable: mountable }); | ||
case 12: | ||
case 'end': | ||
return context$4$0.stop(); | ||
} | ||
}, null, this); | ||
}); | ||
mountable.configuration.server = Object.assign({}, mountable.configuration.server, application.configuration.server); | ||
mountable.configuration.client = Object.assign({}, mountable.configuration.client, application.configuration.server); | ||
application.log.info('[application:subapplication] Mounting ' + mountable.name + ' on ' + path); | ||
application.log.info('[application:subapplication] Changing configuration of subapplications ' + mountable.name); | ||
application.log.info('[application:subapplication] ' + mountable.name + '.configuration.server: ' + JSON.stringify(mountable.configuration.server)); | ||
application.log.info('[application:subapplication] ' + mountable.name + '.configuration.client: ' + JSON.stringify(mountable.configuration.client)); | ||
return application; | ||
@@ -181,0 +203,0 @@ } |
@@ -16,3 +16,3 @@ 'use strict'; | ||
start: function startEngineHook(application) { | ||
'start': function startEngineHook(application) { | ||
return regeneratorRuntime.async(function startEngineHook$(context$1$0) { | ||
@@ -22,3 +22,2 @@ while (1) switch (context$1$0.prev = context$1$0.next) { | ||
application.engine = _engine2['default'](application); | ||
return context$1$0.abrupt('return', this); | ||
@@ -33,25 +32,2 @@ | ||
}; | ||
module.exports = exports['default']; | ||
/*application.engine = engine( application ); | ||
engine.koa.experimental = true; | ||
let server = application.configuration.server; | ||
engine.use = function engineUse ( ...args ) { | ||
return application.engine.koa.use( ...args ); | ||
}; | ||
engine.listen = async function engineListen ( ...args ) { | ||
return application.engine.koa.listen( ...args ); | ||
}; | ||
engine.start = async function engineStart ( port, host ) { | ||
if ( await ports.test( port, host ) === 'open' ) { | ||
if ( server.autoPort !== true ) { | ||
throw new Error( `Port ${port} is taken and server.autPort is disabled, could not start server.` ); | ||
} | ||
application.log.warn( `[application] Port ${port} is taken, trying to obtain next open port... ` ); | ||
server.port = await ports.find( server.port + 1, server.port + 51, server.host ); | ||
} | ||
application.log.info( '[application]', `Starting server at http://${server.host}:${server.port} in environment '${application.configuration.environment}' ...` ); | ||
await application.engine.listen( server.port, server.host ); | ||
return application; | ||
}; | ||
application.engine.stop = function engineStop () { | ||
};*/ | ||
module.exports = exports['default']; |
@@ -11,5 +11,5 @@ 'use strict'; | ||
var _trekRouter = require('trek-router'); | ||
var _koaRouter = require('koa-router'); | ||
var _trekRouter2 = _interopRequireDefault(_trekRouter); | ||
var _koaRouter2 = _interopRequireDefault(_koaRouter); | ||
@@ -32,3 +32,3 @@ var _requireAll = require('require-all'); | ||
case 0: | ||
application.router = new _trekRouter2['default'](); | ||
application.router = _koaRouter2['default'](); | ||
@@ -87,36 +87,38 @@ coreRoutes = _requireAll2['default'](_path.resolve(application.runtime.base, application.configuration.paths.routes)); | ||
var method = routeConfig.method || 'GET'; | ||
var methods = routeConfig.methods || ['GET', 'POST', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']; | ||
var fn = routeFactoryFunction(application, routeConfig); | ||
// Register routes with router | ||
application.router.add(method, routeConfig.path, routeFactoryFunction(application, routeConfig)); | ||
}); | ||
if (typeof fn !== 'function') { | ||
_this.log.info('' + routeName + ' factory returned no valid route for ' + routeConfig.path); | ||
return; | ||
} | ||
// Register router middleware | ||
application.engine.use(function routerMiddleware(next) { | ||
var lookup, fn, args; | ||
return regeneratorRuntime.async(function routerMiddleware$(context$2$0) { | ||
while (1) switch (context$2$0.prev = context$2$0.next) { | ||
case 0: | ||
lookup = application.router.find(this.request.method, this.request.url); | ||
fn = lookup[0]; | ||
args = lookup[1]; | ||
_this.log.info('Mounting ' + routeName + ' on ' + routeConfig.path); | ||
if (!(typeof fn === 'function')) { | ||
context$2$0.next = 9; | ||
application.router.register(routeName, routeConfig.path, methods, regeneratorRuntime.mark(function callee$2$0(next) { | ||
return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) { | ||
while (1) switch (context$3$0.prev = context$3$0.next) { | ||
case 0: | ||
context$3$0.prev = 0; | ||
context$3$0.next = 3; | ||
return fn.bind(this)(next); | ||
case 3: | ||
context$3$0.next = 10; | ||
break; | ||
} | ||
fn = fn.bind(this); | ||
this.params = args; | ||
context$2$0.next = 8; | ||
return fn(next); | ||
case 5: | ||
context$3$0.prev = 5; | ||
context$3$0.t2 = context$3$0['catch'](0); | ||
case 8: | ||
return context$2$0.abrupt('return', context$2$0.sent); | ||
application.log.error('Error while executing route ' + routeName); | ||
application.log.error(context$3$0.t2.stack); | ||
throw context$3$0.t2; | ||
case 9: | ||
case 'end': | ||
return context$2$0.stop(); | ||
} | ||
}, null, this); | ||
case 10: | ||
case 'end': | ||
return context$3$0.stop(); | ||
} | ||
}, callee$2$0, this, [[0, 5]]); | ||
})); | ||
}); | ||
@@ -126,3 +128,3 @@ | ||
case 13: | ||
case 12: | ||
case 'end': | ||
@@ -129,0 +131,0 @@ return context$1$0.stop(); |
@@ -11,5 +11,5 @@ 'use strict'; | ||
var _load = require('../load'); | ||
var _libraryHooksLoad = require('../../../library/hooks/load'); | ||
var _load2 = _interopRequireDefault(_load); | ||
var _libraryHooksLoad2 = _interopRequireDefault(_libraryHooksLoad); | ||
@@ -51,3 +51,3 @@ var _libraryUtilitiesFs = require('../../../library/utilities/fs'); | ||
case 8: | ||
hooks = _load2['default'](application, this.configuration.path, true); | ||
hooks = _libraryHooksLoad2['default'](application, this.configuration.path, true); | ||
@@ -54,0 +54,0 @@ hooks = hooks.map(function (hook) { |
@@ -8,4 +8,4 @@ 'use strict'; | ||
function indexRouteFactory(application) { | ||
return function indexRoute(path) { | ||
function indexRouteFactory() { | ||
return function indexRoute() { | ||
return regeneratorRuntime.async(function indexRoute$(context$2$0) { | ||
@@ -12,0 +12,0 @@ while (1) switch (context$2$0.prev = context$2$0.next) { |
@@ -36,3 +36,3 @@ 'use strict'; | ||
case 2: | ||
path = this.params[0].value; | ||
path = this.captures[0]; | ||
@@ -39,0 +39,0 @@ path = path[0] === '/' ? path.slice(1) : path; |
@@ -22,3 +22,3 @@ #!/usr/bin/env node --harmony | ||
var options = arguments[0] === undefined ? {} : arguments[0]; | ||
var augmented, application, test, log, stop; | ||
var application, log, stop; | ||
return regeneratorRuntime.async(function start$(context$1$0) { | ||
@@ -42,5 +42,5 @@ while (1) switch (context$1$0.prev = context$1$0.next) { | ||
context$2$0.prev = 6; | ||
context$2$0.t112 = context$2$0['catch'](0); | ||
context$2$0.t5 = context$2$0['catch'](0); | ||
application.log.error(context$2$0.t112); | ||
application.log.error(context$2$0.t5); | ||
process.exit(1); | ||
@@ -55,46 +55,35 @@ | ||
augmented = Object.assign({}, { | ||
'cwd': process.cwd(), | ||
'base': _path.resolve(__dirname, '../'), | ||
'env': process.env.NODE_ENV || 'development' }, options, { 'api': options }); | ||
application = undefined; | ||
context$1$0.prev = 3; | ||
context$1$0.next = 6; | ||
return _2['default'](augmented); | ||
context$1$0.prev = 2; | ||
context$1$0.next = 5; | ||
return _2['default'](options); | ||
case 6: | ||
case 5: | ||
application = context$1$0.sent; | ||
context$1$0.next = 9; | ||
return _2['default'](Object.assign({}, augmented, { 'name': 'test' })); | ||
context$1$0.next = 13; | ||
break; | ||
case 9: | ||
test = context$1$0.sent; | ||
case 8: | ||
context$1$0.prev = 8; | ||
context$1$0.t6 = context$1$0['catch'](2); | ||
log = application ? application.log || console : console; | ||
application.mount(test, '/test'); | ||
context$1$0.next = 18; | ||
break; | ||
log.trace(context$1$0.t6); | ||
throw new Error(context$1$0.t6); | ||
case 13: | ||
context$1$0.prev = 13; | ||
context$1$0.t113 = context$1$0['catch'](3); | ||
log = application ? application.log || console : console; | ||
log.trace(context$1$0.t113); | ||
throw new Error(context$1$0.t113); | ||
case 18: | ||
context$1$0.prev = 18; | ||
application.start(); | ||
context$1$0.next = 26; | ||
context$1$0.next = 21; | ||
break; | ||
case 22: | ||
context$1$0.prev = 22; | ||
context$1$0.t114 = context$1$0['catch'](18); | ||
case 17: | ||
context$1$0.prev = 17; | ||
context$1$0.t7 = context$1$0['catch'](13); | ||
application.log.error(context$1$0.t114); | ||
throw new Error(context$1$0.t114); | ||
application.log.error(context$1$0.t7); | ||
throw new Error(context$1$0.t7); | ||
case 26: | ||
case 21: | ||
@@ -117,9 +106,9 @@ process.on('SIGINT', function () { | ||
case 31: | ||
case 26: | ||
case 'end': | ||
return context$1$0.stop(); | ||
} | ||
}, null, this, [[3, 13], [18, 22]]); | ||
}, null, this, [[2, 8], [13, 17]]); | ||
} | ||
start(_minimist2['default'](process.argv.slice(1))); |
@@ -7,3 +7,3 @@ 'use strict'; | ||
exports['default'] = { | ||
'level': 'info', | ||
'level': 'silly', | ||
'transports': ['console', 'file'], | ||
@@ -10,0 +10,0 @@ 'options': { |
@@ -17,3 +17,3 @@ 'use strict'; | ||
'method': 'GET', | ||
'path': '/static/*', | ||
'path': /\/static\/(.*)/, | ||
'options': { | ||
@@ -20,0 +20,0 @@ 'root': './static', |
@@ -25,5 +25,5 @@ 'use strict'; | ||
var _applicationHooks = require('../../application/hooks'); | ||
var _hooks = require('../hooks'); | ||
var _applicationHooks2 = _interopRequireDefault(_applicationHooks); | ||
var _hooks2 = _interopRequireDefault(_hooks); | ||
@@ -37,4 +37,6 @@ var BoilerPlateServer = (function (_EventEmitter) { | ||
this.name = options.name; | ||
this.subs = options.subs || []; | ||
this.runtime = options; | ||
this.log = _logger2['default'](options); | ||
this.log = _logger2['default'](options, this); | ||
} | ||
@@ -83,3 +85,3 @@ | ||
context$1$0.next = 3; | ||
return _applicationHooks2['default'](application); | ||
return _hooks2['default'](application); | ||
@@ -86,0 +88,0 @@ case 3: |
@@ -13,3 +13,3 @@ 'use strict'; | ||
function bootLogger(options) { | ||
function bootLogger(options, application) { | ||
var ConsoleTransport = _winston.transports.Console; | ||
@@ -20,3 +20,3 @@ var FileTransport = _winston.transports.File; | ||
return new _winston.Logger({ | ||
var log = new _winston.Logger({ | ||
'transports': [new ConsoleTransport({ | ||
@@ -37,2 +37,46 @@ 'name': 'bootConsole', | ||
}); | ||
var logger = {}; | ||
logger.error = function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return log.error.apply(log, ['[' + application.name + ']'].concat(args)); | ||
}; | ||
logger.warn = function () { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return log.warn.apply(log, ['[' + application.name + ']'].concat(args)); | ||
}; | ||
logger.info = function () { | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
} | ||
return log.info.apply(log, ['[' + application.name + ']'].concat(args)); | ||
}; | ||
logger.debug = function () { | ||
for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { | ||
args[_key4] = arguments[_key4]; | ||
} | ||
return log.debug.apply(log, ['[' + application.name + ']'].concat(args)); | ||
}; | ||
logger.silly = function () { | ||
for (var _len5 = arguments.length, args = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { | ||
args[_key5] = arguments[_key5]; | ||
} | ||
return log.silly.apply(log, ['[' + application.name + ']'].concat(args)); | ||
}; | ||
return logger; | ||
} | ||
@@ -39,0 +83,0 @@ |
@@ -9,2 +9,4 @@ 'use strict'; | ||
var _path = require('path'); | ||
var _boot = require('./boot'); | ||
@@ -16,12 +18,19 @@ | ||
var options = arguments[0] === undefined ? {} : arguments[0]; | ||
var augmented; | ||
return regeneratorRuntime.async(function boilerplate$(context$1$0) { | ||
while (1) switch (context$1$0.prev = context$1$0.next) { | ||
case 0: | ||
context$1$0.next = 2; | ||
return _boot2['default'](options); | ||
augmented = Object.assign({}, { | ||
'cwd': process.cwd(), | ||
'base': options.base || _path.resolve(__dirname, '../'), | ||
'env': process.env.NODE_ENV || 'development', | ||
'name': options.name || 'boilerplate-server' | ||
}, options, { 'api': options }); | ||
context$1$0.next = 3; | ||
return _boot2['default'](augmented); | ||
case 2: | ||
case 3: | ||
return context$1$0.abrupt('return', context$1$0.sent); | ||
case 3: | ||
case 4: | ||
case 'end': | ||
@@ -34,2 +43,4 @@ return context$1$0.stop(); | ||
exports['default'] = boilerplate; | ||
module.exports = exports['default']; | ||
module.exports = exports['default']; | ||
/*eslint-disable no-process-env */ |
{ | ||
"name": "boilerplate-server", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Lean and mean server module intended to get you kicking on the edge of things real fast.", | ||
@@ -10,6 +10,6 @@ "main": "library/", | ||
"watch": "npm run babel -- --watch", | ||
"babel": "babel source --out-dir ./" | ||
"babel": "npm run clean && babel source --out-dir ./", | ||
"clean": "rm -rf application binary configuration library" | ||
}, | ||
"bin": { | ||
"_boilerplate-server": "binary/_boilerplate-server.js", | ||
"boilerplate-server": "binary/boilerplate-server.js" | ||
@@ -47,2 +47,3 @@ }, | ||
"koa": "^0.19.1", | ||
"koa-router": "^4.3.2", | ||
"lodash": "^3.7.0", | ||
@@ -55,3 +56,2 @@ "minimist": "^1.1.1", | ||
"resolve-path": "^1.2.2", | ||
"trek-router": "^0.1.9", | ||
"winston": "^1.0.0" | ||
@@ -58,0 +58,0 @@ }, |
@@ -15,3 +15,3 @@ import { resolve } from 'path'; | ||
'configure': async function ( application ) { | ||
'configure': async function configureEngineHook ( application ) { | ||
application.configuration = {}; | ||
@@ -26,3 +26,3 @@ | ||
'start': async function ( application ) { | ||
'start': async function startEngineHook ( application ) { | ||
// Load core configuration | ||
@@ -68,4 +68,5 @@ let core = load( this.configuration.path, this.configuration.filter, application.runtime.env ); | ||
merge( application.configuration, core, user, application.runtime.api ); | ||
application.runtime.prefix = application.runtime.prefix || '/'; | ||
return this; | ||
} | ||
}; |
import koa from 'koa'; | ||
import router from 'koa-router'; | ||
@@ -22,2 +23,7 @@ import ports from '../../../library/utilities/ports'; | ||
if ( application.router ) { | ||
fuel.use(application.router.routes()); | ||
fuel.use(application.router.allowedMethods()); | ||
} | ||
if ( await ports.test( port, host ) !== true ) { | ||
@@ -30,2 +36,15 @@ if ( server.autoPort !== true ) { | ||
server.port = await ports.find( server.port + 1, server.port + 51, server.host ); | ||
application.subs.forEach(function(sub){ | ||
application.log.info( `[application:subapplication] Changing configuration of subapplications ${sub.name}` ); | ||
sub.mountable.configuration.server = server; | ||
sub.mountable.configuration.client = Object.assign(sub.mountable.configuration.client || {}, { | ||
host: server.host, | ||
port: server.port | ||
}); | ||
application.log.info( `[application:subapplication] ${sub.mountable.name}.configuration.server: ${JSON.stringify(sub.mountable.configuration.server)}` ); | ||
application.log.info( `[application:subapplication] ${sub.mountable.name}.configuration.client: ${JSON.stringify(sub.mountable.configuration.client)}` ); | ||
}); | ||
} | ||
@@ -54,25 +73,43 @@ | ||
let { fuel, application } = nameSpace.get(this); | ||
let depth = path.split('/').length; | ||
let fragments = path.split('/'); | ||
let hostFragments = application.runtime.prefix.split('/'); | ||
let depth = fragments.length; | ||
if ( (mountable instanceof application.constructor) !== true ) { | ||
throw new TypeError('mountable is no BoilerPlateServer'); | ||
application.log.info( `[application:subapplication] Mounting ${mountable.name} on ${path}` ); | ||
if (path !== '/') { | ||
mountable.router.prefix(path); | ||
} else { | ||
mountable.router.stack.routes.forEach(function(route){ | ||
let match = application.router.route(route.name); | ||
if (match) { | ||
let index = application.router.stack.routes.indexOf(match); | ||
application.router.stack.routes.splice(index, 1); | ||
application.log.info(`[applications:subapplication] Route "${route.name}" of "${mountable.name}" overwrites ${application.name}'s route with same name.`) | ||
} | ||
}); | ||
application.router.stack.routes = application.router.stack.routes.concat(mountable.router.stack.routes); | ||
} | ||
application.router.add('GET', path + '/*', async function( next ) { | ||
let fragments = this.path.split('/').filter((item, index) => index >= depth ); | ||
let path = fragments.length > 0 ? fragments.join('/') : '/'; | ||
let lookup = mountable.router.find('GET', path); | ||
fuel.use(mountable.router.routes()); | ||
fuel.use(mountable.router.allowedMethods()); | ||
let fn = lookup[ 0 ]; | ||
let args = lookup[ 1 ]; | ||
application.router.subs = application.router.subs || []; | ||
application.router.subs.push(mountable.router); | ||
if ( typeof fn === 'function' ) { | ||
fn = fn.bind( this ); | ||
this.path = path; | ||
this.params = args; | ||
return await fn( next ); | ||
} | ||
}); | ||
mountable.runtime.prefix = '/' + fragments | ||
.concat(hostFragments) | ||
.filter((item) => item) | ||
.join('/'); | ||
application.log.info( `[application:subapplication] Mounting ${mountable.name} on ${path}` ); | ||
application.subs.push({ path, mountable }); | ||
mountable.configuration.server = Object.assign({}, mountable.configuration.server, application.configuration.server); | ||
mountable.configuration.client = Object.assign({}, mountable.configuration.client, application.configuration.server); | ||
application.log.info( `[application:subapplication] Changing configuration of subapplications ${mountable.name}` ); | ||
application.log.info( `[application:subapplication] ${mountable.name}.configuration.server: ${JSON.stringify(mountable.configuration.server)}` ); | ||
application.log.info( `[application:subapplication] ${mountable.name}.configuration.client: ${JSON.stringify(mountable.configuration.client)}` ); | ||
return application; | ||
@@ -79,0 +116,0 @@ } |
@@ -6,41 +6,6 @@ import engine from './engine'; | ||
start: async function startEngineHook ( application ) { | ||
'start': async function startEngineHook ( application ) { | ||
application.engine = engine( application ); | ||
/*application.engine = engine( application ); | ||
engine.koa.experimental = true; | ||
let server = application.configuration.server; | ||
engine.use = function engineUse ( ...args ) { | ||
return application.engine.koa.use( ...args ); | ||
}; | ||
engine.listen = async function engineListen ( ...args ) { | ||
return application.engine.koa.listen( ...args ); | ||
}; | ||
engine.start = async function engineStart ( port, host ) { | ||
if ( await ports.test( port, host ) === 'open' ) { | ||
if ( server.autoPort !== true ) { | ||
throw new Error( `Port ${port} is taken and server.autPort is disabled, could not start server.` ); | ||
} | ||
application.log.warn( `[application] Port ${port} is taken, trying to obtain next open port... ` ); | ||
server.port = await ports.find( server.port + 1, server.port + 51, server.host ); | ||
} | ||
application.log.info( '[application]', `Starting server at http://${server.host}:${server.port} in environment '${application.configuration.environment}' ...` ); | ||
await application.engine.listen( server.port, server.host ); | ||
return application; | ||
}; | ||
application.engine.stop = function engineStop () { | ||
};*/ | ||
return this; | ||
} | ||
}; |
import { resolve } from 'path'; | ||
import Router from 'trek-router'; | ||
import router from 'koa-router'; | ||
import requireAll from 'require-all'; | ||
@@ -12,3 +12,3 @@ | ||
'start': async function startRoutesHook ( application ) { | ||
application.router = new Router(); | ||
application.router = router(); | ||
@@ -65,21 +65,23 @@ // load physical core routes | ||
let method = routeConfig.method || 'GET'; | ||
// Register routes with router | ||
application.router.add( method, routeConfig.path, routeFactoryFunction( application, routeConfig ) ); | ||
} ); | ||
let methods = routeConfig.methods || [ 'GET', 'POST', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS' ]; | ||
let fn = routeFactoryFunction( application, routeConfig ); | ||
// Register router middleware | ||
application.engine.use( async function routerMiddleware ( next ) { | ||
if ( typeof fn !== 'function' ) { | ||
this.log.info( `${routeName} factory returned no valid route for ${routeConfig.path}` ); | ||
return; | ||
} | ||
let lookup = application.router.find( this.request.method, this.request.url ); | ||
this.log.info( `Mounting ${routeName} on ${routeConfig.path}` ); | ||
let fn = lookup[ 0 ]; | ||
let args = lookup[ 1 ]; | ||
application.router.register( routeName, routeConfig.path, methods, function * ( next ) { | ||
try { | ||
yield fn.bind( this )( next ); | ||
} catch ( error ) { | ||
application.log.error( `Error while executing route ${routeName}` ); | ||
application.log.error( error.stack ); | ||
throw error; | ||
} | ||
} ); | ||
if ( typeof fn === 'function' ) { | ||
fn = fn.bind( this ); | ||
this.params = args; | ||
return await fn( next ); | ||
} | ||
} ); | ||
@@ -86,0 +88,0 @@ |
import { resolve } from 'path'; | ||
import load from '../load'; | ||
import load from '../../../library/hooks/load'; | ||
import { exists } from '../../../library/utilities/fs'; | ||
@@ -5,0 +5,0 @@ |
@@ -1,5 +0,5 @@ | ||
export default function indexRouteFactory( application ) { | ||
return async function indexRoute ( path ) { | ||
export default function indexRouteFactory () { | ||
return async function indexRoute () { | ||
this.body = 'You are up and running! Place a custom index route in ./application/routes.'; | ||
}; | ||
} |
@@ -14,4 +14,3 @@ import { resolve, basename, extname } from 'path'; | ||
let path = this.params[ 0 ].value; | ||
let path = this.captures[0]; | ||
path = path[ 0 ] === '/' ? path.slice( 1 ) : path; | ||
@@ -18,0 +17,0 @@ |
@@ -11,14 +11,6 @@ #!/usr/bin/env node --harmony | ||
async function start ( options = {} ) { | ||
let augmented = Object.assign( {}, { | ||
'cwd': process.cwd(), | ||
'base': resolve( __dirname, '../' ), | ||
'env': process.env.NODE_ENV || 'development', | ||
}, options, { 'api': options } ); | ||
let application; | ||
try { | ||
application = await boilerplate( augmented ); | ||
let test = await boilerplate( Object.assign( {}, augmented, { 'name': 'test' } ) ); | ||
application.mount( test, '/test' ); | ||
application = await boilerplate( options ); | ||
} catch ( error ) { | ||
@@ -25,0 +17,0 @@ let log = application ? application.log || console : console; |
export default { | ||
'level': 'info', | ||
'level': 'silly', | ||
'transports': [ 'console', 'file' ], | ||
@@ -4,0 +4,0 @@ 'options': { |
@@ -12,3 +12,3 @@ const routes = { | ||
'method': 'GET', | ||
'path': '/static/*', | ||
'path': /\/static\/(.*)/, | ||
'options': { | ||
@@ -15,0 +15,0 @@ 'root': './static', |
import { EventEmitter } from 'events'; | ||
import bootLogger from './logger'; | ||
import hooks from '../../application/hooks'; | ||
import hooks from '../hooks'; | ||
@@ -11,4 +11,6 @@ class BoilerPlateServer extends EventEmitter { | ||
this.name = options.name; | ||
this.subs = options.subs || []; | ||
this.runtime = options; | ||
this.log = bootLogger( options ); | ||
this.log = bootLogger( options, this ); | ||
} | ||
@@ -15,0 +17,0 @@ |
@@ -8,3 +8,3 @@ /* | ||
function bootLogger ( options ) { | ||
function bootLogger ( options, application ) { | ||
let ConsoleTransport = transports.Console; | ||
@@ -15,3 +15,3 @@ let FileTransport = transports.File; | ||
return new Logger( { | ||
let log = new Logger( { | ||
'transports': [ | ||
@@ -35,4 +35,28 @@ new ConsoleTransport( { | ||
} ); | ||
let logger = {}; | ||
logger.error = function (...args) { | ||
return log.error( ...[ `[${application.name}]`, ...args ] ); | ||
}; | ||
logger.warn = function (...args) { | ||
return log.warn( ...[ `[${application.name}]`, ...args ] ); | ||
}; | ||
logger.info = function (...args) { | ||
return log.info( ...[ `[${application.name}]`, ...args ] ); | ||
}; | ||
logger.debug = function (...args) { | ||
return log.debug( ...[ `[${application.name}]`, ...args ] ); | ||
}; | ||
logger.silly = function (...args) { | ||
return log.silly( ...[ `[${application.name}]`, ...args ] ); | ||
}; | ||
return logger; | ||
} | ||
export default bootLogger; |
@@ -0,7 +1,18 @@ | ||
import { resolve } from 'path'; | ||
import boot from './boot'; | ||
async function boilerplate ( options = {} ) { | ||
return await boot( options ); | ||
/*eslint-disable no-process-env */ | ||
let augmented = Object.assign( {}, { | ||
'cwd': process.cwd(), | ||
'base': options.base || resolve( __dirname, '../' ), | ||
'env': process.env.NODE_ENV || 'development', | ||
'name': options.name || 'boilerplate-server' | ||
}, options, { 'api': options } ); | ||
return await boot( augmented ); | ||
} | ||
export default boilerplate; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
99770
2525
16
0
74
+ Addedkoa-router@^4.3.2
+ Addedconcat-regexp@0.0.6(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedkoa-router@4.3.2(transitive)
+ Addedmethods@1.1.2(transitive)
+ Addedpath-to-regexp@1.9.0(transitive)
- Removedtrek-router@^0.1.9
- Removedtrek-router@0.1.9(transitive)