From 2163302190a85138e79653db3bc6c4a724d9bfde Mon Sep 17 00:00:00 2001 From: Arlen22 Date: Tue, 10 Apr 2018 15:55:54 -0500 Subject: [PATCH] Invoke hook when server starts (#3024) * Invoke hook when server starts Invokes the `th-server-command-start` hook when the server is started, with the server object as the parameter. This allows adding a WebSocket listener to the server. * Return the HTTP server from the listen function Returns the node HTTP server created in the listen function to allow extension * Add node HTTP server to server-command-start hook * Change hook to post start in case we add a pre-start hook * Create Hook__th-server-command-post-start.tid --- core/modules/commands/server.js | 5 +++-- .../new/Hook__th-server-command-post-start.tid | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 editions/dev/tiddlers/new/Hook__th-server-command-post-start.tid diff --git a/core/modules/commands/server.js b/core/modules/commands/server.js index c92db4566..fcb097f1b 100644 --- a/core/modules/commands/server.js +++ b/core/modules/commands/server.js @@ -143,7 +143,7 @@ SimpleServer.prototype.requestHandler = function(request,response) { }; SimpleServer.prototype.listen = function(port,host) { - http.createServer(this.requestHandler.bind(this)).listen(port,host); + return http.createServer(this.requestHandler.bind(this)).listen(port,host); }; var Command = function(params,commander,callback) { @@ -302,13 +302,14 @@ Command.prototype.execute = function() { password: password, pathprefix: pathprefix }); - this.server.listen(port,host); + var nodeServer = this.server.listen(port,host); $tw.utils.log("Serving on " + host + ":" + port,"brown/orange"); $tw.utils.log("(press ctrl-C to exit)","red"); // Warn if required plugins are missing if(!$tw.wiki.getTiddler("$:/plugins/tiddlywiki/tiddlyweb") || !$tw.wiki.getTiddler("$:/plugins/tiddlywiki/filesystem")) { $tw.utils.warning("Warning: Plugins required for client-server operation (\"tiddlywiki/filesystem\" and \"tiddlywiki/tiddlyweb\") are missing from tiddlywiki.info file"); } + $tw.hooks.invokeHook('th-server-command-post-start', this.server, nodeServer); return null; }; diff --git a/editions/dev/tiddlers/new/Hook__th-server-command-post-start.tid b/editions/dev/tiddlers/new/Hook__th-server-command-post-start.tid new file mode 100644 index 000000000..cd30b42b1 --- /dev/null +++ b/editions/dev/tiddlers/new/Hook__th-server-command-post-start.tid @@ -0,0 +1,18 @@ +created: 20180409142128584 +modified: 20180409142128584 +tags: HookMechanism +title: Hook: th-server-command-post-start +type: text/vnd.tiddlywiki + +This hook allows plugins to extend the TiddlyWiki server command after it initializes. The two +most obvious use cases are adding routes (such as an attachments folder for external files) +to the SimpleServer instance and adding a websockets handler to the HTTP server. + +Hook function parameters: + +* SimpleServer instance +** Defined in core/modules/commands/server.js +* NodeJS HTTP Server instance +** See the NodeJS docs at [ext[https://nodejs.org/docs/latest-v8.x/api/http.html#http_class_http_server]] + +Return value is ignored.