# all files / keystone/admin/server/app/ createDynamicRouter.js

8.33% Statements 4/48
0% Branches 0/18
0% Functions 0/1
8.33% Lines 4/48

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    891×
    1×
    1×

    1×

     var bodyParser = require('body-parser');
    var express = require('express');
    var multer = require('multer');

    module.exports = function createDynamicRouter (keystone) {

    	// ensure keystone nav has been initialised
    	// TODO: move this elsewhere (on demand generation, or client-side?)
    	if (!keystone.nav) {
    		keystone.nav = keystone.initNav();
    	}

    	var router = express.Router();

    	// Use bodyParser and multer to parse request bodies and file uploads
    	router.use(bodyParser.json({}));
    	router.use(bodyParser.urlencoded({ extended: true }));
    	router.use(multer({ includeEmptyFields: true }));

    	// #1: Session API
    	// TODO: this should respect keystone auth options
    	router.get('/api/session', require('../api/session/get'));
    	router.post('/api/session/signin', require('../api/session/signin'));
    	router.post('/api/session/signout', require('../api/session/signout'));

    	// #2: Session Routes
    	// Bind auth middleware (generic or custom) to * routes, allowing
    	// access to the generic signin page if generic auth is used
    	if (keystone.get('auth') === true) {
    		// TODO: poor separation of concerns; settings should be defaulted elsewhere
    		if (!keystone.get('signout url')) {
    			keystone.set('signout url', '/' + keystone.get('admin path') + '/signout');
    		}
    		if (!keystone.get('signin url')) {
    			keystone.set('signin url', '/' + keystone.get('admin path') + '/signin');
    		}
    		if (!keystone.nativeApp || !keystone.get('session')) {
    			router.all('*', keystone.session.persist);
    		}
    		router.all('/signin', require('../routes/signin'));
    		router.all('/signout', require('../routes/signout'));
    		router.use(keystone.session.keystoneAuth);
    	} else if (typeof keystone.get('auth') === 'function') {
    		router.use(keystone.get('auth'));
    	}

    	// #3: Home route
    	router.get('/', require('../routes/home'));

    	// #4: Cloudinary and S3 specific APIs
    	// TODO: poor separation of concerns; should / could this happen elsewhere?
    	if (keystone.get('cloudinary config')) {
    		router.get('/api/cloudinary/get', require('../api/cloudinary').get);
    		router.get('/api/cloudinary/autocomplete', require('../api/cloudinary').autocomplete);
    		router.post('/api/cloudinary/upload', require('../api/cloudinary').upload);
    	}
    	if (keystone.get('s3 config')) {
    		router.post('/api/s3/upload', require('../api/s3').upload);
    	}

    	// #5: Core Lists API

    	// Init API request helpers
    	router.use('/api', require('../middleware/apiError'));
    	router.use('/api', require('../middleware/logError'));

    	// Init req with list
    	var initList = require('../middleware/initList')(keystone);
    	// lists
    	router.all('/api/counts', require('../api/counts'));
    	router.get('/api/:list', initList(), require('../api/list/get'));
    	router.get('/api/:list/:format(export.csv|export.json)', initList(), require('../api/list/download'));
    	router.post('/api/:list/create', initList(), require('../api/list/create'));
    	router.post('/api/:list/update', initList(), require('../api/list/update'));
    	router.post('/api/:list/delete', initList(), require('../api/list/delete'));
    	// items
    	router.get('/api/:list/:id', initList(), require('../api/item/get'));
    	router.post('/api/:list/:id', initList(), require('../api/item/update'));
    	router.post('/api/:list/:id/delete', initList(), require('../api/list/delete'));
    	router.post('/api/:list/:id/sortOrder/:sortOrder/:newOrder', initList(), require('../api/item/sortOrder'));
    	// #6: List Routes
    	router.all('/:list/:page([0-9]{1,5})?', initList(true), require('../routes/list'));
    	router.all('/:list/:item', initList(true), require('../routes/item'));

    	// TODO: catch 404s and errors with Admin-UI specific handlers

    	return router;
    };