Skip to content

Commit

Permalink
add ability to override v3 routes in v4 and add example
Browse files Browse the repository at this point in the history
  • Loading branch information
paglias committed Jun 16, 2018
1 parent f4243f1 commit 78da4c6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions website/server/controllers/api-v4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See https://github.com/HabitRPG/habitica/pull/10453
13 changes: 13 additions & 0 deletions website/server/controllers/api-v4/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let api = {};

api.getStatus = {
method: 'GET',
url: '/status',
async handler (req, res) {
res.respond(200, {
status: 'v4 is up',
});
},
};

module.exports = api;
9 changes: 6 additions & 3 deletions website/server/libs/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import {
let _wrapAsyncFn = fn => (...args) => fn(...args).catch(args[2]);
let noop = (req, res, next) => next();

module.exports.readController = function readController (router, controller) {
module.exports.readController = function readController (router, controller, overrides = []) {
_.each(controller, (action) => {
let {method, url, middlewares = [], handler} = action;

// Allow to specify a list of routes (METHOD + URL) to skip
if (overrides.indexOf(`${method}-${url}`) !== -1) return;

// If an authentication middleware is used run getUserLanguage after it, otherwise before
// for cron instead use it only if an authentication middleware is present
let authMiddlewareIndex = _.findIndex(middlewares, middleware => {
Expand Down Expand Up @@ -44,15 +47,15 @@ module.exports.readController = function readController (router, controller) {
});
};

module.exports.walkControllers = function walkControllers (router, filePath) {
module.exports.walkControllers = function walkControllers (router, filePath, overrides) {
fs
.readdirSync(filePath)
.forEach(fileName => {
if (!fs.statSync(filePath + fileName).isFile()) {
walkControllers(router, `${filePath}${fileName}/`);
} else if (fileName.match(/\.js$/)) {
let controller = require(filePath + fileName); // eslint-disable-line global-require
module.exports.readController(router, controller);
module.exports.readController(router, controller, overrides);
}
});
};
14 changes: 11 additions & 3 deletions website/server/middlewares/appRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import routes from '../libs/routes';
import path from 'path';

const API_V3_CONTROLLERS_PATH = path.join(__dirname, '/../controllers/api-v3/');
const API_V4_CONTROLLERS_PATH = path.join(__dirname, '/../controllers/api-v4/');
const TOP_LEVEL_CONTROLLERS_PATH = path.join(__dirname, '/../controllers/top-level/');

const app = express();
Expand All @@ -27,10 +28,17 @@ const v3Router = express.Router(); // eslint-disable-line new-cap
routes.walkControllers(v3Router, API_V3_CONTROLLERS_PATH);
app.use('/api/v3', v3Router);

// API v4 proxies API v3
// Can also disable or override with its own version v3 routes
// API v4 proxies API v3 routes by default.
// It can also disable or override v3 routes

// A list of v3 routes in the format METHOD-URL to skip
const v4RouterOverrides = [
'GET-/status',
];

const v4Router = express.Router(); // eslint-disable-line new-cap
routes.walkControllers(v4Router, API_V3_CONTROLLERS_PATH);
routes.walkControllers(v4Router, API_V3_CONTROLLERS_PATH, v4RouterOverrides);
routes.walkControllers(v4Router, API_V4_CONTROLLERS_PATH);
app.use('/api/v4', v4Router);

module.exports = app;

0 comments on commit 78da4c6

Please sign in to comment.