Reorganized routes code into a separate file

This commit is contained in:
Gregory Ballantine 2023-11-27 21:29:52 -05:00
parent 34faecc52c
commit 286219c5ea
3 changed files with 42 additions and 33 deletions

View File

@ -32,32 +32,8 @@ app.set('view engine', 'twig');
// enable serving static files // enable serving static files
app.use(express.static('public')); app.use(express.static('public'));
// load routes // load routes to express
const indexRoutes = require('./src/routes/index'); require('./src/routes')(app);
const projectRoutes = require('./src/routes/project');
const hardwareRoutes = require('./src/routes/hardware');
const benchmarkRoutes = require('./src/routes/benchmark');
// register routes
app.get('/', indexRoutes.getIndex);
// project routes
app.get('/project', projectRoutes.getIndex);
app.get('/project/list', projectRoutes.getList);
app.get('/project/add', projectRoutes.getAdd);
app.post('/project/add', projectRoutes.postAdd);
app.get('/project/:project_id', projectRoutes.getView);
// hardware routes
app.get('/hardware', hardwareRoutes.getIndex);
app.get('/hardware/list', hardwareRoutes.getList);
app.get('/hardware/add', hardwareRoutes.getAdd);
app.post('/hardware/add', hardwareRoutes.postAdd);
app.get('/hardware/:hardware_id', hardwareRoutes.getView);
// benchmark routes
app.get('/benchmark', benchmarkRoutes.getIndex);
app.get('/benchmark/list', benchmarkRoutes.getList);
app.get('/benchmark/add', benchmarkRoutes.getAdd);
app.post('/benchmark/add', benchmarkRoutes.postAdd);
app.get('/benchmark/:benchmark_id', benchmarkRoutes.getView);
app.listen(port, () => { app.listen(port, () => {
console.log(`Leviathan listening on port ${port}`); console.log(`Leviathan listening on port ${port}`);

View File

@ -1,9 +1,33 @@
const Project = require('../models').models.Project; // load routes
const topRoutes = require('./toplevel');
const projectRoutes = require('./project');
const hardwareRoutes = require('./hardware');
const benchmarkRoutes = require('./benchmark');
module.exports = function(app) {
// top-level routes
app.get('/', topRoutes.getIndex);
// project routes
app.get('/project', projectRoutes.getIndex);
app.get('/project/list', projectRoutes.getList);
app.get('/project/add', projectRoutes.getAdd);
app.post('/project/add', projectRoutes.postAdd);
app.get('/project/:project_id', projectRoutes.getView);
// hardware routes
app.get('/hardware', hardwareRoutes.getIndex);
app.get('/hardware/list', hardwareRoutes.getList);
app.get('/hardware/add', hardwareRoutes.getAdd);
app.post('/hardware/add', hardwareRoutes.postAdd);
app.get('/hardware/:hardware_id', hardwareRoutes.getView);
// benchmark routes
app.get('/benchmark', benchmarkRoutes.getIndex);
app.get('/benchmark/list', benchmarkRoutes.getList);
app.get('/benchmark/add', benchmarkRoutes.getAdd);
app.post('/benchmark/add', benchmarkRoutes.postAdd);
app.get('/benchmark/:benchmark_id', benchmarkRoutes.getView);
// GET / - primary app dashboard
exports.getIndex = async function(req, res) {
var projects = await Project.findAll();
res.render('index/dashboard', {
projects: projects
});
}; };

9
src/routes/toplevel.js Normal file
View File

@ -0,0 +1,9 @@
const Project = require('../models').models.Project;
// GET / - primary app dashboard
exports.getIndex = async function(req, res) {
var projects = await Project.findAll();
res.render('index/dashboard', {
projects: projects
});
};