Skip to content

Commit

Permalink
[MVC-Loader][Binary] Fixed a route-building bug for index controller …
Browse files Browse the repository at this point in the history
…(//action). Improved and extended the greppy binary.
  • Loading branch information
Jack12816 committed Aug 16, 2013
1 parent ecffcee commit a09c63a
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 43 deletions.
7 changes: 7 additions & 0 deletions bin/commands/list.js
Expand Up @@ -46,6 +46,13 @@ exports.run = function(opts)
context.name.bold.green,
context.description.white
]);

table.writeRow([
'modules'.white.bold,
context.modules.join(', ')
]);

console.log();
});
}

149 changes: 121 additions & 28 deletions bin/commands/start.js
Expand Up @@ -11,9 +11,9 @@ var colors = require('colors');
var daemon = require('daemon');
var Table = require('tab');

exports.run = function(opts)
exports.run = function(contexts, debug)
{
var contexts = opts.split(',');
contexts = require('../helpers/context').getContextsByArgs(contexts);
var appPath = path.normalize(process.cwd() + '/');

var table = new Table.TableOutputStream({
Expand All @@ -38,44 +38,137 @@ exports.run = function(opts)

startScript = appPath + 'app/' + startScript;

// Start all contexts with the found start script
contexts.forEach(function(context) {
if (!debug) {

var running = false;
var pidFile = appPath + 'var/run/' + context + '.pid';
// Start all contexts with the found start script
contexts.forEach(function(context) {

if (fs.existsSync(pidFile)) {
var contextState = helper.getContextState(context);

var existingPid = fs.readFileSync(pidFile, 'utf8');

if (fs.existsSync('/proc/' + existingPid + '/status')) {
running = true;
if (contextState.running) {
table.writeRow([
'start '.bold.green,
new String(context + ' -- already running (' + contextState.pid + ')').white
]);
return;
}
}

if (running) {
// Start the daemon process
var args = ['--context', context];
var stderr = fs.openSync(appPath + 'var/log/' + context + '.master.stderr.log', 'a');
var child = daemon.daemon(startScript, args, {
cwd : appPath,
stderr : stderr
});

// Write its pid
fs.writeFileSync(contextState.pidFile, child.pid);

table.writeRow([
'start '.bold.green,
new String(context + ' -- already running (' + existingPid + ')').white
new String(context + ' -- started (' + child.pid + ')').white
]);
return;
}
});

// Start the daemon process
var args = ['--context', context];
var stderr = fs.openSync(appPath + 'var/log/' + context + '.master.stderr.log', 'a');
var child = daemon.daemon(startScript, args, {
cwd : appPath,
stderr : stderr
return;
}

if (1 === contexts.length) {

process.stdin.resume();

var child = require('child_process').spawn(process.execPath, [
startScript, '--context', contexts[0]
], {
stdio: [process.stdin, 'pipe', 'pipe']
});

var print = function (data) {
console.log(data.toString().replace(/\n$/, ''));
};

child.stdout.on('data', print);
child.stderr.on('data', print);

child.on('close', function() {
process.stdin.pause();
});

// Write its pid
fs.writeFileSync(pidFile, child.pid);
return;
}

var screenConf = [
"# Greppy generated screen config",
"# Do not edit.",
"",
"# Custom screen config",
"startup_message off",
"msgminwait 0",
"defscrollback 50000",
"",
"# Initial tabs",
"{{tabs}}",
"sessionname {{projectName}}",
"",
"# Custom keybindings",
"bindkey -k kl prev",
"bindkey -k kr next",
"bindkey -k k9 quit",
"bindkey -k ku copy",
"",
"# Focus on the first window",
"select 1",
"",
"# Tabbar",
"# hardstatus alwayslastline",
"hardstatus string '%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<'"
].join('\n');

var tabs = [];
var projectName = require(process.cwd() + '/package').name;

// Start all contexts with the found start script
contexts.forEach(function(context) {

tabs.push(
'screen -t ' + context + ' ' + new String(tabs.length + 1)
+ ' greppy -s ' + context + ' -d'
);
});

table.writeRow([
'start '.bold.green,
new String(context + ' -- started (' + child.pid + ')').white
]);
screenConf = screenConf.replace('{{tabs}}', tabs.join('\n'))
.replace('{{projectName}}', projectName)
.replace('{{contextList}}', contexts.join(','));

var tmpScreenConfPath = '/tmp/.' + projectName + '.screenrc', screenConf;
fs.writeFileSync(tmpScreenConfPath, screenConf);

var screen = new (require('screen-init'))({
args: ['-c', tmpScreenConfPath]
});
}

var helper = {};

helper.getContextState = function(context)
{
var appPath = path.normalize(process.cwd() + '/');
var running = false;
var pidFile = appPath + 'var/run/' + context + '.pid';

if (fs.existsSync(pidFile)) {

var existingPid = fs.readFileSync(pidFile, 'utf8');

if (fs.existsSync('/proc/' + existingPid + '/status')) {
running = true;
}
}

return {
running : running,
pid : existingPid,
pidFile : pidFile
};
}

6 changes: 3 additions & 3 deletions bin/commands/stop.js
Expand Up @@ -11,10 +11,10 @@ var colors = require('colors');
var daemon = require('daemon');
var Table = require('tab');

exports.run = function(opts, callback)
exports.run = function(contexts, callback)
{
var contexts = opts.split(',');
var appPath = path.normalize(process.cwd() + '/');
contexts = require('../helpers/context').getContextsByArgs(contexts);
var appPath = path.normalize(process.cwd() + '/');

var table = new Table.TableOutputStream({
omitHeader: true,
Expand Down
21 changes: 11 additions & 10 deletions bin/greppy
Expand Up @@ -14,12 +14,13 @@ var childProcess = require('child_process');
var Table = require('tab');

var getopt = require('node-getopt').create([
['', 'new=APP_PATH', 'Create the application directory structure'],
['', 'start=ARG', 'Start the configured application ((non-)clustered)'],
['', 'stop=ARG', 'Stop the whole application or explicit modules'],
['', 'restart=ARG', 'Restart the whole application or explicit modules'],
['', 'status', 'Check the status of the whole application'],
['', 'list', 'List all application worker contexts'],
['n', 'new=APP_PATH', 'Create the application directory structure'],
['s', 'start', 'Start the configured application ((non-)clustered)'],
['k', 'stop', 'Stop the whole application or explicit modules'],
['r', 'restart', 'Restart the whole application or explicit modules'],
['m', 'status', 'Check the status of the whole application'],
['l', 'list', 'List all application worker contexts'],
['d', 'debug', 'Debug flag'],

['h', 'help', 'Display this help'],
['v', 'version', 'Show version']
Expand Down Expand Up @@ -54,22 +55,22 @@ if (opts.options.hasOwnProperty('new')) {
* start command
*/
if (opts.options.hasOwnProperty('start')) {
require('./commands/start').run(opts.options.start);
require('./commands/start').run(opts.argv, (opts.options.hasOwnProperty('debug')) ? true : false);
}

/**
* stop command
*/
if (opts.options.hasOwnProperty('stop')) {
require('./commands/stop').run(opts.options.stop);
require('./commands/stop').run(opts.argv);
}

/**
* restart command
*/
if (opts.options.hasOwnProperty('restart')) {
require('./commands/stop').run(opts.options.restart, function() {
require('./commands/start').run(opts.options.restart);
require('./commands/stop').run(opts.argv, function() {
require('./commands/start').run(opts.argv);
});
}

Expand Down
45 changes: 45 additions & 0 deletions bin/helpers/context.js
@@ -0,0 +1,45 @@
/**
* Context helper
*
* @module greppy/cli/helper/context
* @author Hermann Mayer <hermann.mayer92@gmail.com>
*/

var helper = {};
var path = require('path');
var fs = require('fs');

helper.getContextsByArgs = function(contexts)
{
var appPath = path.normalize(process.cwd() + '/');
var registeredContexts = [];

// Find all contexts
fs.readdirSync(appPath + 'app/context/').forEach(function(file) {

if (!file.match(/\.js$/gi)) {
return;
}

registeredContexts.push(path.basename(file, '.js'));
});

contexts.forEach(function(context) {

if (-1 === registeredContexts.indexOf(context)) {
console.log(new String('Context ' + context.green.bold + ' does not exist.').white);
console.log();
console.log(new String('Available contexts are: ' + registeredContexts.join(', ').green.bold).white);
process.exit(1);
}
});

if (0 === contexts.length) {
contexts = registeredContexts;
}

return contexts;
}

module.exports = helper;

6 changes: 5 additions & 1 deletion lib/http/mvc/loader.js
Expand Up @@ -72,7 +72,7 @@ ControllerLoader.prototype.loadController = function(controllerPath, controllerN

var basePath = (controller.options && controller.options.path)
? controller.options.path
: ('index' == controllerName) ? '/' : '/' + controllerName.replace(/\.js$/g, '');
: ('index' == controllerName) ? '' : '/' + controllerName.replace(/\.js$/g, '');

actions.forEach(function(actionName) {

Expand All @@ -88,6 +88,10 @@ ControllerLoader.prototype.loadController = function(controllerPath, controllerN
? action.path
: ('index' == actionName) ? '' : '/' + actionName;

if ('' == actionPath) {
actionPath = '/';
}

app[method.toLowerCase()](basePath + actionPath, action.action);

logger.debug(
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -34,7 +34,9 @@
"memory-cache": "~0.0.5",
"colors": "~0.6.0",
"tab": "~0.1.0",
"daemon": "~1.1.0"
"daemon": "~1.1.0",
"pty.js": "~0.2.3",
"screen-init": "~0.2.0"
},
"main": "./lib/greppy",
"bin": {
Expand Down

0 comments on commit a09c63a

Please sign in to comment.