Skip to content

Commit

Permalink
Create templates for the default and not found panels
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Apr 19, 2012
1 parent cdad54b commit 5ecc100
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 34 deletions.
66 changes: 32 additions & 34 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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) {
Expand All @@ -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 changes: 74 additions & 0 deletions lib/handlers.js
Original file line number Diff line number Diff line change
@@ -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;
};
Empty file added views/default.css
Empty file.
10 changes: 10 additions & 0 deletions views/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>

</body>
</html>
1 change: 1 addition & 0 deletions views/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* your JavaScript here - remember you can override this default template using 'Save'->'As Template' */
1 change: 1 addition & 0 deletions views/not_found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
document.getElementById("hello").innerHTML = "<strong>This URL does not have any code saved to it.</strong>";

0 comments on commit 5ecc100

Please sign in to comment.