summaryrefslogtreecommitdiffstats
path: root/server/router.js
diff options
context:
space:
mode:
authorJannik Schönartz2018-08-10 04:41:57 +0200
committerJannik Schönartz2018-08-10 04:41:57 +0200
commit17591fc7edf4104895b5842d9bf3b0e8eff98304 (patch)
tree619f44ce618f41771a617a8a5c289263c04346d7 /server/router.js
parent[permissions] fix bug with recursive-mode-switch (diff)
downloadbas-17591fc7edf4104895b5842d9bf3b0e8eff98304.tar.gz
bas-17591fc7edf4104895b5842d9bf3b0e8eff98304.tar.xz
bas-17591fc7edf4104895b5842d9bf3b0e8eff98304.zip
[server/router] Rework router
Reworked the router to match more design guidlines with the REST apis
Diffstat (limited to 'server/router.js')
-rw-r--r--server/router.js15
1 files changed, 15 insertions, 0 deletions
diff --git a/server/router.js b/server/router.js
index f77d09a..fbd71f2 100644
--- a/server/router.js
+++ b/server/router.js
@@ -1,6 +1,7 @@
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'))
@@ -14,6 +15,16 @@ router.post('/changepassword', auth.changePassword)
var ipxe = require(path.join(__dirname, 'api', 'ipxe'))
router.get('/ipxe/loadScript', ipxe.get.loadScript)
+// Forward routing to every api module with /<api>/...
+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) {
@@ -30,6 +41,10 @@ function mapApi (method) {
router.get('/:api/:action', auth.verifyToken, mapApi('get'))
router.post('/:api/:action', auth.verifyToken, mapApi('post'))
+// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
router.use('*', (req, res) => {
res.status(404).end()
})