Comparing version 0.1.0 to 0.2.0
@@ -22,3 +22,3 @@ require('colors'); | ||
function log(level, arguments) { | ||
function log(level, args) { | ||
if (level >= logLevel) { | ||
@@ -49,11 +49,11 @@ var color, | ||
} | ||
for (var key in arguments) { | ||
for (var key in args) { | ||
key = parseInt(key, 10); | ||
arguments[key + 1] = arguments[key]; | ||
arguments[0] = msg[color]; | ||
args[key + 1] = args[key]; | ||
args[0] = msg[color]; | ||
} | ||
arguments.length++; | ||
console.log.apply(null, arguments); | ||
args.length++; | ||
console.log.apply(null, args); | ||
} | ||
} | ||
}; |
149
overmind.js
@@ -0,23 +1,76 @@ | ||
/** | ||
* dependencies | ||
*/ | ||
var Server = require('./server'), | ||
logger = require('./logger')(10); | ||
Storage = require('./storage'), | ||
logger = require('./logger')(10), | ||
path = require('path'); | ||
/** | ||
* Default port | ||
* @const | ||
* @type {number} | ||
*/ | ||
var DEFAULT_PORT = 3000; | ||
/** | ||
* Default storages' path | ||
* @const | ||
* @type {string} | ||
*/ | ||
var DEFAULT_STORAGE_PATH = path.resolve(path.join(__dirname, './storage')); | ||
/** | ||
* @name Overmind | ||
* @constructor | ||
* @return {Overmind} | ||
*/ | ||
function Overmind() { | ||
var path = require('path'); | ||
this._servers = []; | ||
this._menuButtons = []; | ||
this.headerFile = path.join(__dirname, 'header.jade'); | ||
this._cache = {}; | ||
/** | ||
* logging level | ||
* levels : | ||
* 10 - trace, | ||
* 20 - debug, | ||
* 30 - info, | ||
* 40 - warn, | ||
* 50 - errors | ||
* @type {number} | ||
*/ | ||
this._logLevel = 10; | ||
this.logger = require('./logger')(this._logLevel); | ||
this._port = DEFAULT_PORT; | ||
this._storagePath = DEFAULT_STORAGE_PATH; | ||
return this; | ||
} | ||
/** | ||
* Start all servers | ||
* @return {Overmind} | ||
*/ | ||
Overmind.prototype.start = function() { | ||
var _this = this, | ||
server = require('express')(), | ||
evh = require('express-vhost'); | ||
server.use(evh.vhost()); | ||
server.listen(this._port); | ||
// creating new storage | ||
this.storage = new Storage('overmind', this._storagePath); | ||
// creating servers | ||
this.logger.trace('starting servers'); | ||
this._servers.forEach(function(server) { | ||
server.start(); | ||
_this.logger.info('starting server ' + server.name.underline); | ||
server.start(evh); | ||
_this.logger.info( | ||
server.name.underline + | ||
' can be reached by address '+ | ||
('http://' + server.hostname + ':' + _this.port()).underline.bold | ||
); | ||
}); | ||
this.logger = require('./logger')(this._logLevel); | ||
return this; | ||
}; | ||
@@ -35,6 +88,5 @@ | ||
* Declare server | ||
* @param {Function} logic (see server's constructor) | ||
* @param {Object|Undefined} params (see server's constructor) | ||
* @this {Overmind} | ||
* @return @this | ||
* @param {function(Server)} logic (see server's constructor) | ||
* @param {(Object|undefined)} params (see Server's constructor) | ||
* @return {Overmind} | ||
*/ | ||
@@ -48,5 +100,2 @@ Overmind.prototype.addServer = function(logic, params) { | ||
this._servers.push(server); | ||
if (server.global && server.global.menuButton) { | ||
this._menuButtons.push(server); | ||
} | ||
return this; | ||
@@ -57,12 +106,19 @@ }; | ||
* Generate header html | ||
* @param {Function} callback | ||
* @param {function( (Error|null), (string|undefined) )} callback | ||
* callback is called with error (if any) and html string | ||
*/ | ||
Overmind.prototype.getHeader = function(callback) { | ||
var servers = this.getServers(), | ||
cache = this._cache; | ||
var servers = this.getServers().filter(function (server) { | ||
if (server.inMenu) return server; | ||
}), | ||
cache = this._cache, | ||
_this = this; | ||
if (cache.header) | ||
this.logger.trace('getting header'); | ||
if (cache.header) { | ||
this.logger.trace('getting header from cache'); | ||
return callback(null, cache.header); | ||
} | ||
this.logger.info('generating new header'); | ||
require('fs').readFile(this.headerFile, function(err, data) { | ||
@@ -74,2 +130,3 @@ if (err) | ||
cache.header = html; | ||
_this.logger.trace('header compiled'); | ||
callback(null, html); | ||
@@ -80,13 +137,61 @@ }); | ||
/** | ||
* Overriding default port for servers | ||
* @param {number} port new port | ||
* @return {Overmind} | ||
*/ | ||
Overmind.prototype.port = function(port) { | ||
if (!port) | ||
return this._port; | ||
this._port = port; | ||
return this; | ||
}; | ||
/** | ||
* Overriding default path for servers's storage | ||
*/ | ||
Overmind.prototype.storagePath = function(path) { | ||
if (!arguments.length) | ||
return this._storagePath; | ||
// TODO check old storage and replace it | ||
this._storagePath = path; | ||
}; | ||
/** | ||
* Change logging level | ||
* @param {number} logLevel new log level value (10-50) | ||
* @return {Overmind} | ||
*/ | ||
Overmind.prototype.loglevel = function(logLevel) { | ||
/** | ||
* logging level | ||
* @type {Number} | ||
*/ | ||
if (!logLevel) | ||
return this.logLevel; | ||
this._logLevel = logLevel; | ||
this.logger = require('./logger')(this._logLevel); | ||
return this; | ||
}; | ||
module.exports = new Overmind; | ||
/** | ||
* Overmind options | ||
* @param {object} options port, logLevel, storagePath, etc | ||
* @return {Overmind} | ||
*/ | ||
Overmind.prototype.setOptions = function(options) { | ||
if (typeof options === 'object' && !(options instanceof Array)) { | ||
for (var key in options) { | ||
var opt = options[key]; | ||
if (this[key] && (typeof this[key] === 'function')) | ||
this[key](opt); | ||
else | ||
this.logger.error('no method found for key ' + key.underline); | ||
} | ||
} else { | ||
this.logger.error('setOptions\'s param should be object'); | ||
} | ||
return this; | ||
}; | ||
module.exports = new Overmind(); |
{ | ||
"name": "overmind", | ||
"description": "yet another nodejs framework", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Anatoliy Ostrovskiy", | ||
@@ -9,5 +9,10 @@ "dependencies": { | ||
"jade": "0.28.1", | ||
"colors": "~0.6.0-1" | ||
"colors": "~0.6.0-1", | ||
"nodejs-microdb": "0.0.2", | ||
"express-vhost": "~0.1.0", | ||
"mkdirp": "~0.3.5", | ||
"glob": "~3.2.6", | ||
"fs-extra": "~0.6.3" | ||
}, | ||
"main": "overmind.js" | ||
} |
109
server.js
@@ -1,3 +0,1 @@ | ||
var portIterator = 3000, | ||
usedPorts = []; | ||
@@ -8,22 +6,7 @@ function handleError(err) { | ||
function getPort() { | ||
portIterator++; | ||
if (usedPorts.indexOf(portIterator) > -1) | ||
return getPort(); | ||
usedPorts.push(portIterator); | ||
return portIterator; | ||
} | ||
/** | ||
* @name Server | ||
* @constructor | ||
* @param {Function} logic | ||
* @param {Object|Undefined} params (optional) | ||
* name {String} | ||
* path {String} | ||
* hostname {String|Undefined} | ||
* overmind {Object} - link to overmind | ||
* global - TODO | ||
* TODO check args | ||
* @param {function(Server)} logic | ||
* @param {({name: string, hostname: string, overmind: Object, inMenu: boolean}|undefined)} params | ||
* @api private | ||
@@ -36,3 +19,3 @@ */ | ||
* server's name | ||
* @type {String} | ||
* @type {string} | ||
*/ | ||
@@ -43,5 +26,3 @@ this.name = params.name; | ||
* server's hostname | ||
* @type {String} | ||
* for example: "test.localhost", "ololo.ololo.com" | ||
* TODO @type {Array} | ||
* @type {string} | ||
*/ | ||
@@ -52,3 +33,3 @@ this.hostname = params.hostname; | ||
* server's bussiness logic | ||
* @type {Function(server)} | ||
* @type {function(server)} | ||
*/ | ||
@@ -58,27 +39,20 @@ this.logic = logic; | ||
/** | ||
* global server's behavior | ||
* @type {Object} | ||
todo: | ||
menuButton: {boolean} | ||
* will be in menu or not | ||
* @type {boolean} | ||
*/ | ||
this.global = params.global; | ||
this.inMenu = (typeof params.inMenu !== 'boolean') || params.inMenu; | ||
/** | ||
* link to overmind | ||
* @type {Object} | ||
* @type {Overmind} | ||
*/ | ||
this.overmind = params.overmind; | ||
var port = params.port; | ||
if (port) { | ||
if (usedPorts.indexOf(port) > -1) { | ||
port = getPort(); | ||
} else { | ||
usedPorts.push(port); | ||
} | ||
} else { | ||
port = getPort(); | ||
} | ||
this._port = port; | ||
// TODO view path | ||
/** | ||
* alias for this.overmind.logger | ||
* @type {Object} | ||
*/ | ||
this.logger = this.overmind.logger; | ||
params.views && this.views(params.views); | ||
} | ||
@@ -88,5 +62,6 @@ | ||
* Starting server | ||
* @param {Vhost} evh - joins express's app with hostname | ||
* @api private | ||
*/ | ||
Server.prototype.start = function() { | ||
Server.prototype.start = function(evh) { | ||
if (this.__started) | ||
@@ -101,2 +76,4 @@ return; | ||
// @todo use this._storagePath | ||
// replace render fn | ||
@@ -114,3 +91,31 @@ view.prototype.render = function(options, fn) { | ||
} | ||
html = header + html; | ||
var match, | ||
i18nReg= /%{2}([a-zA-Z0-9]+)%{2}/g, | ||
placeholdersReg = /%{2}\$(.*)%{2}/g; | ||
// TODO i18n storage | ||
function translate(str) { | ||
_this.logger.debug('translating ' + str.underline); | ||
return str; | ||
} | ||
// TODO placeholder storage? | ||
function getPlaceholder(placeholder) { | ||
_this.logger.debug('replacing placeholder ' + placeholder.underline); | ||
switch (placeholder) { | ||
case 'header_menu': | ||
return header; | ||
default: | ||
return '<b>placeholder not found</b>'; | ||
} | ||
} | ||
while(match = i18nReg.exec(html)) { | ||
html = html.replace(new RegExp(match[0], 'g'), translate(match[1])); | ||
} | ||
while(match = placeholdersReg.exec(html)) { | ||
html = html.replace(match[0], getPlaceholder(match[1])); | ||
} | ||
fn(err, html); | ||
@@ -121,2 +126,3 @@ }); | ||
// rename express to app? | ||
this.express = express(); | ||
@@ -127,2 +133,3 @@ this.express.set('view', view); | ||
this.express.set('view engine', 'jade'); | ||
this.express.use(express.bodyParser()) | ||
@@ -132,4 +139,3 @@ // calling server's logic | ||
// creating http server | ||
require('http').createServer(this.express).listen(this._port); | ||
evh.register(this.hostname, this.express); | ||
}; | ||
@@ -139,3 +145,3 @@ | ||
* Declare server's view path | ||
* @param {String} path | ||
* @param {string} path path to views | ||
* @api public | ||
@@ -165,11 +171,2 @@ */ | ||
/** | ||
* Server's port | ||
* @return {number} port | ||
* @api public | ||
*/ | ||
Server.prototype.port = function() { | ||
return this._port; | ||
}; | ||
module.exports = Server; |
Sorry, the diff of this file is not supported yet
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
16720
11
491
0
60
8
3
+ Addedexpress-vhost@~0.1.0
+ Addedfs-extra@~0.6.3
+ Addedglob@~3.2.6
+ Addedmkdirp@~0.3.5
+ Addednodejs-microdb@0.0.2
+ Addedexpress-vhost@0.1.2(transitive)
+ Addedfs-extra@0.6.4(transitive)
+ Addedglob@3.2.11(transitive)
+ Addedjsonfile@1.0.1(transitive)
+ Addedlru-cache@2.7.3(transitive)
+ Addedminimatch@0.3.0(transitive)
+ Addedncp@0.4.2(transitive)
+ Addednodejs-microdb@0.0.2(transitive)
+ Addedrimraf@2.2.8(transitive)
+ Addedsigmund@1.0.1(transitive)