Skip to content

Instantly share code, notes, and snippets.

Created December 30, 2015 17:55
Show Gist options
  • Save anonymous/438249949c1ce18ca246 to your computer and use it in GitHub Desktop.
Save anonymous/438249949c1ce18ca246 to your computer and use it in GitHub Desktop.
A file to allow running multiple TiddlyWikis as seperate folders of an Express JS application
#!/usr/bin/env node
var url = require('url');
var path = require('path');
$tw = require("./boot/boot.js").TiddlyWiki();
$tw.boot.argv = [path.join( __dirname, 'data', 'marketing')];
$tw.boot.boot();
var serverCommand = require('./core/modules/commands/server.js').Command;
var command = new serverCommand([], { wiki: $tw.wiki });
var server = command.server;
server.set({
rootTiddler: "$:/core/save/all",
renderType: "text/plain",
serveType: "text/html",
username: "",
password: "",
pathprefix: "/tw5"
});
module.exports = function(request, response, next){
// Compose the state object
var state = {};
state.wiki = $tw.wiki;
state.server = server;
state.urlInfo = url.parse(request.url);
//console.log($tw.wiki);
// Find the route that matches this path
var route = server.findMatchingRoute(request,state);
// Check for the username and password if we've got one
var username = server.get("username"),
password = server.get("password");
if(username && password) {
// Check they match
if(server.checkCredentials(request,username,password) !== "ALLOWED") {
var servername = state.wiki.getTiddlerText("$:/SiteTitle") || "TiddlyWiki5";
response.writeHead(401,"Authentication required",{
"WWW-Authenticate": 'Basic realm="Please provide your username and password to login to ' + servername + '"'
});
response.end();
return;
}
}
// Return a 404 if we didn't find a route
if(!route) {
response.writeHead(404);
response.end();
return;
}
// Set the encoding for the incoming request
// TODO: Presumably this would need tweaking if we supported PUTting binary tiddlers
request.setEncoding("utf8");
// Dispatch the appropriate method
switch(request.method) {
case "GET": // Intentional fall-through
case "DELETE":
route.handler(request,response,state);
break;
case "PUT":
var data = "";
request.on("data",function(chunk) {
data += chunk.toString();
});
request.on("end",function() {
state.data = data;
route.handler(request,response,state);
});
break;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment