Golopotw has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/365539 )
Change subject: Simplify loadRoutes() ...................................................................... Simplify loadRoutes() The previous revision finds existing files in directory ./routes to load routes, this is replaced by hard coded routes, that is, the convention way, for simplicity. Also the convoluted pattern to mouting router onto app is simplified. Change-Id: I23c958f96dc065a8d3fb024c16fbb3ae281b7c3e --- M app.js M routes/info.js M routes/mathoid.js M routes/root.js 4 files changed, 26 insertions(+), 128 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mathoid refs/changes/39/365539/1 diff --git a/app.js b/app.js index 1bfcc89..f102454 100644 --- a/app.js +++ b/app.js @@ -159,47 +159,11 @@ */ function loadRoutes (app) { - // get the list of files in routes/ - return fs.readdirAsync(__dirname + '/routes').map(function(fname) { - return BBPromise.try(function() { - // ... and then load each route - // but only if it's a js file - if(!/\.js$/.test(fname)) { - return undefined; - } - // import the route file - var route = require(__dirname + '/routes/' + fname); - return route(app); - }).then(function(route) { - if(route === undefined) { - return undefined; - } - // check that the route exports the object we need - if(route.constructor !== Object || !route.path || !route.router || !(route.api_version || route.skip_domain)) { - throw new TypeError('routes/' + fname + ' does not export the correct object!'); - } - // normalise the path to be used as the mount point - if(route.path[0] !== '/') { - route.path = '/' + route.path; - } - if(route.path[route.path.length - 1] !== '/') { - route.path = route.path + '/'; - } - if(!route.skip_domain) { - route.path = '/:domain/v' + route.api_version + route.path; - } - // wrap the route handlers with Promise.try() blocks - sUtil.wrapRouteHandlers(route, app); - // all good, use that route - app.use(route.path, route.router); - }); - }).then(function () { - // catch errors - sUtil.setErrorHandler(app); - // route loading is now complete, return the app object - return BBPromise.resolve(app); - }); - + app.use('/', require('./routes/root')); + app.use('/', require('./routes/mathoid')); + app.use('/_info', require('./routes/info')); + sUtil.setErrorHandler(app); + return BBPromise.resolve(app); } diff --git a/routes/info.js b/routes/info.js index 5dd2590..905e904 100644 --- a/routes/info.js +++ b/routes/info.js @@ -1,18 +1,7 @@ 'use strict'; - -var sUtil = require('../lib/util'); - - -/** - * The main router object - */ -var router = sUtil.router(); - -/** - * The main application object reported when this module is require()d - */ -var app; +var express = require('express'); +var router = express.Router(); /** @@ -23,10 +12,10 @@ // simple sync return res.json({ - name: app.info.name, - version: app.info.version, - description: app.info.description, - home: app.info.homepage + name: req.app.info.name, + version: req.app.info.version, + description: req.app.info.description, + home: req.app.info.homepage }); }); @@ -39,7 +28,7 @@ router.get('/name', function(req, res) { // simple return - res.json({ name: app.info.name }); + res.json({ name: req.app.info.name }); }); @@ -51,7 +40,7 @@ router.get('/version', function(req, res) { // simple return - res.json({ version: app.info.version }); + res.json({ version: req.app.info.version }); }); @@ -63,28 +52,17 @@ */ router.all('/home', function(req, res) { - var home = app.info.homepage; + var home = req.app.info.homepage; if(home && /^http/.test(home)) { // we have a home page URI defined, so send it res.redirect(301, home); return; } else { // no URI defined for the home page, error out - res.status(404).end('No home page URL defined for ' + app.info.name); + res.status(404).end('No home page URL defined for ' + req.app.info.name); } }); -module.exports = function(appObj) { - - app = appObj; - - return { - path: '/_info', - skip_domain: true, - router: router - }; - -}; - +module.exports = router; diff --git a/routes/mathoid.js b/routes/mathoid.js index 433c9ce..35ca193 100644 --- a/routes/mathoid.js +++ b/routes/mathoid.js @@ -13,17 +13,8 @@ ] }); - -/** - * The main router object - */ -var router = sUtil.router(); - -/** - * The main application object reported when this module is require()d - */ -var app; - +var express = require('express'); +var router = express.Router(); /* The response headers for different render types */ var outHeaders = function (data) { @@ -78,6 +69,7 @@ }; function handleRequest(res, q, type, outFormat, features, req) { + var app = req.app; var sanitizedTex, feedback; var svg = app.conf.svg && /^svg|json|complete$/.test(outFormat); var mml = (type !== "MathML") && /^mml|json|complete$/.test(outFormat); @@ -181,7 +173,7 @@ */ router.post('/:outformat?/', function (req, res) { var outFormat; - var speech = app.conf.speech_on; + var speech = req.app.conf.speech_on; // First some rudimentary input validation if (!(req.body.q)) { emitError("q (query) post parameter is missing!"); @@ -214,7 +206,7 @@ speech = false; } function setOutFormat(fmt) { - if (app.conf[fmt] || (fmt === 'graph' && app.conf.texvcinfo)) { + if (req.app.conf[fmt] || (fmt === 'graph' && req.app.conf.texvcinfo)) { outFormat = fmt; } else { emitFormatError(fmt); @@ -264,16 +256,4 @@ }); - -module.exports = function (appObj) { - - app = appObj; - - return { - path: '/', - skip_domain: true, - router: router - }; - -}; - +module.exports = router; diff --git a/routes/root.js b/routes/root.js index 3b34d8e..3286d8f 100644 --- a/routes/root.js +++ b/routes/root.js @@ -1,19 +1,6 @@ 'use strict'; - - -var sUtil = require('../lib/util'); - - -/** - * The main router object - */ -var router = sUtil.router(); - -/** - * The main application object reported when this module is require()d - */ -var app; - +var express = require('express'); +var router = express.Router(); /** * GET /robots.txt @@ -39,21 +26,10 @@ if(!(req.query || {}).hasOwnProperty('spec')) { next(); } else { - res.json(app.conf.spec); + res.json(req.app.conf.spec); } }); -module.exports = function(appObj) { - - app = appObj; - - return { - path: '/', - skip_domain: true, - router: router - }; - -}; - +module.exports = router; -- To view, visit https://gerrit.wikimedia.org/r/365539 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I23c958f96dc065a8d3fb024c16fbb3ae281b7c3e Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/mathoid Gerrit-Branch: master Gerrit-Owner: Golopotw <golo...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits