/** * Adds bindings for the keystone routes * * ####Example: * * var app = express(); * app.configure(...); // configuration settings * app.use(...); // middleware, routes, etc. should come before keystone is initialised * keystone.routes(app); * * @param {Express()} app * @api public */ function routes(app) { this.app = app; var keystone = this; //Set backend url var backend_dir = process.env.BACKEND_DIR ? process.env.BACKEND_DIR : '/keystone'; this.set('backend dir', backend_dir); //catch all for custom admin directoy url if(backend_dir != '/keystone'){ app.all('/keystone*', function(req, res, next){ if(req.originalMethod == "GET") { return res.redirect(backend_dir + req.params[0]); } if(req.originalMethod == "POST") { //need to work this out //just getting a POC from the existing codebase //Will probably end up switching to headless and //using resty-stone or keystone rest //and will build out the admin UI custom. } }); } //Set backend static path for assets if config variable is declared if(process.env.BACKEND_STATIC_MOUNT && process.env.BACKEND_STATIC_PATH) { var express = this.express, static_mount = process.env.BACKEND_STATIC_MOUNT, static_path = require("shelljs").pwd() + process.env.BACKEND_STATIC_PATH; this.set('backend static mount', static_mount); this.set('backend static path', static_path); app.use(static_mount, express.static(static_path)); } /// ensure keystone nav has been initialised if (!this.nav) { this.nav = this.initNav(); } // Cache compiled view templates if we are in Production mode this.set('view cache', this.get('env') === 'production'); // Bind auth middleware (generic or custom) to /backend_dir* routes, allowing // access to the generic signin page if generic auth is used if (this.get('auth') === true) { if (!this.get('signout url')) { this.set('signout url', backend_dir+'/signout'); } if (!this.get('signin url')) { this.set('signin url', backend_dir+'/signin'); } if (!this.nativeApp || !this.get('session')) { app.all(backend_dir+'*', this.session.persist); } app.all(backend_dir+'/signin', require('../node_modules/keystone/routes/views/signin')); app.all(backend_dir+'/signout', require('../node_modules/keystone/routes/views/signout')); app.all(backend_dir+'*', this.session.keystoneAuth); } else if ('function' === typeof this.get('auth')) { app.all(backend_dir+'*', this.get('auth')); } // Keystone Admin Route app.all(backend_dir, require('../node_modules/keystone/routes/views/home.js')); // Email test routes if (this.get('email tests')) { this.bindEmailTestRoutes(app, this.get('email tests')); } // Cloudinary API for image uploading (only if Cloudinary is configured) if (keystone.get('wysiwyg cloudinary images')) { if (!keystone.get('cloudinary config')) { throw new Error('KeystoneJS Initialisaton Error:\n\nTo use wysiwyg cloudinary images, the \'cloudinary config\' setting must be configured.\n\n'); } app.post(backend_dir+'/api/cloudinary/upload', require('.../node_modules/keystone/routes/api/cloudinary').upload); } // Cloudinary API for selecting an existing image from the cloud if (keystone.get('cloudinary config')) { app.get(backend_dir+'/api/cloudinary/get', require('../node_modules/keystone/routes/api/cloudinary').get); app.get(backend_dir+'/api/cloudinary/autocomplete', require('../node_modules/keystone/routes/api/cloudinary').autocomplete); } var initList = function(protect) { return function(req, res, next) { req.list = keystone.list(req.params.list); if (!req.list || (protect && req.list.get('hidden'))) { req.flash('error', 'List ' + req.params.list + ' could not be found.'); return res.redirect(backend_dir+''); } next(); }; }; // Generic Lists API app.all(backend_dir+'/api/:list/:action', initList(), require('./routes/api.list')); // Generic Lists Download Route app.all(backend_dir+'/download/:list', initList(), require('../node_modules/keystone/routes/download/list')); // List and Item Details Admin Routes app.all(backend_dir+'/:list/:page([0-9]{1,5})?', initList(true), require('./routes/view.list')); app.all(backend_dir+'/:list/:item', initList(true), require('./routes/view.item')); return this; } module.exports = routes;