summaryrefslogblamecommitdiffstats
path: root/server/router.js
blob: f77d09a5140ee8fa63cfd8efd13ebcd86610bf7d (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11


                                

                        





                                                                 
 



                                                       














                                                                  
 



                               
                       
var express = require('express')
var router = express.Router()
var path = require('path')

// 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)

// 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'))

router.use('*', (req, res) => {
  res.status(404).end()
})

module.exports = router