var express = require('express') var router = express.Router() var path = require('path') var fs = require('fs') // Authentication routes var auth = require(path.join(__dirname, 'lib', 'authentication')) router.get('/auth', auth.auth) router.post('/login', auth.login) router.post('/signup', auth.signup) router.post('/logout', auth.logout) router.post('/changepassword', auth.changePassword) // Public callable functions. var ipxe = require(path.join(__dirname, 'api', 'ipxe')) router.get('/ipxe/loadScript', ipxe.get.loadScript) // Forward routing to every api module with //... fs.readdirSync(path.join(__dirname, 'api')).forEach(filename => { var api = require(path.join(__dirname, 'api', filename)) if (api.router) router.use('/' + filename.split('.')[0] + '/', auth.verifyToken, api.router) }) // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // !!!!!!!! TODO: DELETE IF ALL MODULES ARE REWORKED WITH exports.router !!!!!!! // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // Dynamic API routes function mapApi (method) { return function (req, res) { var api = require(path.join(__dirname, 'api', req.params.api)) if (method in api && req.params.action in api[method]) { api[method][req.params.action](req, res) } else { res.status(501).end() } } } // Every API can be called with // router.get('/:api/:action', auth.verifyToken, mapApi('get')) router.post('/:api/:action', auth.verifyToken, mapApi('post')) // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! router.use('*', (req, res) => { res.status(404).end() }) module.exports = router