diff --git a/src/routes/admin.js b/src/routes/admin.js index da9fbc0b4c..7e8f2ccbc5 100644 --- a/src/routes/admin.js +++ b/src/routes/admin.js @@ -1,108 +1,86 @@ 'use strict'; -var express = require('express'); +const helpers = require('./helpers'); +module.exports = function (app, middleware, controllers) { + const middlewares = [middleware.pluginHooks]; + + helpers.setupAdminPageRoute(app, '/admin', middleware, middlewares, controllers.admin.dashboard.get); + + helpers.setupAdminPageRoute(app, '/admin/general/dashboard', middleware, middlewares, controllers.admin.dashboard.get); + helpers.setupAdminPageRoute(app, '/admin/general/languages', middleware, middlewares, controllers.admin.languages.get); + helpers.setupAdminPageRoute(app, '/admin/general/sounds', middleware, middlewares, controllers.admin.sounds.get); + helpers.setupAdminPageRoute(app, '/admin/general/navigation', middleware, middlewares, controllers.admin.navigation.get); + helpers.setupAdminPageRoute(app, '/admin/general/homepage', middleware, middlewares, controllers.admin.homepage.get); + helpers.setupAdminPageRoute(app, '/admin/general/social', middleware, middlewares, controllers.admin.social.get); + + helpers.setupAdminPageRoute(app, '/admin/manage/categories', middleware, middlewares, controllers.admin.categories.getAll); + helpers.setupAdminPageRoute(app, '/admin/manage/categories/:category_id', middleware, middlewares, controllers.admin.categories.get); + helpers.setupAdminPageRoute(app, '/admin/manage/categories/:category_id/analytics', middleware, middlewares, controllers.admin.categories.getAnalytics); + + helpers.setupAdminPageRoute(app, '/admin/manage/privileges/:cid?', middleware, middlewares, controllers.admin.privileges.get); + helpers.setupAdminPageRoute(app, '/admin/manage/tags', middleware, middlewares, controllers.admin.tags.get); + helpers.setupAdminPageRoute(app, '/admin/manage/post-queue', middleware, middlewares, controllers.admin.postQueue.get); + helpers.setupAdminPageRoute(app, '/admin/manage/ip-blacklist', middleware, middlewares, controllers.admin.blacklist.get); + + helpers.setupAdminPageRoute(app, '/admin/manage/users', middleware, middlewares, controllers.admin.users.sortByJoinDate); + helpers.setupAdminPageRoute(app, '/admin/manage/users/search', middleware, middlewares, controllers.admin.users.search); + helpers.setupAdminPageRoute(app, '/admin/manage/users/latest', middleware, middlewares, controllers.admin.users.sortByJoinDate); + helpers.setupAdminPageRoute(app, '/admin/manage/users/not-validated', middleware, middlewares, controllers.admin.users.notValidated); + helpers.setupAdminPageRoute(app, '/admin/manage/users/no-posts', middleware, middlewares, controllers.admin.users.noPosts); + helpers.setupAdminPageRoute(app, '/admin/manage/users/top-posters', middleware, middlewares, controllers.admin.users.topPosters); + helpers.setupAdminPageRoute(app, '/admin/manage/users/most-reputation', middleware, middlewares, controllers.admin.users.mostReputaion); + helpers.setupAdminPageRoute(app, '/admin/manage/users/inactive', middleware, middlewares, controllers.admin.users.inactive); + helpers.setupAdminPageRoute(app, '/admin/manage/users/flagged', middleware, middlewares, controllers.admin.users.flagged); + helpers.setupAdminPageRoute(app, '/admin/manage/users/banned', middleware, middlewares, controllers.admin.users.banned); + helpers.setupAdminPageRoute(app, '/admin/manage/registration', middleware, middlewares, controllers.admin.users.registrationQueue); + + helpers.setupAdminPageRoute(app, '/admin/manage/admins-mods', middleware, middlewares, controllers.admin.adminsMods.get); + + helpers.setupAdminPageRoute(app, '/admin/manage/groups', middleware, middlewares, controllers.admin.groups.list); + helpers.setupAdminPageRoute(app, '/admin/manage/groups/:name', middleware, middlewares, controllers.admin.groups.get); + + helpers.setupAdminPageRoute(app, '/admin/manage/uploads', middleware, middlewares, controllers.admin.uploads.get); + helpers.setupAdminPageRoute(app, '/admin/manage/digest', middleware, middlewares, controllers.admin.digest.get); + + helpers.setupAdminPageRoute(app, '/admin/settings/:term?', middleware, middlewares, controllers.admin.settings.get); + + helpers.setupAdminPageRoute(app, '/admin/appearance/:term?', middleware, middlewares, controllers.admin.appearance.get); + + helpers.setupAdminPageRoute(app, '/admin/extend/plugins', middleware, middlewares, controllers.admin.plugins.get); + helpers.setupAdminPageRoute(app, '/admin/extend/widgets', middleware, middlewares, controllers.admin.extend.widgets.get); + helpers.setupAdminPageRoute(app, '/admin/extend/rewards', middleware, middlewares, controllers.admin.extend.rewards.get); + + helpers.setupAdminPageRoute(app, '/admin/advanced/database', middleware, middlewares, controllers.admin.database.get); + helpers.setupAdminPageRoute(app, '/admin/advanced/events', middleware, middlewares, controllers.admin.events.get); + helpers.setupAdminPageRoute(app, '/admin/advanced/hooks', middleware, middlewares, controllers.admin.hooks.get); + helpers.setupAdminPageRoute(app, '/admin/advanced/logs', middleware, middlewares, controllers.admin.logs.get); + helpers.setupAdminPageRoute(app, '/admin/advanced/errors', middleware, middlewares, controllers.admin.errors.get); + helpers.setupAdminPageRoute(app, '/admin/advanced/errors/export', middleware, middlewares, controllers.admin.errors.export); + helpers.setupAdminPageRoute(app, '/admin/advanced/cache', middleware, middlewares, controllers.admin.cache.get); + + helpers.setupAdminPageRoute(app, '/admin/development/logger', middleware, middlewares, controllers.admin.logger.get); + helpers.setupAdminPageRoute(app, '/admin/development/info', middleware, middlewares, controllers.admin.info.get); + + apiRoutes(app, middleware, controllers); +}; -function apiRoutes(router, middleware, controllers) { - router.get('/users/csv', middleware.authenticate, controllers.admin.users.getCSV); - router.get('/analytics', middleware.authenticate, controllers.admin.dashboard.getAnalytics); - - var multipart = require('connect-multiparty'); - var multipartMiddleware = multipart(); - - var middlewares = [multipartMiddleware, middleware.validateFiles, middleware.applyCSRF, middleware.authenticate]; - - router.post('/category/uploadpicture', middlewares, controllers.admin.uploads.uploadCategoryPicture); - router.post('/uploadfavicon', middlewares, controllers.admin.uploads.uploadFavicon); - router.post('/uploadTouchIcon', middlewares, controllers.admin.uploads.uploadTouchIcon); - router.post('/uploadlogo', middlewares, controllers.admin.uploads.uploadLogo); - router.post('/uploadOgImage', middlewares, controllers.admin.uploads.uploadOgImage); - router.post('/upload/sound', middlewares, controllers.admin.uploads.uploadSound); - router.post('/upload/file', middlewares, controllers.admin.uploads.uploadFile); - router.post('/uploadDefaultAvatar', middlewares, controllers.admin.uploads.uploadDefaultAvatar); -} - -function adminRouter(middleware, controllers) { - var router = express.Router(); - - router.use(middleware.admin.buildHeader); - - addRoutes(router, middleware, controllers); - - return router; -} - -function apiRouter(middleware, controllers) { - var router = express.Router(); - - addRoutes(router, middleware, controllers); - - apiRoutes(router, middleware, controllers); - - return router; -} -function addRoutes(router, middleware, controllers) { - var middlewares = [middleware.pluginHooks]; - - router.get('/', middlewares, controllers.admin.dashboard.get); - router.get('/general/dashboard', middlewares, controllers.admin.dashboard.get); - router.get('/general/languages', middlewares, controllers.admin.languages.get); - router.get('/general/sounds', middlewares, controllers.admin.sounds.get); - router.get('/general/navigation', middlewares, controllers.admin.navigation.get); - router.get('/general/homepage', middlewares, controllers.admin.homepage.get); - router.get('/general/social', middlewares, controllers.admin.social.get); - - router.get('/manage/categories', middlewares, controllers.admin.categories.getAll); - router.get('/manage/categories/:category_id', middlewares, controllers.admin.categories.get); - router.get('/manage/categories/:category_id/analytics', middlewares, controllers.admin.categories.getAnalytics); - - router.get('/manage/privileges/:cid?', middlewares, controllers.admin.privileges.get); - router.get('/manage/tags', middlewares, controllers.admin.tags.get); - router.get('/manage/post-queue', middlewares, controllers.admin.postQueue.get); - router.get('/manage/ip-blacklist', middlewares, controllers.admin.blacklist.get); - - router.get('/manage/users', middlewares, controllers.admin.users.sortByJoinDate); - router.get('/manage/users/search', middlewares, controllers.admin.users.search); - router.get('/manage/users/latest', middlewares, controllers.admin.users.sortByJoinDate); - router.get('/manage/users/not-validated', middlewares, controllers.admin.users.notValidated); - router.get('/manage/users/no-posts', middlewares, controllers.admin.users.noPosts); - router.get('/manage/users/top-posters', middlewares, controllers.admin.users.topPosters); - router.get('/manage/users/most-reputation', middlewares, controllers.admin.users.mostReputaion); - router.get('/manage/users/inactive', middlewares, controllers.admin.users.inactive); - router.get('/manage/users/flagged', middlewares, controllers.admin.users.flagged); - router.get('/manage/users/banned', middlewares, controllers.admin.users.banned); - router.get('/manage/registration', middlewares, controllers.admin.users.registrationQueue); - - router.get('/manage/admins-mods', middlewares, controllers.admin.adminsMods.get); - - router.get('/manage/groups', middlewares, controllers.admin.groups.list); - router.get('/manage/groups/:name', middlewares, controllers.admin.groups.get); - - router.get('/manage/uploads', middlewares, controllers.admin.uploads.get); - router.get('/manage/digest', middlewares, controllers.admin.digest.get); - - router.get('/settings/:term?', middlewares, controllers.admin.settings.get); - - router.get('/appearance/:term?', middlewares, controllers.admin.appearance.get); - - router.get('/extend/plugins', middlewares, controllers.admin.plugins.get); - router.get('/extend/widgets', middlewares, controllers.admin.extend.widgets.get); - router.get('/extend/rewards', middlewares, controllers.admin.extend.rewards.get); - - router.get('/advanced/database', middlewares, controllers.admin.database.get); - router.get('/advanced/events', middlewares, controllers.admin.events.get); - router.get('/advanced/hooks', middlewares, controllers.admin.hooks.get); - router.get('/advanced/logs', middlewares, controllers.admin.logs.get); - router.get('/advanced/errors', middlewares, controllers.admin.errors.get); - router.get('/advanced/errors/export', middlewares, controllers.admin.errors.export); - router.get('/advanced/cache', middlewares, controllers.admin.cache.get); - - router.get('/development/logger', middlewares, controllers.admin.logger.get); - router.get('/development/info', middlewares, controllers.admin.info.get); +function apiRoutes(router, middleware, controllers) { + router.get('/api/admin/users/csv', middleware.authenticate, helpers.tryRoute(controllers.admin.users.getCSV)); + router.get('/api/admin/analytics', middleware.authenticate, helpers.tryRoute(controllers.admin.dashboard.getAnalytics)); + + const multipart = require('connect-multiparty'); + const multipartMiddleware = multipart(); + + const middlewares = [multipartMiddleware, middleware.validateFiles, middleware.applyCSRF, middleware.authenticate]; + + router.post('/api/admin/category/uploadpicture', middlewares, helpers.tryRoute(controllers.admin.uploads.uploadCategoryPicture)); + router.post('/api/admin/uploadfavicon', middlewares, helpers.tryRoute(controllers.admin.uploads.uploadFavicon)); + router.post('/api/admin/uploadTouchIcon', middlewares, helpers.tryRoute(controllers.admin.uploads.uploadTouchIcon)); + router.post('/api/admin/uploadlogo', middlewares, helpers.tryRoute(controllers.admin.uploads.uploadLogo)); + router.post('/api/admin/uploadOgImage', middlewares, helpers.tryRoute(controllers.admin.uploads.uploadOgImage)); + router.post('/api/admin/upload/sound', middlewares, helpers.tryRoute(controllers.admin.uploads.uploadSound)); + router.post('/api/admin/upload/file', middlewares, helpers.tryRoute(controllers.admin.uploads.uploadFile)); + router.post('/api/admin/uploadDefaultAvatar', middlewares, helpers.tryRoute(controllers.admin.uploads.uploadDefaultAvatar)); } - -module.exports = function (app, middleware, controllers) { - app.use('/admin/', adminRouter(middleware, controllers)); - app.use('/api/admin/', apiRouter(middleware, controllers)); -}; diff --git a/src/routes/helpers.js b/src/routes/helpers.js index b3bf038b8b..85c3dd1196 100644 --- a/src/routes/helpers.js +++ b/src/routes/helpers.js @@ -5,22 +5,24 @@ var helpers = module.exports; helpers.setupPageRoute = function (router, name, middleware, middlewares, controller) { middlewares = [middleware.maintenanceMode, middleware.registrationComplete, middleware.pageView, middleware.pluginHooks].concat(middlewares); - async function tryRoute(req, res, next) { - if (controller && controller.constructor && controller.constructor.name === 'AsyncFunction') { + router.get(name, middleware.busyCheck, middleware.buildHeader, middlewares, helpers.tryRoute(controller)); + router.get('/api' + name, middlewares, helpers.tryRoute(controller)); +}; + +helpers.setupAdminPageRoute = function (router, name, middleware, middlewares, controller) { + router.get(name, middleware.admin.buildHeader, middlewares, helpers.tryRoute(controller)); + router.get('/api' + name, middlewares, helpers.tryRoute(controller)); +}; + +helpers.tryRoute = function (controller) { + if (controller && controller.constructor && controller.constructor.name === 'AsyncFunction') { + return async function (req, res, next) { try { - return await controller(req, res, next); + await controller(req, res, next); } catch (err) { - return next(err); + next(err); } - } - controller(req, res, next); + }; } - - router.get(name, middleware.busyCheck, middleware.buildHeader, middlewares, tryRoute); - router.get('/api' + name, middlewares, tryRoute); -}; - -helpers.setupAdminPageRoute = function (router, name, middleware, middlewares, controller) { - router.get(name, middleware.admin.buildHeader, middlewares, controller); - router.get('/api' + name, middlewares, controller); + return controller; };