Skip to content
This repository has been archived by the owner on Aug 22, 2019. It is now read-only.

Commit

Permalink
Merge pull request #448 from stenington/express-3-and-nunjucks-on-dev
Browse files Browse the repository at this point in the history
Express 3 and nunjucks on dev
  • Loading branch information
stenington committed Nov 20, 2012
2 parents 6c8801f + ec9694f commit a1991ad
Show file tree
Hide file tree
Showing 37 changed files with 3,768 additions and 1,283 deletions.
27 changes: 12 additions & 15 deletions app.js
@@ -1,50 +1,47 @@
// Configure & start express.
var express = require('express');
var http = require('http');
var fs = require('fs');
var path = require('path');
var middleware = require('./middleware');
var logger = require('./lib/logging').logger;
var configuration = require('./lib/configuration');
var hogan = require('hogan.js');
var hoganadapter = require('./lib/hogan-express.js');
var flash = require('connect-flash');
var nunjucks = require('nunjucks');

var app = express.createServer();
var app = express();
app.logger = logger;
app.config = configuration;

// default view engine
app.set('view engine', 'hogan.js');
app.register('hogan.js', hoganadapter.init(hogan));

// View helpers. `user` and `badges` are set so we can use them in `if`
// statements without getting undefined errors and without having to use typeof
// checks.
app.helpers({
app.locals({
login: true,
title: 'Backpack',
error: [],
success: [],
badges: {},
});

app.dynamicHelpers({
user: function (req, res) {
return req.user || null;
}
});
// default view engine
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader('views'));
env.express(app);

// Middleware. Also see `middleware.js`
// ------------------------------------
app.use(express.static(path.join(__dirname, "static")));
app.use(express.static(path.join(configuration.get('var_dir'), "badges")));
app.use("/views", express.static(path.join(__dirname, "views")));
app.use(middleware.noFrame({ whitelist: [ '/issuer/frame.*', '/', '/share/.*' ] }));
app.use(express.bodyParser({ uploadDir: configuration.get('badge_path') }));
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(middleware.logRequests());
app.use(middleware.cookieSessions());
app.use(middleware.userFromSession());
app.use(middleware.csrf({
app.use(flash());
app.use(middleware.csrf({
whitelist: [
'/backpack/authenticate',
'/issuer/validator/?',
Expand Down Expand Up @@ -145,5 +142,5 @@ if (!module.parent) {
};
start_server(app);
} else {
module.exports = app;
module.exports = http.createServer(app);
}
8 changes: 4 additions & 4 deletions controllers/backpack.js
Expand Up @@ -19,7 +19,7 @@ var Group = require('../models/group');
exports.login = function login(request, response) {
// request.flash returns an array. Pass on the whole thing to the view and
// decide there if we want to display all of them or just the first one.
response.render('login', {
response.render('login.html', {
error: request.flash('error'),
csrfToken: request.session._csrf
});
Expand Down Expand Up @@ -112,7 +112,7 @@ exports.stats = function stats(request, response, next) {
function startResponse(err, badges) {
if (err) return next(err);
var data = computeStats(badges);
response.render('stats', { stats: data });
response.render('stats.html', data);
}

function computeStats(badges) {
Expand Down Expand Up @@ -218,13 +218,13 @@ exports.manage = function manage(request, response, next) {
if (err) return next(err);
prepareBadgeIndex(badges);
modifyGroups(groups);
response.render('backpack', {
response.render('backpack.html', {
error: error,
success: success,
badges: badges,
csrfToken: request.session._csrf,
groups: groups,
tooltips: request.param('tooltips')
tooltips: typeof request.param('tooltips') !== 'undefined'
});
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/baker.js
Expand Up @@ -50,7 +50,7 @@ exports.baker = function (request, response) {

// render the badge baker frontend and bounce if no assertion was passed
if (!assertionUrl)
return response.render('baker', { title: 'Creator', login: false });
return response.render('baker.html', { title: 'Creator', login: false });

// all errors get reported as json. we will explicitly set the
// content-type to image/png on success.
Expand Down
2 changes: 1 addition & 1 deletion controllers/demo.js
Expand Up @@ -13,7 +13,7 @@ var ORIGIN = protocol + '://' + configuration.get('hostname') + (port ? ':' + po

// Render the view for the demo badge issuer.
exports.issuer = function (req, res) {
res.render('issuer', {
res.render('issuer.html', {
login: false,
title: 'Demo Issuer',
csrfToken: req.session._csrf
Expand Down
8 changes: 4 additions & 4 deletions controllers/issuer.js
Expand Up @@ -99,7 +99,7 @@ exports.generateScript = function (req, res) {

exports.frame = function (req, res) {
res.header('Cache-Control', 'no-cache, must-revalidate');
res.render('badge-accept', {
res.render('badge-accept.html', {
layout: null,
framed: true,
csrfToken: req.session._csrf,
Expand All @@ -118,7 +118,7 @@ exports.frameless = function (req, res) {
}
}
res.header('Cache-Control', 'no-cache, must-revalidate');
res.render('badge-accept', {
res.render('badge-accept.html', {
layout: null,
framed: false,
assertions: JSON.stringify(assertionUrls),
Expand Down Expand Up @@ -342,7 +342,7 @@ exports.validator = function (request, response) {

'default': function () {
var fielderrors = _.map(fields, humanize);
return response.render('validator', {
return response.render('validator.html', {
status: 200,
errors: fielderrors,
csrfToken: request.session._csrf,
Expand All @@ -366,7 +366,7 @@ exports.welcome = function(request, response, next) {
if (badges && badges.length)
return response.redirect('/', 303);
else
return response.render('issuer-welcome');
return response.render('issuer-welcome.html');
}

Badge.find({email: user.get('email')}, makeResponse);
Expand Down
4 changes: 2 additions & 2 deletions controllers/share.js
Expand Up @@ -69,7 +69,7 @@ exports.editor = function (request, response) {
portfolio.group = group;
portfolio.badges = badgesWithStories;
portfolio.preamble = prepareText(portfolio.get('preamble'));
response.render('portfolio-editor', {
response.render('portfolio-editor.html', {
csrfToken: request.session._csrf,
portfolio: portfolio
});
Expand Down Expand Up @@ -128,7 +128,7 @@ exports.show = function (request, response, next) {
portfolio.badges = badgesWithStories;
portfolio.preamble = prepareText(portfolio.get('preamble'));

return response.render('portfolio', {
return response.render('portfolio.html', {
opengraph: [
{ property: 'title', content: portfolio.attributes.title },
{ property: 'type', content: 'openbadges:share' },
Expand Down
18 changes: 0 additions & 18 deletions lib/hogan-express.js

This file was deleted.

2 changes: 1 addition & 1 deletion middleware.js
Expand Up @@ -71,7 +71,7 @@ exports.userFromSession = function userFromSession() {
logger.error("Problem finding/creating user:");
logger.error(err);
}
req.user = user;
req.user = res.locals.user = user;
return next();
});
};
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -11,15 +11,16 @@
"colors": "*",
"commander": "~0.5.2",
"connect-cookie-session": "git+https://github.com/brianloveswords/connect-cookie-session.git",
"express": "~2.5.9",
"connect-flash": "*",
"express": "~3",
"formidable": "~1.0.9",
"functools": "~1.2.0",
"hogan.js": "~1.0.5",
"jshint": "~0.6.2",
"metapng": "git+https://github.com/brianloveswords/metapng.js.git",
"mime": "~1.2.5",
"mysql": "~0.9.5",
"nock": "~0.11.6",
"nunjucks": "~0.1.6",
"request": "~2.9.200",
"soda": "~0.2.5",
"validator": "~0.4.5",
Expand Down

0 comments on commit a1991ad

Please sign in to comment.