From 6e1f689cfd8f820090c4ab15519114f4d3bf929f Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Fri, 10 Apr 2015 21:38:31 -0400 Subject: Overhaul DB impl Make DB schema aware Add autoincrement support Add custom ID field support --- app/index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'app/index.js') diff --git a/app/index.js b/app/index.js index b312eb1..fc38b0a 100644 --- a/app/index.js +++ b/app/index.js @@ -77,8 +77,11 @@ function OKCMS(options) { var adminTemplateProvider = this._adminTemplateProvider = new OKTemplate({root: adminTemplateRoot}); - var db = new OKDB(options.db || 'fs'); var schemas = this._schemas = this._createSchemas(schemaConfig); + var db = new OKDB({ + db: options.db || 'fs', + schemas: schemas + }); var resourceCache = this._resourceCache = this._createResources(resourceConfig, db, schemas); @@ -248,7 +251,7 @@ function ResourceCache(resources) { throw new Error('Undefined resource given to ResourceCache'); if (resource.bound) { cache[resource.type] = resource.parent; - cache[resource.type + ':' + resource.id] = resource; + cache[resource.type + ':' + resource.getID()] = resource; } else { cache[resource.type] = resource; } -- cgit v1.2.3-70-g09d2 From 1eff07c9c2ca5b61a28a1037a586d25c3791d67b Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Fri, 10 Apr 2015 22:37:33 -0400 Subject: Add autoincrementing index to resources --- app/index.js | 2 ++ app/node_modules/okschema/index.js | 5 +++++ 2 files changed, 7 insertions(+) (limited to 'app/index.js') diff --git a/app/index.js b/app/index.js index fc38b0a..e462b48 100644 --- a/app/index.js +++ b/app/index.js @@ -122,6 +122,8 @@ OKCMS.prototype._createSchemas = function(schemaConfig) { schemaConfig = schemaConfig || {}; return Object.keys(schemaConfig).reduce(function(cache, key) { var spec = schemaConfig[key]; + // All resources have an autoincrementing index so we can order them suckas + spec.__index = {type: 'meta', autoincrement: true}; cache[key] = OKSchema(spec); return cache; }, {}); diff --git a/app/node_modules/okschema/index.js b/app/node_modules/okschema/index.js index 4633e7a..d53ed7b 100644 --- a/app/node_modules/okschema/index.js +++ b/app/node_modules/okschema/index.js @@ -56,6 +56,11 @@ var types = { }]; } } + }, + // Special type for resource meta information + 'meta': { + parent: 'string', + assertValid: function(spec, value) {} } } -- cgit v1.2.3-70-g09d2 From 3b16b2d58f876f6ab4abcdf8012efb2411504940 Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Mon, 13 Apr 2015 10:25:46 -0400 Subject: HTTP Digest authentication --- app/index.js | 2 +- app/node_modules/okadminview/index.js | 19 +++++++++++++++++++ app/node_modules/okadminview/package.json | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) (limited to 'app/index.js') diff --git a/app/index.js b/app/index.js index e462b48..708943c 100644 --- a/app/index.js +++ b/app/index.js @@ -32,7 +32,7 @@ function OKCMS(options) { var adminConfig = options.admin || {}; var adminRoot = this._adminRoot = adminConfig.root || path.join(__dirname, '../themes/okadmin/public'); - var adminPath = this._adminPath = adminConfig.path || '/_admin' + var adminPath = this._adminPath = '/_admin'; var templateRoot = options.templateRoot || 'templates'; var adminTemplateRoot = options.templateRoot || path.join(__dirname, '../themes/okadmin/templates'); diff --git a/app/node_modules/okadminview/index.js b/app/node_modules/okadminview/index.js index 897c583..ac633e8 100644 --- a/app/node_modules/okadminview/index.js +++ b/app/node_modules/okadminview/index.js @@ -4,10 +4,22 @@ var bodyParser = require('body-parser'); var methodOverride = require('method-override'); var session = require('express-session'); var flash = require('connect-flash'); +var passport = require('passport'); +var DigestStrategy = require('passport-http').DigestStrategy; var Q = require('q'); var pluralize = require('pluralize'); var OKQuery = require('okquery'); +// Configure auth +passport.use(new DigestStrategy({qop: 'auth'}, + function authenticate(username, done) { + if (!process.env.OK_USER || !process.env.OK_PASS) { + return done(new Error('No user or pass configured on server')); + } else { + return done(null, process.env.OK_USER, process.env.OK_PASS); + } +})); + /** * OKAdminView! */ @@ -100,6 +112,13 @@ function OKAdminView(options) { } })); + var auth = passport.authenticate('digest', {session: false}); + + // This should really be mounted on the router, but can't be due to + // https://github.com/jaredhanson/passport-http/pull/16 + app.use('/_admin/', passport.initialize()); + app.all('/_admin/:path*', auth); + router.get('/', function readIndex(req, res, next) { fetchIndexTemplateData(meta, indexQueries).then(function(data) { view.renderIndex(req, res, assign(data, { diff --git a/app/node_modules/okadminview/package.json b/app/node_modules/okadminview/package.json index 4c6d11c..bdaefc5 100644 --- a/app/node_modules/okadminview/package.json +++ b/app/node_modules/okadminview/package.json @@ -15,6 +15,8 @@ "lodash.clonedeep": "^3.0.0", "method-override": "^2.3.2", "object-assign": "^2.0.0", + "passport": "^0.2.1", + "passport-http": "^0.2.2", "pluralize": "^1.1.2", "q": "^1.2.0" } -- cgit v1.2.3-70-g09d2 From 74c749554f46192b0d4424fe5371982d1e48f37c Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 13 Apr 2015 12:38:53 -0400 Subject: put admin area on /admin --- Readme.md | 1 + app/index.js | 2 +- themes/okadmin/templates/partials/tail.liquid | 8 ++++---- 3 files changed, 6 insertions(+), 5 deletions(-) (limited to 'app/index.js') diff --git a/Readme.md b/Readme.md index a96860f..b9e5b2d 100644 --- a/Readme.md +++ b/Readme.md @@ -10,4 +10,5 @@ * node index Server will be running on http://lvh.me:1337/ +Admin area available on http://lvh.me:1337/admin/ diff --git a/app/index.js b/app/index.js index 708943c..719c424 100644 --- a/app/index.js +++ b/app/index.js @@ -32,7 +32,7 @@ function OKCMS(options) { var adminConfig = options.admin || {}; var adminRoot = this._adminRoot = adminConfig.root || path.join(__dirname, '../themes/okadmin/public'); - var adminPath = this._adminPath = '/_admin'; + var adminPath = this._adminPath = '/admin'; var templateRoot = options.templateRoot || 'templates'; var adminTemplateRoot = options.templateRoot || path.join(__dirname, '../themes/okadmin/templates'); diff --git a/themes/okadmin/templates/partials/tail.liquid b/themes/okadmin/templates/partials/tail.liquid index 88764a6..b3c575d 100644 --- a/themes/okadmin/templates/partials/tail.liquid +++ b/themes/okadmin/templates/partials/tail.liquid @@ -2,8 +2,8 @@ - - - - + + + + -- cgit v1.2.3-70-g09d2 From 5b7e5a133e5326a98b974b7e9e390ac393d1a05e Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Mon, 13 Apr 2015 13:42:11 -0400 Subject: Client friendly error handling --- app/index.js | 64 ++++++++++++++-- app/node_modules/okadminview/index.js | 125 ++++++++++++++------------------ app/node_modules/okserver/index.js | 7 ++ app/node_modules/okview/index.js | 133 +++++++++++++++------------------- examples/index.js | 2 + themes/okadmin/templates/404.liquid | 54 ++++++++++++++ themes/okadmin/templates/5xx.liquid | 54 ++++++++++++++ 7 files changed, 289 insertions(+), 150 deletions(-) create mode 100644 themes/okadmin/templates/404.liquid create mode 100644 themes/okadmin/templates/5xx.liquid (limited to 'app/index.js') diff --git a/app/index.js b/app/index.js index 719c424..419adfc 100644 --- a/app/index.js +++ b/app/index.js @@ -36,6 +36,7 @@ function OKCMS(options) { var templateRoot = options.templateRoot || 'templates'; var adminTemplateRoot = options.templateRoot || path.join(__dirname, '../themes/okadmin/templates'); + var debug = options.debug || false; // Set metadata defaults // TODO Abstract this out somewhere else @@ -84,13 +85,17 @@ function OKCMS(options) { }); var resourceCache = this._resourceCache = this._createResources(resourceConfig, db, schemas); + var errorHandler = createErrorHandlerProducer( + templateProvider, adminTemplateProvider, debug); // Create view instances from config var views = this._views = - this._createViews(viewConfig, db, meta, resourceCache, templateProvider); + this._createViews(viewConfig, db, meta, resourceCache, templateProvider, + errorHandler); var adminViews = this._adminViews = this._createAdminViews(adminPath, app, express, resourceConfig, - resourceCache, adminTemplateProvider, adminMeta); + resourceCache, adminTemplateProvider, adminMeta, + errorHandler); // Create services var imageService = OKImageService({ @@ -109,7 +114,8 @@ function OKCMS(options) { adminPath: adminPath, services: { image: imageService - } + }, + errorHandler: errorHandler }); } @@ -152,7 +158,7 @@ OKCMS.prototype._createResources = function(resourceConfig, db, schemaCache) { }; OKCMS.prototype._createViews = function(viewConfig, db, - meta, resourceCache, templateProvider) { + meta, resourceCache, templateProvider, errorHandler) { viewConfig = viewConfig || {}; var self = this; var createQueries = this._createQueries.bind(this); @@ -171,7 +177,8 @@ OKCMS.prototype._createViews = function(viewConfig, db, route: route, template: template, queries: queries, - meta: meta + meta: meta, + errorHandler: errorHandler }); return cache; }, {}); @@ -193,7 +200,8 @@ OKCMS.prototype._createViews = function(viewConfig, db, }; OKCMS.prototype._createAdminViews = function(path, app, express, - resourceConfig, resourceCache, templateProvider, meta) { + resourceConfig, resourceCache, templateProvider, meta, + errorHandler) { var views = {}; var withTrail = withTrailingSlash(path); var withoutTrail = withoutTrailingSlash(path); @@ -217,7 +225,8 @@ OKCMS.prototype._createAdminViews = function(path, app, express, resourceConfig: resourceConfig, resourceCache: resourceCache, templateProvider: templateProvider, - meta: meta + meta: meta, + errorHandler: errorHandler }); return views; }; @@ -269,6 +278,47 @@ ResourceCache.prototype.get = function(type, id) { } }; +/** + * Higher order function implementing customizable error handling + */ +function createErrorHandlerProducer(templateProvider, adminTemplateProvider, debugMode) { + + var template404 = templateProvider.getTemplate('404') || + adminTemplateProvider.getTemplate('404'); + var template5xx = templateProvider.getTemplate('5xx') || + adminTemplateProvider.getTemplate('5xx'); + + if (!template404 || !template5xx) + throw new Error('No error templates provided by admin theme or user') + + return debugMode ? createDebugHandler : createErrorHandler; + + function createErrorHandler(req, res, status) { + return function handleError(err) { + switch (status) { + case 404: + template404.render().then(function(rendered) { + res.status(status); + res.send(rendered); + }); + break; + default: + template5xx.render().then(function(rendered) { + res.status(status); + res.send(rendered); + }); + } + + }; + } + + function createDebugHandler(req, res, status) { + return function handleError(err) { + res.send(err.stack); + }; + } +} + module.exports = { createApp: function(options) { diff --git a/app/node_modules/okadminview/index.js b/app/node_modules/okadminview/index.js index 2f31e1e..648487c 100644 --- a/app/node_modules/okadminview/index.js +++ b/app/node_modules/okadminview/index.js @@ -37,6 +37,8 @@ function OKAdminView(options) { throw new Error('No templateProvider provided to OKAdminView'); if (!options.meta) throw new Error('No meta query provided to OKAdminView'); + if (!options.errorHandler) + throw new Error('No error handler provided to OKAdminView'); var app = options.app; var express = options.express; @@ -44,6 +46,7 @@ function OKAdminView(options) { var resourceCache = this._resourceCache = options.resourceCache; var resourceConfig = this._resourceConfig = cloneDeep(options.resourceConfig); var provider = options.templateProvider; + var error = this._error = options.errorHandler; // Load templates var templates = this._templates = @@ -112,12 +115,10 @@ function OKAdminView(options) { } })); - var auth = passport.authenticate('digest', {session: false}); - // This should really be mounted on the router, but can't be due to // https://github.com/jaredhanson/passport-http/pull/16 app.use('/admin/', passport.initialize()); - app.all('/admin/:path*', auth); + app.all('/admin/:path*', passport.authenticate('digest', {session: false})); router.get('/', function readIndex(req, res, next) { fetchIndexTemplateData(meta, indexQueries).then(function(data) { @@ -125,22 +126,22 @@ function OKAdminView(options) { success: req.flash('success'), errors: req.flash('errors') })); - }).fail(errorHandler(req, res)); + }).fail(error(req, res, 500)); }); router.get('/:type/new/', function createResourceView(req, res, next) { var type = req.params.type || ''; var resource = resourceCache.get(type); if (!resource) { - errorHandler(req, res)(new Error('No such resource ' + type)); + error(req, res, 404)(new Error('No such resource ' + type)); } else { meta.get().then(function(metadata) { - var templateData = getResourceTemplateData(metadata, resource, {}); + var templateData = transformData(metadata, resource, {}); view.renderResourceNew(req, res, assign(templateData, { success: req.flash('success'), errors: req.flash('errors'), })); - }).fail(errorHandler(req, res)); + }).fail(error(req, res, 500)); } }); @@ -148,26 +149,27 @@ function OKAdminView(options) { var type = req.params.type || ''; var id = req.params.id || ''; var resource = resourceCache.get(type, id); - if (!resource) { - errorHandler(req, res)(new Error('No such resource')); - } else { - var query = OKQuery({ - resource: resource, - query: id - }); - fetchResourceTemplateData(meta, query, getResourceTemplateData) - .then(function(data) { - if (!data) { - resourceMissingHandler(req, res)() - } else { - view.renderResource(req, res, assign(data, { - success: req.flash('success'), - errors: req.flash('errors') - })); - } - }).fail(errorHandler(req, res)); - } - + var query = OKQuery({ + resource: resource, + query: id + }); + fetchResourceTemplateData(meta, query, transformData) + .then(function(data) { + if (!data) { + resourceMissingHandler(req, res)() + } else { + view.renderResource(req, res, assign(data, { + success: req.flash('success'), + errors: req.flash('errors') + })); + } + }).fail(function(err) { + if (err.message === 'No resource data') { + error(req, res, 404)(new Error('No such resource')); + } else { + error(req, res, 500)(err); + } + }); }); router.post('/:type/', function createResource(req, res, next) { @@ -175,7 +177,7 @@ function OKAdminView(options) { var resource = resourceCache.get(type); var data = req.body; if (!resource) { - errorHandler(req, res)(new Error('No such resource ' + type)); + error(req, res, 400)(new Error('No such resource ' + type)); } else { meta.get().then(function(metadata) { try { @@ -183,12 +185,12 @@ function OKAdminView(options) { resource.create(data).then(function(created) { req.flash('success', {action: 'create'}); res.redirect(303, resource.getID(data)); - }).fail(errorHandler(req, res)); + }).fail(error(req, res, 500)); } catch (errors) { - var templateData = getResourceTemplateData(metadata, resource, data); + var templateData = transformData(metadata, resource, data); view.renderResource(req, res, assign(templateData, {errors: errors})); } - }).fail(errorHandler(req, res));; + }).fail(error(req, res, 500));; } }); @@ -198,11 +200,9 @@ function OKAdminView(options) { var resourcesJSON = body[type]; var resource = resourceCache.get(type); if (!resourcesJSON || !resourcesJSON.length) { - res.status(400); - errorHandler(req, res)(new Error('Bad request')); + error(req, res, 400)(new Error('Bad request')); } else if (!resource) { - res.status(404); - errorHandler(req, res)(new Error('No such resource')); + error(req, res, 404)(new Error('No such resource')); } else { try { var ids = []; @@ -212,7 +212,7 @@ function OKAdminView(options) { return data; }); } catch (e) { - errorHandler(req, res)(new Error('Resource batch contains invalid JSON')); + error(req, res, 500)(new Error('Resource batch contains invalid JSON')); return; } Q.all([ @@ -222,7 +222,7 @@ function OKAdminView(options) { var metadata = results.shift(); req.flash('success', {action: 'batch_update'}); res.redirect(303, '../..'); - }).fail(errorHandler(req, res)); + }).fail(error(req, res, 500)); } }); @@ -232,7 +232,7 @@ function OKAdminView(options) { var data = req.body; var resource = resourceCache.get(type, id); if (!resource) { - errorHandler(req, res)(new Error('No such resource ' + type)); + error(req, res, 400)(new Error('No such resource ' + type)); } else { // TODO Prob should make metadata synchronous... meta.get().then(function(metadata) { @@ -241,12 +241,12 @@ function OKAdminView(options) { resource.update(id, data).then(function(updated) { req.flash('success', {action: 'update'}); res.redirect(303, '../' + resource.getID(updated)); - }).fail(errorHandler(req, res)); + }).fail(error(req, res, 500)); } catch (errors) { - var templateData = getResourceTemplateData(metadata, resource, data); + var templateData = transformData(metadata, resource, data); view.renderResource(req, res, assign(templateData, {errors: errors})); } - }).fail(errorHandler(req, res)); + }).fail(error(req, res, 500)); } }); @@ -255,14 +255,14 @@ function OKAdminView(options) { var id = req.params.id; var resource = resourceCache.get(type, id); if (!resource) { - errorHandler(req, res)(new Error('No such resource ' + type)); + error(req, res, 500)(new Error('No such resource ' + type)); } else { meta.get().then(function(metadata) { resource.destroy(id).then(function() { req.flash('success', {action: 'delete'}); res.redirect(303, '../..'); - }).fail(errorHandler(req, res)); - }).fail(errorHandler(req, res)); + }).fail(error(req, res, 500)); + }).fail(error(req, res, 500)); } }); @@ -271,9 +271,9 @@ function OKAdminView(options) { } /** - * Get template data for a single resource + * Yields formatted template data for a single resource */ -function getResourceTemplateData(meta, resource, data) { +function transformData(meta, resource, data) { meta = meta || {}; resource = resource || {}; data = data || {}; @@ -306,21 +306,21 @@ OKAdminView.prototype.renderIndex = function(req, res, data) { data = data || {}; this._templates['index'].render(data).then(function(rendered) { res.send(rendered); - }).fail(errorHandler(req, res)); + }).fail(this._error(req, res, 500)); }; OKAdminView.prototype.renderResource = function(req, res, data) { data = data || {}; this._templates['resource'].render(data).then(function(rendered) { res.send(rendered); - }).fail(errorHandler(req, res)); + }).fail(this._error(req, res, 500)); }; OKAdminView.prototype.renderResourceNew = function(req, res, data) { data = data || {meta: {}, resource: {}}; this._templates['resource_new'].render(data).then(function(rendered) { res.send(rendered); - }).fail(errorHandler(req, res)); + }).fail(this._error(req, res, 500)); }; /** @@ -378,30 +378,15 @@ function fetchResourceTemplateData(meta, query, fn) { return Q.promise(function(resolve, reject) { meta.get().then(function(metadata) { query.get().then(function(data) { - var resource = query.resource; - resolve(fn(metadata, resource, data)); + if (!data) { + reject(new Error('No resource data')); + } else { + var resource = query.resource; + resolve(fn(metadata, resource, data)); + } }).fail(reject); }).fail(reject) }); } -/** - * TODO Real error handling - */ -function errorHandler(req, res) { - return function(err) { - res.send(err.stack); - }; -} - -/** - * TODO Real 404 handling - */ -function resourceMissingHandler(req, res) { - return function() { - res.status(404); - res.send('404'); - } -} - module.exports = OKAdminView; diff --git a/app/node_modules/okserver/index.js b/app/node_modules/okserver/index.js index cf06b3c..abca8b5 100644 --- a/app/node_modules/okserver/index.js +++ b/app/node_modules/okserver/index.js @@ -16,6 +16,8 @@ function OKServer(options) { throw new Error('No admin root directory provided to OKServer'); if (!options.adminPath) throw new Error('No admin path provided to OKServer'); + if (!options.errorHandler) + throw new Error('No error handler provided to OKServer'); var root = options.root; var adminRoot = options.adminRoot; var adminPath = options.adminPath; @@ -25,6 +27,7 @@ function OKServer(options) { var router = express.Router({ strict: app.get('strict routing') }); + var error = options.errorHandler; var services = options.services || {}; Object.keys(views) // Sort such that more general routes are matched last @@ -67,6 +70,10 @@ function OKServer(options) { // Make sure this lady is last. Checks whether the desired // route has a trailing-slash counterpart and redirects there app.use(slash()); + // Otherwise it's a 404 + app.use(function(req, res) { + error(req, res, 404)(new Error('No matching route')); + }); } OKServer.prototype.listen = function listen(port) { diff --git a/app/node_modules/okview/index.js b/app/node_modules/okview/index.js index 2d1c0a0..63f22b5 100644 --- a/app/node_modules/okview/index.js +++ b/app/node_modules/okview/index.js @@ -24,8 +24,11 @@ function OKView(options) { throw new Error('No meta resource provided to OKView'); if (!options.route) throw new Error('No route provided to OKView'); + if (!options.errorHandler) + throw new Error('No error handler provided to OKView'); var route = options.route; var mount = options.mount || 'get'; + var error = this._error = options.errorHandler; this._template = options.template; var meta = this._meta = options.meta; var queries = this._queries = options.queries || []; @@ -47,16 +50,9 @@ function OKView(options) { enumerable: true }); - this._middleware = createMiddleware(this); - this._fetchTemplateData = unbound ? fetchUnbound : fetchBound; - - function fetchUnbound(id) { - return fetchTemplateData(meta, queries, id) - } - - function fetchBound() { - return fetchTemplateData(meta, queries); - } + this._middleware = unbound + ? unboundMiddleware(this, meta, queries, error) + : boundMiddleware(this, meta, queries, error); } OKView.prototype.middleware = function() { @@ -66,39 +62,20 @@ OKView.prototype.middleware = function() { OKView.prototype.render = function(req, res, data) { this._template.render(data).then(function(html) { res.send(html); - }).fail(errorHandler(req, res, data)); -}; - -OKView.prototype.fetchTemplateData = function() { - return this._fetchTemplateData.apply(this, arguments); + }).fail(this._error(req, res, 500)); }; -/** - * Unbound views need different middleware to resolve requests - */ -function createMiddleware(view) { - if (view.unbound) { - return unboundMiddleware(view); - } else { - return boundMiddleware(view); - } -} - -// Note that these middleware do not call next -// and should thus always be added at the end of the -// middleware chain. - /** * Creates middleware for a view which does not * yet have a resource id associated with it */ -function unboundMiddleware(view) { +function unboundMiddleware(view, meta, queries, error) { var paramName = getParamName(view.route); return function(req, res, next) { var id = req.params[paramName]; - view.fetchTemplateData(id).then(function(data) { + fetchTemplateData(meta, queries, id).then(function(data) { view.render(req, res, data); - }).fail(errorHandler(req, res, next)); + }).fail(failHandler(req, res, error)); }; } @@ -106,20 +83,22 @@ function unboundMiddleware(view) { * Creates middleware for a view which already * has a resource id associated with it */ -function boundMiddleware(view) { +function boundMiddleware(view, meta, queries, error) { return function(req, res, next) { - view.fetchTemplateData().then(function(data) { + fetchTemplateData(meta, queries).then(function(data) { view.render(req, res, data); - }).fail(errorHandler(req, res, next)); + }).fail(failHandler(req, res, error)); }; } -/** - * TODO BS error handling for now - */ -function errorHandler(req, res, next) { - return function(err) { - res.send(err.stack); +function failHandler(req, res, error) { + return function (err) { + // TODO Use custom exception type + if (err.message === 'No resource found') { + error(req, res, 404)(err); + } else { + error(req, res, 500)(err); + } } } @@ -138,9 +117,12 @@ function getParamName(route) { * and returns a promise for an object merging all queried * data, pluralizing keys where necessary. * - * Lil bit convoluted, sorry. + * Pretty convoluted, sorry. */ function fetchTemplateData(meta, queries, id) { + // If there's only one query, we assume it is for a single + // resource and will resolve errors if no data is found + var single = queries && queries.length === 1; return Q.promise(function(resolve, reject) { return Q.all( [meta.get()].concat(queries.map(function(query) { @@ -148,39 +130,44 @@ function fetchTemplateData(meta, queries, id) { }))) .then(function(results) { var metadata = results.shift(); - var normalized = results.reduce(function(cache, result, i) { - // Could be just some rogue request - if (!result) { - return cache; - } - var resource = queries[i].resource; - var type = queries[i].type; - var manyResult = isarray(result); - // Inform template of ID in generic field - if (manyResult) { - result = result.map(function(data) { - return assign({}, data, {id: resource.getID(data)}) - }); - } else { - result = assign({}, result, {id: resource.getID(result)}); - } - // If we have a lot of results for a certain type, - // we pluralize the key and yield an array of results - if (cache[type] || manyResult) { - var plural = pluralize(type); - delete cache[type]; - cache[plural] = []; + if (single && !results[0]) { + reject(new Error('No resource found')); + } else { + var normalized = results.reduce(function(cache, result, i) { + // Could be just some rogue request + if (!result) { + return cache; + } + var resource = queries[i].resource; + var type = queries[i].type; + var manyResult = isarray(result); + // Inform template of ID in generic field if (manyResult) { - cache[plural] = cache[plural].concat(result); + result = result.map(function(data) { + return assign({}, data, {id: resource.getID(data)}) + }); + } else { + result = assign({}, result, {id: resource.getID(result)}); + } + // If we have a lot of results for a certain type, + // we pluralize the key and yield an array of results + if (cache[type] || manyResult) { + var plural = pluralize(type); + delete cache[type]; + cache[plural] = []; + if (manyResult) { + cache[plural] = cache[plural].concat(result); + } else { + cache[plural].push(result); + } } else { - cache[plural].push(result); + cache[type] = result; } - } else { - cache[type] = result; - } - return cache; - }, {meta: metadata}); - resolve(normalized); + return cache; + }, {meta: metadata}); + + resolve(normalized); + } }).fail(reject); }); } diff --git a/examples/index.js b/examples/index.js index 95d2bcf..3e9f509 100644 --- a/examples/index.js +++ b/examples/index.js @@ -4,6 +4,8 @@ var app = okcms.createApp({ root: 'public', + debug: false, + schemas: { page: { id: {type: 'string'}, diff --git a/themes/okadmin/templates/404.liquid b/themes/okadmin/templates/404.liquid new file mode 100644 index 0000000..87f5342 --- /dev/null +++ b/themes/okadmin/templates/404.liquid @@ -0,0 +1,54 @@ + + + + 404 + + + +
+

¯\_(ツ)_/¯

+

We couldn't find that page.

+

Sure you have the right URL?

+ Back +
+ + diff --git a/themes/okadmin/templates/5xx.liquid b/themes/okadmin/templates/5xx.liquid new file mode 100644 index 0000000..f245545 --- /dev/null +++ b/themes/okadmin/templates/5xx.liquid @@ -0,0 +1,54 @@ + + + + 404 + + + +
+

(;一_一)

+

Looks like we experienced an error.

+

Sorry about that. Maybe try again later.

+ Back +
+ + -- cgit v1.2.3-70-g09d2 From 34f1ca02bea38e0b7ef185ebf07d3ec6df30f370 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 13 Apr 2015 17:57:17 -0400 Subject: split up the projects by category, add a schema flag so we can use the same singular template --- app/index.js | 6 ++-- app/node_modules/okquery/index.js | 6 ++++ app/node_modules/okview/index.js | 2 +- site/db.json | 8 +++-- site/index.js | 67 +++++++++++++++++++++------------------ site/templates/index.liquid | 31 ++++++++---------- 6 files changed, 65 insertions(+), 55 deletions(-) (limited to 'app/index.js') diff --git a/app/index.js b/app/index.js index 419adfc..7860f76 100644 --- a/app/index.js +++ b/app/index.js @@ -87,7 +87,6 @@ function OKCMS(options) { this._createResources(resourceConfig, db, schemas); var errorHandler = createErrorHandlerProducer( templateProvider, adminTemplateProvider, debug); - // Create view instances from config var views = this._views = this._createViews(viewConfig, db, meta, resourceCache, templateProvider, @@ -141,7 +140,7 @@ OKCMS.prototype._createResources = function(resourceConfig, db, schemaCache) { var type = config.type; var schema = schemaCache[type]; if (!schema) - throw new Error('Resource config references nonexistent schema'); + throw new Error('Resource config references nonexistent schema ' + type); var resource = OKResource({ type: type, db: db, @@ -244,7 +243,8 @@ OKCMS.prototype._createQueries = function(queryConfig, resourceCache) { var query = config.query || '*'; return new OKQuery({ resource: resource, - query: query + query: query, + as: config.as, }); }); }; diff --git a/app/node_modules/okquery/index.js b/app/node_modules/okquery/index.js index 9cc8b78..519bc08 100644 --- a/app/node_modules/okquery/index.js +++ b/app/node_modules/okquery/index.js @@ -33,6 +33,12 @@ function OKQuery(options) { enumerable: true }); + Object.defineProperty(this, 'as', { + value: options.as, + writable: false, + enumerable: true + }); + this.get = createQuery(resource, query, { default: options.default }); diff --git a/app/node_modules/okview/index.js b/app/node_modules/okview/index.js index 63f22b5..951261c 100644 --- a/app/node_modules/okview/index.js +++ b/app/node_modules/okview/index.js @@ -139,7 +139,7 @@ function fetchTemplateData(meta, queries, id) { return cache; } var resource = queries[i].resource; - var type = queries[i].type; + var type = queries[i].as || queries[i].type; var manyResult = isarray(result); // Inform template of ID in generic field if (manyResult) { diff --git a/site/db.json b/site/db.json index b6fbf24..22833f1 100644 --- a/site/db.json +++ b/site/db.json @@ -1,9 +1,8 @@ { "meta": [], - "project": [ + "retail": [ { "id": "diesel-ss15", - "category": "advertising", "title": "DIESEL JOGG JEANS SS15 DENIM CAMPAIGN", "shortname": "DIESEL SS15", "description": "Sed posuere consectetur est at lobortis. Donec id elit non mi porta gravida at eget metus. Cras mattis consectetur purus sit amet fermentum. Vestibulum id ligula porta felis euismod semper. Donec sed odio dui. \r\n\r\nVestibulum id ligula porta felis euismod semper. Vestibulum id ligula porta felis euismod semper. Vestibulum id ligula porta felis euismod semper. Aenean lacinia bibendum nulla sed consectetur. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. \r\n\r\nCurabitur blandit tempus porttitor. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec ullamcorper nulla non metus auctor fringilla. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur blandit tempus porttitor. Maecenas sed diam eget risus varius blandit sit amet non magna.", @@ -50,5 +49,8 @@ "image": "http://checkingintocollege.com/wp/wp-content/uploads/2014/08/angryphone.jpg", "__index": 0 } - ] + ], + "advertising": [], + "experiential": [], + "content": [] } \ No newline at end of file diff --git a/site/index.js b/site/index.js index dace900..8eec697 100644 --- a/site/index.js +++ b/site/index.js @@ -1,5 +1,14 @@ var okcms = require('..'); +var projectSchema = { + id: {type: 'string', id: true}, + title: {type: 'string'}, + shortname: {type: 'string'}, + description: {type: 'text'}, + video: {type: 'video'}, + images: {type: 'captioned-image-list'}, +} + var app = okcms.createApp({ root: 'public', @@ -11,26 +20,19 @@ var app = okcms.createApp({ body: {type: 'text'}, image: {type: 'string'} }, - project: { - id: {type: 'string', id: true}, - title: {type: 'string'}, - shortname: {type: 'string'}, - category: {type: 'enum', options: [ - 'retail', - 'advertising', - 'experiential', - 'content'] - }, - description: {type: 'text'}, - video: {type: 'video'}, - images: {type: 'captioned-image-list'}, - } + retail: projectSchema, + advertising: projectSchema, + experiential: projectSchema, + content: projectSchema, }, resources: [ { type: 'page', static: {id: 'about'}}, { type: 'page', static: {id: 'contact'}}, - { type: 'project' } + { type: 'retail' }, + { type: 'advertising' }, + { type: 'experiential' }, + { type: 'content' } ], services: { @@ -44,8 +46,11 @@ var app = okcms.createApp({ views: { '/': { data: [ - {type: 'project', query: '*'}, - {type: 'page', query: '*'} + {type: 'page', query: '*'}, + {type: 'retail', query: '*'}, + {type: 'advertising', query: '*'}, + {type: 'experiential', query: '*'}, + {type: 'content', query: '*'}, ] }, '/about': { @@ -55,17 +60,19 @@ var app = okcms.createApp({ data: {type: 'page', query: 'contact'} }, '/all': { - data: { - type: 'project', - query: '*' - }, + data: [ + {type: 'retail', query: '*'}, + {type: 'advertising', query: '*'}, + {type: 'experiential', query: '*'}, + {type: 'content', query: '*'}, + ], template: 'all' }, '/retail/:id': { data: { - type: 'project', + type: 'retail', + as: 'project', query: { - category: 'retail', id: ':id' } }, @@ -73,9 +80,8 @@ var app = okcms.createApp({ }, '/advertising/:id': { data: { - type: 'project', + type: 'advertising', query: { - category: 'advertising', id: ':id' } }, @@ -83,9 +89,9 @@ var app = okcms.createApp({ }, '/experiential/:id': { data: { - type: 'project', + type: 'experiential', + as: 'project', query: { - category: 'experiential', id: ':id' } }, @@ -93,12 +99,13 @@ var app = okcms.createApp({ }, '/content/:id': { data: { - type: 'project', + type: 'content', + as: 'project', query: { - category: 'content', id: ':id' } - } + }, + template: 'project' } } diff --git a/site/templates/index.liquid b/site/templates/index.liquid index 954670a..0bd6b7d 100644 --- a/site/templates/index.liquid +++ b/site/templates/index.liquid @@ -85,36 +85,28 @@ WEBSITE BY OKFOCUS, http://okfoc.us, Internet Legends.