summaryrefslogtreecommitdiffstats
path: root/server/router.js
diff options
context:
space:
mode:
authorUdo Walter2018-07-30 02:24:48 +0200
committerUdo Walter2018-07-30 02:24:48 +0200
commitb9186711eb2dedb475b5c77053f9c2a9a864066d (patch)
treedbd3095c9569b0bd16922532a50353d19625fab7 /server/router.js
parent[server/migrations] add ondelete cascade to group_x_group and group_x_client (diff)
downloadbas-b9186711eb2dedb475b5c77053f9c2a9a864066d.tar.gz
bas-b9186711eb2dedb475b5c77053f9c2a9a864066d.tar.xz
bas-b9186711eb2dedb475b5c77053f9c2a9a864066d.zip
[server/router] api calls are now dynamicly routed to the corresponding api module
Diffstat (limited to 'server/router.js')
-rw-r--r--server/router.js38
1 files changed, 18 insertions, 20 deletions
diff --git a/server/router.js b/server/router.js
index 9e29f93..828f8b7 100644
--- a/server/router.js
+++ b/server/router.js
@@ -2,16 +2,6 @@ var express = require('express')
var router = express.Router()
var path = require('path')
-function mapActions (api, method) {
- return function (req, res) {
- if (method in api && req.params.action in api[method]) {
- api[method][req.params.action](req, res)
- } else {
- res.status(501).end()
- }
- }
-}
-
// Authentication routes
var auth = require(path.join(__dirname, 'lib', 'authentication'))
router.get('/auth', auth.auth)
@@ -20,20 +10,11 @@ router.post('/signup', auth.signup)
router.post('/logout', auth.logout)
router.post('/changepassword', auth.changePassword)
+// ############ Legacy Code: TODO(Jannik): Rework to api and get/post or delete! ############
// User API
var user = require(path.join(__dirname, 'api', 'user'))
router.get('/user/info', auth.verifyToken, user.info)
-// Groups API
-var groups = require(path.join(__dirname, 'api', 'groups'))
-router.get('/groups/:action', mapActions(groups, 'get'))
-router.post('/groups/:action', mapActions(groups, 'post'))
-
-// Clients API
-var clients = require(path.join(__dirname, 'api', 'clients'))
-router.get('/clients/:action', mapActions(clients, 'get'))
-router.post('/clients/:action', mapActions(clients, 'post'))
-
// Permissions API
var permissions = require(path.join(__dirname, 'api', 'permissions'))
router.get('/getRolesByUserid', permissions.getRolesByUserid)
@@ -61,5 +42,22 @@ router.post('/backends/checkConnection', auth.verifyToken, backends.checkConnect
// Load ipxe scipts API
var ipxeloader = require(path.join(__dirname, 'api', 'ipxe-loader'))
router.get('/ipxe-loader/load-script', ipxeloader.loadScript)
+// ############################################################################
+
+// 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 /<api>/<action>
+router.get('/:api/:action', auth.verifyToken, mapApi('get'))
+router.post('/:api/:action', auth.verifyToken, mapApi('post'))
module.exports = router