Create templates for the default and not found panels

This commit is contained in:
Aron Carroll 2012-04-19 16:38:55 +01:00
parent cdad54b4f7
commit 5ecc100f27
6 changed files with 118 additions and 34 deletions

View File

@ -1,11 +1,13 @@
var options = require('./config'),
store = require('./store')(options.store),
express = require('express'),
hogan = require('hogan.js'),
path = require('path'),
fs = require('fs'),
app = express();
var express = require('express'),
hogan = require('hogan.js'),
path = require('path'),
fs = require('fs'),
app = express(),
options = require('./config'),
store = require('./store')(options.store),
handlers = require('./handlers')(app);
app.store = store;
app.templates = {};
// Apply the keys from the config file.
@ -16,9 +18,13 @@ Object.getOwnPropertyNames(options).forEach(function (key) {
app.set('root', path.resolve(path.join(__dirname, '..')));
app.set('version', require('../package').version);
// Register all the middleware.
app.use(express.logger());
app.use(express.static(path.join(app.set('root'), 'public')));
app.use(express.errorHandler({dumpExceptions: true}));
app.use(express.errorHandler({showStack: true, dumpExceptions: true}));
// Create a Hogan/Mustache handler for templates.
app.engine('html', function (path, options, fn) {
fs.readFile(path, 'utf8', function (err, template) {
if (err) {
@ -38,47 +44,39 @@ app.engine('html', function (path, options, fn) {
});
});
// Configure the template engine.
app.set('view engine', 'html');
app.set('views', path.join(app.set('root'), 'views'));
// Define some generic template variables.
app.locals({
root: (options.url.ssl ? 'https://' : 'http://') + options.url.host + options.url.prefix,
version: app.set('version'),
home: null
});
function renderIndex(req, res, bin) {
var template = {
css: bin.css || '',
html: bin.html || '',
javascript: bin.javascript || ''
};
// Set up the routes.
app.get('/', handlers.getDefault);
app.get('/:bin/edit', handlers.loadBin, handlers.getBin);
app.get('/:bin/:rev/edit', handlers.loadBin, handlers.getBin);
app.get('/:bin/:rev?', handlers.loadBin, handlers.getBinPreview);
// Very temporary render for the index file.
res.render('index', {
tips: '{}',
revision: bin.revision || 1,
json_template: JSON.stringify(template),
version: app.set('environment') === 'production' ? app.set('version') : 'debug'
});
}
app.get('/', function (req, res) {
renderIndex(req, res, {});
});
app.get('/:bin/:rev?', function (req, res) {
var rev = parseInt(req.params.rev, 10) || 1;
store.connect(function () {
store.getBin({id: req.params.bin, revision: rev}, function (err, result) {
renderIndex(req, res, result);
});
});
app.use(function (err, req, res, next) {
if (err instanceof handlers.NotFound) {
return handlers.notFound(req, res);
}
next(err);
});
// Export the application to allow it to be included.
module.exports = app;
// Run a local development server if this file is called directly.
if (require.main === module) {
app.listen(3000);
app.store.connect(function (err) {
if (err) {
throw err;
}
app.listen(3000);
});
}

74
lib/handlers.js Normal file
View File

@ -0,0 +1,74 @@
var async = require('asyncjs'),
path = require('path');
// Create a not found error handler.
function NotFound() {
Error.apply(this, arguments);
}
NotFound.prototype = Object.create(Error.prototype);
module.exports = function (app) {
var handlers = {
getDefault: function (req, res) {
handlers.renderFiles(req, res);
},
getBin: function (req, res, next) {
handlers.render(req, res, req.bin);
},
getBinPreview: function (req, res) {
res.send('bin: ' + req.bin.id);
},
notFound: function (req, res) {
var files = handlers.defaultFiles();
files[2] = 'not_found.js';
handlers.renderFiles(req, res, files);
},
loadBin: function (req, res, next) {
var rev = parseInt(req.params.rev, 10) || 1,
query = {id: req.params.bin, revision: rev};
app.store.getBin(query, function (err, result) {
if (err) {
return next(new NotFound('Could not find bin: ' + req.params.bin));
} else {
req.bin = result;
next();
}
});
},
render: function (req, res, bin) {
var template = {
css: bin.css || '',
html: bin.html || '',
javascript: bin.javascript || ''
};
// Very temporary render for the index file.
res.render('index', {
tips: '{}',
revision: bin.revision || 1,
json_template: JSON.stringify(template),
version: app.set('environment') === 'production' ? app.set('version') : 'debug'
});
},
renderFiles: function (req, res, files) {
files = files || handlers.defaultFiles();
async.files(files, app.set('views')).readFile("utf8").toArray(function (err, results) {
if (!err) {
handlers.render(req, res, {
html: results[0].data,
css: results[1].data,
javascript: results[2].data
});
}
});
},
defaultFiles: function () {
return ['html', 'css', 'js'].map(function (ext) {
return 'default.' + ext;
});
},
NotFound: NotFound
};
return handlers;
};

0
views/default.css Normal file
View File

10
views/default.html Normal file
View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
</body>
</html>

1
views/default.js Normal file
View File

@ -0,0 +1 @@
/* your JavaScript here - remember you can override this default template using 'Save'->'As Template' */

1
views/not_found.js Normal file
View File

@ -0,0 +1 @@
document.getElementById("hello").innerHTML = "<strong>This URL does not have any code saved to it.</strong>";