diff --git a/core/modules/commander.js b/core/modules/commander.js index ad7f4b5b3..fe796fd7c 100644 --- a/core/modules/commander.js +++ b/core/modules/commander.js @@ -94,6 +94,13 @@ Commander.prototype.executeNextCommand = function() { if(this.verbose) { this.streams.output.write("Executing command: " + commandName + " " + params.join(" ") + "\n"); } + // Parse named parameters if required + if(command.info.namedParameters) { + params = this.extractNamedParameters(params,command.info.namedParameters); + if(typeof params === "string") { + return this.callback(params); + } + } if(command.info.synchronous) { // Synchronous command c = new command.Command(params,this); @@ -122,6 +129,31 @@ Commander.prototype.executeNextCommand = function() { } }; +/* +Given an array of parameter strings `params` in name:value format, and an array of mandatory parameter names in `mandatoryParameters`, returns a hashmap of values or a string if error +*/ +Commander.prototype.extractNamedParameters = function(params,mandatoryParameters) { + var errors = [], + paramsByName = Object.create(null); + $tw.utils.each(params,function(param) { + var index = param.indexOf(":"); + if(index < 1) { + errors.push("malformed named parameter: '" + param + "'"); + } + paramsByName[param.slice(0,index)] = param.slice(index+1); + }); + $tw.utils.each(mandatoryParameters,function(mandatoryParameter) { + if(!$tw.utils.hop(paramsByName,mandatoryParameter)) { + errors.push("missing mandatory parameter: '" + mandatoryParameter + "'"); + } + }); + if(errors.length > 0) { + return errors.join(" and\n"); + } else { + return paramsByName; + } +}; + Commander.initCommands = function(moduleType) { moduleType = moduleType || "command"; $tw.commands = {}; diff --git a/core/modules/commands/listen.js b/core/modules/commands/listen.js new file mode 100644 index 000000000..3cb04f681 --- /dev/null +++ b/core/modules/commands/listen.js @@ -0,0 +1,54 @@ +/*\ +title: $:/core/modules/commands/listen.js +type: application/javascript +module-type: command + +Listen for HTTP requests and serve tiddlers + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var Server = require("$:/core/modules/server.js").Server; + +exports.info = { + name: "listen", + synchronous: true, + namedParameters: [] // Use named parameters, but none of them are mandatory +}; + +var Command = function(params,commander,callback) { + var self = this; + this.params = params; + this.commander = commander; + this.callback = callback; + this.knownParameters = ["port","host","rootTiddler","renderType","serveType","username","password","pathprefix","debugLevel"]; +}; + +Command.prototype.execute = function() { + var self = this; + if(!$tw.boot.wikiTiddlersPath) { + $tw.utils.warning("Warning: Wiki folder '" + $tw.boot.wikiPath + "' does not exist or is missing a tiddlywiki.info file"); + } + // Set up server + var variables = Object.create(null); + $tw.utils.each(this.knownParameters,function(name) { + if($tw.utils.hop(self.params,name)) { + variables[name] = self.params[name]; + } + }); + this.server = new Server({ + wiki: this.commander.wiki, + variables: variables + }); + var nodeServer = this.server.listen(); + $tw.hooks.invokeHook("th-server-command-post-start",this.server,nodeServer); + return null; +}; + +exports.Command = Command; + +})(); diff --git a/core/modules/commands/server.js b/core/modules/commands/server.js index 8e9a90c72..c6886596a 100644 --- a/core/modules/commands/server.js +++ b/core/modules/commands/server.js @@ -3,7 +3,7 @@ title: $:/core/modules/commands/server.js type: application/javascript module-type: command -Serve tiddlers over http +Deprecated legacy command for serving tiddlers \*/ (function(){ @@ -12,194 +12,41 @@ Serve tiddlers over http /*global $tw: false */ "use strict"; -if($tw.node) { - var util = require("util"), - fs = require("fs"), - url = require("url"), - path = require("path"), - http = require("http"); -} +var Server = require("$:/core/modules/server.js").Server; exports.info = { name: "server", synchronous: true }; -/* -A simple HTTP server with regexp-based routes -*/ -function SimpleServer(options) { - this.routes = options.routes || []; - this.wiki = options.wiki; - this.variables = options.variables || {}; -} - -SimpleServer.prototype.set = function(obj) { - var self = this; - $tw.utils.each(obj,function(value,name) { - self.variables[name] = value; - }); -}; - -SimpleServer.prototype.get = function(name) { - return this.variables[name]; -}; - -SimpleServer.prototype.addRoute = function(route) { - this.routes.push(route); -}; - -SimpleServer.prototype.findMatchingRoute = function(request,state) { - var pathprefix = this.get("pathprefix") || ""; - for(var t=0; t