-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutemanager.ts
129 lines (113 loc) · 4.22 KB
/
routemanager.ts
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import fs from 'fs';
import path from 'path';
import { info, error, debug } from './logmanager';
import { Application } from 'express';
/**
* Dynamically loads backend and frontend files and loads the views.
* @param app
*/
export function loadRoutes(app: Application) {
const backendPath = path.join(path.join(__dirname, 'views'), 'backend');
const backendFiles = fs.readdirSync(backendPath).filter(file => file.endsWith('.ts'));
const frontendPath = path.join(path.join(__dirname, 'views'), 'frontend');
info(`Loading backend files from ${backendPath}`);
/**
* Looping through all *.js files in backend folder
*/
for (const file of backendFiles) {
const filePath = path.join(backendPath, file);
info(`Loading ${file}`);
/**
* Require the found file to import eveything inside the module.exports array
*/
const route = require(filePath);
/**
* Sanity checks start here
*/
/**
* Check if the file has title in module.exports and if it is a string
*/
if (typeof route.title !== 'string') {
error('title is not a string or not set! skipping File.');
continue;
}
/**
* Check if the file has urlpath in module.exports and if it is a string
*/
if (typeof route.urlpath !== 'string') {
error('urlpath is not a string or not set! skipping File.');
continue;
}
/**
* Check if the file has pugfile in module.exports and if it is a string
*/
if (typeof route.pugfile !== 'string') {
error('pugfile is not a string or not set! skipping File.');
continue;
}
/**
* Check if the pugfile actually exists in the frontend directory
*/
if (!fs.existsSync(path.join(frontendPath, route.pugfile))) {
error(`The file ${route.pugfile} does not exist! skipping File.`);
continue;
}
/**
* Dynamically loading options starts here
*/
const options: any[string] = [];
options['title'] = route.title;
/**
* Check if backend file has an onLoad() function in module.exports
* onLoad() has to return a Map object containing options.
* If you have no options to load, return an empty Map.
*/
if (typeof route.onLoad === 'function') {
debug('File has declared an onLoad() function! Calling now...');
const variables: Map<string, any> = route.onLoad();
/**
* Check if map is not empty
*/
if (variables.size > 0) {
/**
* Looping through all the entries in the map and putting them into the options Array.
*/
for (const key of variables.keys()) {
const value = variables.get(key);
options[key] = value;
debug(`Loaded key: ${key} as value: ${value}`);
}
}
}
/**
* Now finally set the routes, this basically can be translated to:
* if route.urlpath is called, render route.pugfile with given options and execute the function
*/
app.get(route.urlpath, (req, res) => {
res.render(route.pugfile, options, function (err, html) {
debug(`Route called: ${route.urlpath} with title: ${options.title}`);
if (err) {
throw err;
}
if (typeof route.onCall === 'function') {
debug('File has declared an onCall() function! Calling now...');
route.onCall();
}
/**
* Finally send the html to the browser.
*/
res.send(html);
});
});
}
/**
* External links start here
*/
app.get('/github', (req, res) => {
res.redirect('https://github.com');
});
// 404 Error, has to be called last (after all other pages)
app.use(function (req, res) {
res.status(404).render('404', { title: '404 - ' + req.path, page: req.path });
});
}